Class: Kdeploy::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/output_formatter.rb

Overview

Formats and displays execution results

Instance Method Summary collapse

Constructor Details

#initialize(debug: false) ⇒ OutputFormatter

Returns a new instance of OutputFormatter.



9
10
11
12
# File 'lib/kdeploy/output_formatter.rb', line 9

def initialize(debug: false)
  @pastel = Pastel.new
  @debug = debug
end

Instance Method Details

#build_summary_line(host, counts, max_host_len) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/kdeploy/output_formatter.rb', line 115

def build_summary_line(host, counts, max_host_len)
  ok_w = 7
  changed_w = 11
  failed_w = 10

  ok_str = @pastel.green("ok=#{counts[:ok].to_s.ljust(ok_w - 3)}")
  changed_str = @pastel.yellow("changed=#{counts[:changed].to_s.ljust(changed_w - 8)}")
  failed_str = @pastel.red("failed=#{counts[:failed].to_s.ljust(failed_w - 7)}")
  "#{host.ljust(max_host_len)} : #{ok_str}  #{changed_str}  #{failed_str}"
end

#calculate_summary_counts(result) ⇒ Object



108
109
110
111
112
113
# File 'lib/kdeploy/output_formatter.rb', line 108

def calculate_summary_counts(result)
  ok = i[success changed].include?(result[:status]) ? result[:output].size : 0
  failed = result[:status] == :failed ? 1 : 0
  changed = result[:status] == :changed ? result[:output].size : 0
  { ok: ok, failed: failed, changed: changed }
end

#colorize_summary_line(line, counts) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/kdeploy/output_formatter.rb', line 126

def colorize_summary_line(line, counts)
  if counts[:failed].positive?
    @pastel.red(line)
  elsif counts[:ok].positive? && counts[:failed].zero?
    @pastel.green(line)
  else
    line
  end
end

#format_command_for_dry_run(command) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kdeploy/output_formatter.rb', line 160

def format_command_for_dry_run(command)
  case command[:type]
  when :run
    "#{@pastel.green('>')} #{command[:command]}"
  when :upload
    "#{@pastel.blue('>')} Upload: #{command[:source]} -> #{command[:destination]}"
  when :upload_template
    "#{@pastel.blue('>')} Template: #{command[:source]} -> #{command[:destination]}"
  when :sync
    ignore_str = command[:ignore]&.any? ? " (ignore: #{command[:ignore].join(', ')})" : ''
    delete_str = command[:delete] ? ' (delete: true)' : ''
    "#{@pastel.blue('>')} Sync: #{command[:source]} -> #{command[:destination]}#{ignore_str}#{delete_str}"
  else
    "#{@pastel.blue('>')} #{command[:type]}: #{command}"
  end
end

#format_dry_run_box(title, content) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kdeploy/output_formatter.rb', line 136

def format_dry_run_box(title, content)
  TTY::Box.frame(
    content,
    title: { top_left: " #{title} " },
    style: {
      border: {
        fg: :yellow
      }
    }
  )
end

#format_dry_run_headerObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kdeploy/output_formatter.rb', line 148

def format_dry_run_header
  TTY::Box.frame(
    'Showing what would be done without executing any commands',
    title: { top_left: ' Dry Run Mode ', bottom_right: ' Kdeploy ' },
    style: {
      border: {
        fg: :blue
      }
    }
  )
end

#format_error(error_message) ⇒ Object



94
95
96
# File 'lib/kdeploy/output_formatter.rb', line 94

def format_error(error_message)
  @pastel.red("    ✗ ERROR: #{error_message}")
end

#format_file_step(step, type, prefix) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/kdeploy/output_formatter.rb', line 57

def format_file_step(step, type, prefix)
  duration_str = format_duration(step[:duration])
  icon = type == :upload ? '📤' : '📝'
  file_path = step[:command].sub(prefix, '')
  # Truncate long paths for cleaner output
  display_path = file_path.length > 50 ? "...#{file_path[-47..]}" : file_path
  color_method = type == :upload ? :green : :yellow
  @pastel.dim("    #{icon} ") + @pastel.send(color_method, display_path) + duration_str
end

#format_file_steps(steps, shown, type, _header, prefix) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/kdeploy/output_formatter.rb', line 46

def format_file_steps(steps, shown, type, _header, prefix)
  output = []
  steps.each do |step|
    next if step_already_shown?(step, type, shown)

    mark_step_as_shown(step, type, shown)
    output << format_file_step(step, type, prefix)
  end
  output
end

#format_host_status(host, status) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/kdeploy/output_formatter.rb', line 18

def format_host_status(host, status)
  status_str = case status
               when :success then @pastel.green('✓ ok')
               when :changed then @pastel.yellow('~ changed')
               else @pastel.red('✗ failed')
               end
  @pastel.bright_white("  #{host.ljust(20)} #{status_str}")
end

#format_run_steps(steps, shown) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/kdeploy/output_formatter.rb', line 67

def format_run_steps(steps, shown)
  output = []
  steps.each do |step|
    next if step_already_shown?(step, :run, shown)

    mark_step_as_shown(step, :run, shown)
    output.concat(format_single_run_step(step))
  end
  output
end

#format_single_run_step(step) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kdeploy/output_formatter.rb', line 78

def format_single_run_step(step)
  output = []
  duration_str = format_duration(step[:duration])
  command_line = step[:command].to_s.lines.first.strip
  # Truncate long commands for cleaner output
  display_cmd = command_line.length > 60 ? "#{command_line[0..57]}..." : command_line
  output << (@pastel.dim('    • ') + @pastel.cyan(display_cmd) + duration_str)
  # Only show multiline details in debug mode
  if @debug
    output.concat(format_multiline_command(step[:command]))
    cmd_output = format_command_output(step[:output])
    output.concat(cmd_output) if cmd_output.any?
  end
  output
end

#format_summary_headerObject



98
99
100
# File 'lib/kdeploy/output_formatter.rb', line 98

def format_summary_header
  "#{@pastel.bright_cyan("\n📊 Execution Summary")}\n#{@pastel.dim('─' * 60)}"
end

#format_summary_line(host, result, max_host_len) ⇒ Object



102
103
104
105
106
# File 'lib/kdeploy/output_formatter.rb', line 102

def format_summary_line(host, result, max_host_len)
  counts = calculate_summary_counts(result)
  line = build_summary_line(host, counts, max_host_len)
  colorize_summary_line(line, counts)
end

#format_sync_steps(steps, shown) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/kdeploy/output_formatter.rb', line 35

def format_sync_steps(steps, shown)
  output = []
  steps.each do |step|
    next if step_already_shown?(step, :sync, shown)

    mark_step_as_shown(step, :sync, shown)
    output << format_sync_step(step)
  end
  output
end

#format_task_header(task_name) ⇒ Object



14
15
16
# File 'lib/kdeploy/output_formatter.rb', line 14

def format_task_header(task_name)
  "#{@pastel.bright_cyan("\n🚀 Task: #{task_name}")}\n#{@pastel.dim('─' * 60)}"
end

#format_template_steps(steps, shown) ⇒ Object



31
32
33
# File 'lib/kdeploy/output_formatter.rb', line 31

def format_template_steps(steps, shown)
  format_file_steps(steps, shown, :upload_template, @pastel.yellow('  === Template ==='), 'upload_template: ')
end

#format_upload_steps(steps, shown) ⇒ Object



27
28
29
# File 'lib/kdeploy/output_formatter.rb', line 27

def format_upload_steps(steps, shown)
  format_file_steps(steps, shown, :upload, @pastel.green('  === Upload ==='), 'upload: ')
end