zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

shell.ex (2607B)


      1 defmodule Credo.CLI.Output.Shell do
      2   @moduledoc false
      3 
      4   # This module is used by `Credo.CLI.Output.UI` to write to the shell.
      5 
      6   use GenServer
      7 
      8   def start_link(opts \\ []) do
      9     {:ok, _pid} = GenServer.start_link(__MODULE__, opts, name: __MODULE__)
     10   end
     11 
     12   def puts do
     13     puts("")
     14   end
     15 
     16   @doc "Write the given `value` to `:stdout`."
     17   def puts(value) do
     18     GenServer.call(__MODULE__, {:puts, value})
     19   end
     20 
     21   def use_colors(use_colors) do
     22     GenServer.call(__MODULE__, {:use_colors, use_colors})
     23   end
     24 
     25   def suppress_output(callback_fn) do
     26     GenServer.call(__MODULE__, {:suppress_output, true})
     27     callback_fn.()
     28     GenServer.call(__MODULE__, {:suppress_output, false})
     29   end
     30 
     31   @doc "Like `puts/1`, but writes to `:stderr`."
     32   def warn(value) do
     33     GenServer.call(__MODULE__, {:warn, value})
     34   end
     35 
     36   # callbacks
     37 
     38   def init(_) do
     39     {:ok, %{use_colors: true, suppress_output: false}}
     40   end
     41 
     42   def handle_call({:suppress_output, suppress_output}, _from, current_state) do
     43     new_state = Map.put(current_state, :suppress_output, suppress_output)
     44 
     45     {:reply, nil, new_state}
     46   end
     47 
     48   def handle_call({:use_colors, use_colors}, _from, current_state) do
     49     new_state = Map.put(current_state, :use_colors, use_colors)
     50 
     51     {:reply, nil, new_state}
     52   end
     53 
     54   def handle_call({:puts, _value}, _from, %{suppress_output: true} = current_state) do
     55     {:reply, nil, current_state}
     56   end
     57 
     58   def handle_call(
     59         {:puts, value},
     60         _from,
     61         %{use_colors: true, suppress_output: false} = current_state
     62       ) do
     63     do_puts(value)
     64 
     65     {:reply, nil, current_state}
     66   end
     67 
     68   def handle_call(
     69         {:puts, value},
     70         _from,
     71         %{use_colors: false, suppress_output: false} = current_state
     72       ) do
     73     value
     74     |> remove_colors()
     75     |> do_puts()
     76 
     77     {:reply, nil, current_state}
     78   end
     79 
     80   def handle_call({:warn, _value}, _from, %{suppress_output: true} = current_state) do
     81     {:reply, nil, current_state}
     82   end
     83 
     84   def handle_call(
     85         {:warn, value},
     86         _from,
     87         %{use_colors: true, suppress_output: false} = current_state
     88       ) do
     89     do_warn(value)
     90 
     91     {:reply, nil, current_state}
     92   end
     93 
     94   def handle_call(
     95         {:warn, value},
     96         _from,
     97         %{use_colors: false, suppress_output: false} = current_state
     98       ) do
     99     value
    100     |> remove_colors()
    101     |> do_warn()
    102 
    103     {:reply, nil, current_state}
    104   end
    105 
    106   defp remove_colors(value) do
    107     value
    108     |> List.wrap()
    109     |> List.flatten()
    110     |> Enum.reject(&is_atom/1)
    111   end
    112 
    113   defp do_puts(value) do
    114     Bunt.puts(value)
    115   end
    116 
    117   defp do_warn(value) do
    118     Bunt.warn(value)
    119   end
    120 end