From 2abca7e221ffb574aac10188b1e47970cb8c7b23 Mon Sep 17 00:00:00 2001 From: Suraj Borate Date: Tue, 18 Feb 2025 19:52:21 +0530 Subject: [PATCH 1/2] optional-ringlogger-launch-cmd-argument --- lib/ring_logger.ex | 9 ++++++++- lib/ring_logger/viewer.ex | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/ring_logger.ex b/lib/ring_logger.ex index 7c8a148..772eae0 100644 --- a/lib/ring_logger.ex +++ b/lib/ring_logger.ex @@ -57,6 +57,7 @@ defmodule RingLogger do alias RingLogger.Autoclient alias RingLogger.Server + alias RingLogger.Viewer @typedoc "Option values used by the ring logger" @type server_option() :: @@ -193,11 +194,17 @@ defmodule RingLogger do @spec tail(non_neg_integer(), client_options()) :: :ok def tail(n, opts), do: Autoclient.tail(n, opts) + @doc """ + Starts the Ring Logger Viewer TUI app on the current prompt with intial arguments + """ + @spec viewer(String.t()) :: :ok + defdelegate viewer(cmd_string), to: Viewer, as: :view + @doc """ Starts the Ring Logger Viewer TUI app on the current prompt """ @spec viewer() :: :ok - def viewer(), do: RingLogger.Viewer.view() + defdelegate viewer(), to: Viewer, as: :view @doc """ Reset the index into the log for `next/1` to the oldest entry. diff --git a/lib/ring_logger/viewer.ex b/lib/ring_logger/viewer.ex index 02f09c6..b2ce9d5 100644 --- a/lib/ring_logger/viewer.ex +++ b/lib/ring_logger/viewer.ex @@ -40,8 +40,8 @@ defmodule RingLogger.Viewer do @level_strings ["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"] - @spec view() :: :ok - def view() do + @spec view(String.t()) :: :ok + def view(cmd_string \\ "") do screen_dims = get_screen_dims() if screen_dims.w <= @min_width do @@ -52,9 +52,37 @@ defmodule RingLogger.Viewer do raise "Sorry, your terminal needs to be at least #{@min_height} rows high to use this tool!" end - IO.puts("Starting RingLogger Viewer...") + parse_launch_cmd(cmd_string, @init_state) |> get_log_snapshot() |> loop() + end + + @doc """ + updates state by applying multiple filters to initial state or return initial state + """ + def parse_launch_cmd("", state), do: state - @init_state |> get_log_snapshot() |> loop() + @spec parse_launch_cmd(String.t(), map()) :: map() + def parse_launch_cmd(cmd_string, state) do + cmd_list = String.split(cmd_string, ";") + + state = + Enum.reduce(cmd_list, state, fn cmd, state -> + cmd_char = String.trim_leading(cmd, " ") |> String.at(0) |> String.downcase() + apply_command_parser(cmd_char, cmd, state) + end) + + %{state | current_page: 0} + end + + # apply_command_parser/3 returns state by applying single filter + defp apply_command_parser(cmd_char, cmd, state) do + case {cmd_char, cmd, state} do + {"l", cmd, state} -> set_log_level(cmd, state) + {"a", cmd, state} -> add_remove_app(cmd, state) + {"r", _cmd, _state} -> %{@init_state | current_page: 0} + {"g", cmd, state} -> add_remove_grep(cmd, state) + {"q", _cmd, state} -> %{state | running: false} + _ -> state + end end #### Drawing and IO Functions From 7713fe1f98e8021162f0d7ccfe5923b34e18eec0 Mon Sep 17 00:00:00 2001 From: Suraj Borate Date: Tue, 20 May 2025 16:44:49 +0530 Subject: [PATCH 2/2] removed merge conflict --- lib/ring_logger/viewer.ex | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/lib/ring_logger/viewer.ex b/lib/ring_logger/viewer.ex index f2e3cd9..8683afa 100644 --- a/lib/ring_logger/viewer.ex +++ b/lib/ring_logger/viewer.ex @@ -41,7 +41,6 @@ defmodule RingLogger.Viewer do @level_strings ["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"] - @microsecond_factor 1_000_000 @seconds_in_year 365 * 24 * 60 * 60 @seconds_in_month 30 * 24 * 60 * 60 @@ -52,7 +51,6 @@ defmodule RingLogger.Viewer do @spec view(String.t()) :: :ok def view(cmd_string \\ "") do - screen_dims = get_screen_dims() if screen_dims.w <= @min_width do @@ -92,31 +90,6 @@ defmodule RingLogger.Viewer do {"r", _cmd, _state} -> %{@init_state | current_page: 0} {"g", cmd, state} -> add_remove_grep(cmd, state) {"q", _cmd, state} -> %{state | running: false} - _ -> state - end - end - - @doc """ - updates state by applying multiple filters to initial state or return initial state - """ - def parse_launch_cmd("", state), do: state - - @spec parse_launch_cmd(String.t(), map()) :: map() - def parse_launch_cmd(cmd_string, state) do - cmd_list = String.split(cmd_string, ";") - - state = - Enum.reduce(cmd_list, state, fn cmd, state -> - cmd_char = String.trim_leading(cmd, " ") |> String.at(0) |> String.downcase() - apply_command_parser(cmd_char, cmd, state) - end) - - %{state | current_page: 0} - end - - # apply_command_parser/3 returns state by applying single filter - defp apply_command_parser(cmd_char, cmd, state) do - case {cmd_char, cmd, state} do {"d", cmd, state} -> add_time_log(cmd, state) _ -> state end