Class: Brut::TUI::Script::PutsSubscriber
- Inherits:
-
Object
- Object
- Brut::TUI::Script::PutsSubscriber
- Defined in:
- lib/brut/tui/script/puts_subscriber.rb
Overview
A subscriber that attempts to provide a user-friend, brief, summarized, fancy output for the script that is running. It is intended to show you what's happening at a high level, but deferring all details (like child process output) to the LoggingSubscriber's log file.
Instance Method Summary collapse
-
#initialize(progname, terminal:, theme:, stdout: false, stderr: false) ⇒ PutsSubscriber
constructor
A new instance of PutsSubscriber.
-
#on_command_execution_failed(command:) ⇒ Object
-
#on_command_execution_succeeded(command:) ⇒ Object
-
#on_command_std_err(output:, command:) ⇒ Object
-
#on_command_std_out(output:, command:) ⇒ Object
-
#on_exception(exception:) ⇒ Object
-
#on_executing_command(command:) ⇒ Object
-
#on_message(message:, type:) ⇒ Object
-
#on_phase_started(description:, step_number:, total_steps:) ⇒ Object
-
#on_step_started(description:) ⇒ Object
Constructor Details
#initialize(progname, terminal:, theme:, stdout: false, stderr: false) ⇒ PutsSubscriber
Returns a new instance of PutsSubscriber.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 6 def initialize(progname, terminal:, theme:, stdout: false, stderr: false) @progname = progname @terminal = terminal @theme = theme @prefix_recent = false @stdout = stdout @stderr = stderr @stdout_buffer = {} @stderr_buffer = {} @step_indent = "Phase 1/1 ".length end |
Instance Method Details
#on_command_execution_failed(command:) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 40 def on_command_execution_failed(command:) println @theme.with_markup("FAILED", text: :error), step_indent: true if !@stdout println "" println @stdout_buffer[command] + "\n" end if !@stderr println "" println @theme.warning + @stderr_buffer[command] end end |
#on_command_execution_succeeded(command:) ⇒ Object
77 78 79 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 77 def on_command_execution_succeeded(command:) println @theme.with_markup("✅", text: :success), step_indent: true end |
#on_command_std_err(output:, command:) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 67 def on_command_std_err(output:, command:) if @stderr @prefix_recent = false $stdout.print @theme.warning + output + @theme.reset $stdout.flush else @stderr_buffer[command] << output end end |
#on_command_std_out(output:, command:) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 57 def on_command_std_out(output:, command:) if @stdout @prefix_recent = false $stdout.print @theme.normal + @theme.weak + output + @theme.reset $stdout.flush else @stdout_buffer[command] << output end end |
#on_exception(exception:) ⇒ Object
53 54 55 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 53 def on_exception(exception:) println @theme.error + "Exception: #{exception.class}: #{exception.}\n #{exception.backtrace.join("\n ")}" end |
#on_executing_command(command:) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 33 def on_executing_command(command:) println @theme.with_markup("> `#{command}`"), step_indent: true $stdout.print @theme.reset @stdout_buffer[command] = "" @stderr_buffer[command] = "" end |
#on_message(message:, type:) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 81 def (message:, type:) if type == :done println @theme.with_markup(, text: :success) else println @theme.with_markup(, text: type), step_indent: true end end |
#on_phase_started(description:, step_number:, total_steps:) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 18 def on_phase_started(description:, step_number:, total_steps:) total_format_string = if total_steps < 10 "%1d" else "%2d" end preamble = sprintf("Phase %d/#{total_format_string}", step_number, total_steps) @step_indent = preamble.length + 1 println @theme.reset + @theme.bold + preamble + @theme.reset + " " + @theme.with_markup(description, text: :heading) end |
#on_step_started(description:) ⇒ Object
29 30 31 |
# File 'lib/brut/tui/script/puts_subscriber.rb', line 29 def on_step_started(description:) println @theme.with_markup(description), step_indent: true end |