Module: Strata::CLI::Terminal

Instance Method Summary collapse

Instance Method Details

#create_spinner(message, message_color: :cyan, spinner_color: :cyan, format: :dots, clear: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/strata/cli/terminal.rb', line 11

def create_spinner(message, message_color: :cyan, spinner_color: :cyan, format: :dots, clear: false)
  pastel = Pastel.new

  colored_message = pastel.send(message_color, message)
  colored_spinner = "[#{pastel.send(spinner_color, ":spinner")}]"

  TTY::Spinner.new("#{colored_spinner} #{colored_message}",
    success_mark: pastel.green(""),
    error_mark: pastel.red(""),
    format: format,
    clear: clear)
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/strata/cli/terminal.rb', line 50

def print_table(data, headers: nil, color: :magenta)
  if data.empty?
    say TTY::Table.new.render(:unicode), color
    return
  end

  terminal_width = begin
    IO.console.winsize[1]
  rescue
    80
  end

  actual_headers = headers || (0...data.first.length).map { |i| "Col#{i + 1}" }

  # Calculate actual width needed for each column
  column_widths = []
  actual_headers.each_with_index do |header, i|
    header_width = header.to_s.length
    data_width = data.map { |row| row[i].to_s.length }.max || 0
    column_widths << [header_width, data_width].max
  end

  # Table overhead: 2 for outer borders + 1 per column separator
  table_overhead = 2 + (column_widths.length - 1) * 1
  # Add 2 chars padding per column (space on each side)
  total_padding = column_widths.length * 2

  # Find how many columns fit
  fitted_cols = 0
  running_width = table_overhead + total_padding

  column_widths.each_with_index do |width, _i|
    break unless running_width + width <= terminal_width

    running_width += width
    fitted_cols += 1
  end

  # Ensure at least one column
  fitted_cols = [fitted_cols, 1].max

  # Create the table
  limited_headers = actual_headers.first(fitted_cols)
  limited_data = data.map { |row| row.first(fitted_cols) }

  table = TTY::Table.new(header: limited_headers, rows: limited_data)
  say table.render(:unicode, padding: [0, 1]), color
  return unless fitted_cols < actual_headers.length

  truncated = actual_headers.length - fitted_cols
  say "(Showing #{fitted_cols}/#{actual_headers.length} columns - #{truncated} truncated)", :cyan
end

#with_spinner(message = "Loading...", success_message: "", failed_message: "", clear: false, message_color: :cyan, spinner_color: :cyan, format: :dots) ⇒ Object

Shows a loader while running IO tasks Uses consistent format matching deployment monitor: cyan spinner that transitions to checkmark



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/strata/cli/terminal.rb', line 26

def with_spinner(message = "Loading...",
  success_message: "",
  failed_message: "",
  clear: false,
  message_color: :cyan,
  spinner_color: :cyan,
  format: :dots)
  spinner = create_spinner(message,
    message_color: message_color,
    spinner_color: spinner_color,
    format: format,
    clear: clear)
  spinner.auto_spin

  begin
    result = yield
    spinner.success(success_message.empty? ? "" : Pastel.new.green(success_message))
    result
  rescue => e
    spinner.error(failed_message.empty? ? "" : Pastel.new.red(failed_message))
    raise e
  end
end