Powered by AppSignal & Oban Pro

Scratch

cli_ex/scratch.livemd

Scratch

Getting Started with Elixir

defmodule Count do
  def count(0) do
    IO.puts("\rDone")
  end

  def count(value) do
    IO.write("\r#{value}")
    Process.sleep(500)
    count(value - 1)
  end
end
Count.count(15)
defmodule Progress do
  @rounding_precision 2
  @progress_bar_size 50

  def bar(count, total) do
    percent = percent_complete(count, total)
    divisor = 100 / @progress_bar_size

    completed_count = round(percent / divisor)
    incomplete_count = @progress_bar_size - completed_count

    "#{repeat(completed_count, "*")}#{repeat(incomplete_count, "-")} #{percent}%"
  end

  defp percent_complete(count, total) do
    Float.round(100.0 * (count / total), @rounding_precision)
  end

  defp repeat(count, _) when count < 1, do: ""

  defp repeat(count, str) do
    1..count
    |> Enum.map(fn _ -> str end)
    |> Enum.join()
  end
end

total = 50

Enum.each(1..total, fn task ->
  IO.write("\r#{Progress.bar(task, total)}")
  Process.sleep(100)
end)

IO.puts("\n")
defmodule Color do
  def blue(text) do
    IO.ANSI.blue() <> text <> IO.ANSI.reset()
  end
end

Color.blue("THIS IS BLUE") |> IO.puts()
Enum.each(["one", "two", "three"], fn x -> IO.puts(x) end)
owl =
  """
     ,_,
    {o,o}
    /)  )
  ---"-"--
  """
  |> String.trim_trailing()

colors = [:blue, :red, :cyan, :yellow, :green, :black]

1..6
|> Enum.map(fn index ->
  owl
  |> Owl.Data.tag(Enum.random(colors))
  |> Owl.Box.new(title: to_string(index))
  |> Owl.Data.tag(Enum.random(colors))
end)
|> Enum.reverse()
|> Enum.reduce(&amp;Owl.Data.zip/2)
|> Owl.Box.new(title: "6 owls")
|> Owl.Data.tag(:magenta)
|> Owl.IO.puts()