Class: Clack::TaskLog

Inherits:
Object
  • Object
show all
Defined in:
lib/clack/task_log.rb

Overview

A streaming log that clears on success and remains on failure Useful for build output, npm install style streaming, etc.

Examples:

Basic usage

tl = Clack.task_log(title: "Building...")
tl.message("Compiling file 1...")
tl.message("Compiling file 2...")
tl.success("Build complete!")  # Clears the log
# or tl.error("Build failed!") # Keeps the log visible

Instance Method Summary collapse

Constructor Details

#initialize(title:, limit: nil, retain_log: false, with_guide: nil, output: $stdout) ⇒ TaskLog

Returns a new instance of TaskLog.

Parameters:

  • title (String)

    Title displayed at the top

  • limit (Integer, nil) (defaults to: nil)

    Max lines to show (older lines scroll out)

  • retain_log (Boolean) (defaults to: false)

    Keep full log history for display on error

  • with_guide (Boolean, nil) (defaults to: nil)

    Show the guide rail around the title and log lines (default: Clack.settings)

  • output (IO) (defaults to: $stdout)

    Output stream



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clack/task_log.rb', line 21

def initialize(title:, limit: nil, retain_log: false, with_guide: nil, output: $stdout)
  @title = title
  @limit = limit
  @retain_log = retain_log
  # Resolved once: the title is printed right here, and every later redraw
  # must clear the same number of lines, so the setting cannot change mid-log.
  @guide = Core::Settings.with_guide?(with_guide)
  @output = output
  @buffer = []
  @full_buffer = []
  @groups = []
  @lines_written = 0
  @tty = tty_output?(output)

  render_title
end

Instance Method Details

#add_group_message(_group, msg) ⇒ Object

Add a message from a group to the log buffer.



78
79
80
81
82
83
# File 'lib/clack/task_log.rb', line 78

def add_group_message(_group, msg)
  clear_buffer
  @buffer << msg.to_s
  apply_limit
  render_buffer if @tty
end

#error(msg, show_log: true) ⇒ Object

Complete with error - keeps the log visible

Parameters:

  • msg (String)

    Error message

  • show_log (Boolean) (defaults to: true)

    If false, hide the log



69
70
71
72
73
74
# File 'lib/clack/task_log.rb', line 69

def error(msg, show_log: true)
  clear_all
  @output.puts "#{Colors.red(Symbols::S_STEP_ERROR)}  #{msg}"
  render_full_buffer if show_log
  reset_buffers
end

#group(name) ⇒ TaskLogGroup

Create a named group for messages

Parameters:

  • name (String)

    Group header name

Returns:

  • (TaskLogGroup)

    Group object with message/success/error methods



50
51
52
53
54
# File 'lib/clack/task_log.rb', line 50

def group(name)
  grp = TaskLogGroup.new(name, self)
  @groups << grp
  grp
end

#message(msg) ⇒ Object

Add a message to the log

Parameters:

  • msg (String)

    Message to display



40
41
42
43
44
45
# File 'lib/clack/task_log.rb', line 40

def message(msg)
  clear_buffer
  @buffer << msg.to_s.gsub(/\e\[[\d;]*[ABCDEFGHfJKSTsu]/, "") # Strip cursor movement codes
  apply_limit
  render_buffer if @tty
end

#success(msg, show_log: false) ⇒ Object

Complete with success - clears the log

Parameters:

  • msg (String)

    Success message

  • show_log (Boolean) (defaults to: false)

    If true, show the log even on success



59
60
61
62
63
64
# File 'lib/clack/task_log.rb', line 59

def success(msg, show_log: false)
  clear_all
  @output.puts "#{Colors.green(Symbols::S_STEP_SUBMIT)}  #{msg}"
  render_full_buffer if show_log
  reset_buffers
end