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, 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

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

    Output stream



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clack/task_log.rb', line 19

def initialize(title:, limit: nil, retain_log: false, output: $stdout)
  @title = title
  @limit = limit
  @retain_log = retain_log
  @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.



73
74
75
76
77
78
# File 'lib/clack/task_log.rb', line 73

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



64
65
66
67
68
69
# File 'lib/clack/task_log.rb', line 64

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



45
46
47
48
49
# File 'lib/clack/task_log.rb', line 45

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



35
36
37
38
39
40
# File 'lib/clack/task_log.rb', line 35

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



54
55
56
57
58
59
# File 'lib/clack/task_log.rb', line 54

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