Class: Odysseus::CLI::UI
- Inherits:
-
Object
- Object
- Odysseus::CLI::UI
- Defined in:
- lib/odysseus/cli/ui.rb
Constant Summary collapse
- SPINNER_FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze
- COPPER =
ANSI color helpers
"\e[38;2;255;183;123m".freeze
- MINT =
"\e[38;2;112;216;200m".freeze
- RED =
"\e[38;2;255;100;100m".freeze
- DIM =
"\e[2m".freeze
- RESET =
"\e[0m".freeze
Instance Method Summary collapse
- #blank(io: $stdout) ⇒ Object
-
#build_logger ⇒ Object
--- Logger adapter ---.
- #debug? ⇒ Boolean
- #deploy_complete(duration: nil) ⇒ Object
-
#deploy_header(service:, image:, image_tag:, build: false, distribution: nil) ⇒ Object
--- Deploy-specific helpers ---.
- #detail(message) ⇒ Object
-
#error(message, io: $stdout) ⇒ Object
iois for the commands whose stdout is data rather than chatter:odysseus logs web1 > app.logcaptures a log stream, and a notice about which container the logs came from does not belong in it. -
#header(title, io: $stdout) ⇒ Object
--- Header ---.
- #info(label, value, io: $stdout) ⇒ Object
-
#initialize(debug: false) ⇒ UI
constructor
A new instance of UI.
- #next_step! ⇒ Object
- #reset_steps! ⇒ Object
-
#section(title) ⇒ Object
--- Section divider ---.
-
#spin_step(message) ⇒ Object
--- Single spin step --- Shows spinner while block runs, resolves to ✓/✗.
- #step(message, io: $stdout) ⇒ Object
- #step_fail(message) ⇒ Object
- #step_info(message) ⇒ Object
-
#step_ok(message) ⇒ Object
--- Immediate steps (no async) ---.
-
#stream_steps(title: nil) ⇒ Object
--- Streaming steps --- Runs a block, captures its stdout line by line, and renders each meaningful line as a sub-step with spinner → ✓.
-
#success(message) ⇒ Object
--- Simple output (no numbering) ---.
-
#table(headers:, rows:) ⇒ Object
--- Tables ---.
- #warn(message, io: $stdout) ⇒ Object
Constructor Details
#initialize(debug: false) ⇒ UI
Returns a new instance of UI.
53 54 55 56 |
# File 'lib/odysseus/cli/ui.rb', line 53 def initialize(debug: false) @debug = debug @step_number = 0 end |
Instance Method Details
#blank(io: $stdout) ⇒ Object
95 96 97 |
# File 'lib/odysseus/cli/ui.rb', line 95 def blank(io: $stdout) io.puts '' end |
#build_logger ⇒ Object
--- Logger adapter ---
370 371 372 373 374 375 376 377 378 379 |
# File 'lib/odysseus/cli/ui.rb', line 370 def build_logger ui = self Object.new.tap do |l| l.define_singleton_method(:info) { |msg| ui.step(msg) } l.define_singleton_method(:warn) { |msg| ui.warn(msg) } l.define_singleton_method(:error) { |msg| ui.error(msg) } l.define_singleton_method(:debug) { |msg| ui.detail(msg) } l.define_singleton_method(:verbose?) { ui.debug? } end end |
#debug? ⇒ Boolean
58 59 60 |
# File 'lib/odysseus/cli/ui.rb', line 58 def debug? @debug end |
#deploy_complete(duration: nil) ⇒ Object
362 363 364 365 366 |
# File 'lib/odysseus/cli/ui.rb', line 362 def deploy_complete(duration: nil) msg = 'Deployment successful' msg += " in #{duration}s" if duration step_ok msg end |
#deploy_header(service:, image:, image_tag:, build: false, distribution: nil) ⇒ Object
--- Deploy-specific helpers ---
354 355 356 357 358 359 360 |
# File 'lib/odysseus/cli/ui.rb', line 354 def deploy_header(service:, image:, image_tag:, build: false, distribution: nil) header 'Odysseus Deploy' info 'Service', service info 'Image', "#{image}:#{image_tag}" info 'Distribute', distribution if build && distribution blank end |
#detail(message) ⇒ Object
319 320 321 |
# File 'lib/odysseus/cli/ui.rb', line 319 def detail() puts " #{DIM}#{redact()}#{RESET}" if debug? end |
#error(message, io: $stdout) ⇒ Object
io is for the commands whose stdout is data rather than chatter:
odysseus logs web1 > app.log captures a log stream, and a notice about
which container the logs came from does not belong in it. A default
rather than a second set of methods, so every other caller keeps writing
to stdout exactly as before.
303 304 305 |
# File 'lib/odysseus/cli/ui.rb', line 303 def error(, io: $stdout) io.puts " #{RED}✗#{RESET} #{}" end |
#header(title, io: $stdout) ⇒ Object
--- Header ---
These three take io for the same reason error/warn/step below do: the
interactive sessions print a header describing a terminal whose own
output is stdout, so the description has to go somewhere else. The
default keeps every other caller writing exactly where it did.
77 78 79 80 81 82 83 84 85 |
# File 'lib/odysseus/cli/ui.rb', line 77 def header(title, io: $stdout) reset_steps! if debug? io.puts "\e[36m#{title}\e[0m" else io.puts '' io.puts " #{COPPER}#{title}#{RESET}" end end |
#info(label, value, io: $stdout) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/odysseus/cli/ui.rb', line 87 def info(label, value, io: $stdout) if debug? io.puts " #{label}: #{value}" else io.puts " #{DIM}#{label}:#{RESET} #{value}" end end |
#next_step! ⇒ Object
66 67 68 |
# File 'lib/odysseus/cli/ui.rb', line 66 def next_step! @step_number += 1 end |
#reset_steps! ⇒ Object
62 63 64 |
# File 'lib/odysseus/cli/ui.rb', line 62 def reset_steps! @step_number = 0 end |
#section(title) ⇒ Object
--- Section divider ---
344 345 346 347 348 349 350 |
# File 'lib/odysseus/cli/ui.rb', line 344 def section(title) if debug? puts "\e[36m=== #{title} ===\e[0m" else puts " #{COPPER}▸ #{title}#{RESET}" end end |
#spin_step(message) ⇒ Object
--- Single spin step --- Shows spinner while block runs, resolves to ✓/✗. Captures stdout from the block so it doesn't leak.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/odysseus/cli/ui.rb', line 103 def spin_step() next_step! num = step_num_str if debug? puts " #{num} #{redact()}" result = yield puts " #{num} ✓ #{redact()}" return result end result = nil err = nil done = false # Capture stdout from the block old_stdout = $stdout rd, wr = IO.pipe $stdout = wr thread = Thread.new do result = yield rescue StandardError => e err = e ensure done = true wr.close end frame_idx = 0 loop do break if done frame = SPINNER_FRAMES[frame_idx % SPINNER_FRAMES.size] old_stdout.print "\r #{DIM}#{num}#{RESET} #{COPPER}#{frame}#{RESET} #{}" old_stdout.flush frame_idx += 1 sleep 0.08 end rd.close $stdout = old_stdout thread.join print "\r\e[K" if err puts " #{DIM}#{num}#{RESET} #{RED}✗#{RESET} #{}" raise err else puts " #{DIM}#{num}#{RESET} #{MINT}✓#{RESET} #{}" end result end |
#step(message, io: $stdout) ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/odysseus/cli/ui.rb', line 311 def step(, io: $stdout) if debug? io.puts " #{redact()}" else io.puts " #{DIM}›#{RESET} #{}" end end |
#step_fail(message) ⇒ Object
282 283 284 285 |
# File 'lib/odysseus/cli/ui.rb', line 282 def step_fail() next_step! puts " #{DIM}#{step_num_str}#{RESET} #{RED}✗#{RESET} #{}" end |
#step_info(message) ⇒ Object
287 288 289 290 |
# File 'lib/odysseus/cli/ui.rb', line 287 def step_info() next_step! puts " #{DIM}#{step_num_str}#{RESET} #{COPPER}➜#{RESET} #{}" end |
#step_ok(message) ⇒ Object
--- Immediate steps (no async) ---
277 278 279 280 |
# File 'lib/odysseus/cli/ui.rb', line 277 def step_ok() next_step! puts " #{DIM}#{step_num_str}#{RESET} #{MINT}✓#{RESET} #{}" end |
#stream_steps(title: nil) ⇒ Object
--- Streaming steps --- Runs a block, captures its stdout line by line, and renders each meaningful line as a sub-step with spinner → ✓. All sub-steps share the same step number.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/odysseus/cli/ui.rb', line 164 def stream_steps(title: nil) next_step! num = step_num_str if debug? puts " #{num} > #{title}" if title # In debug mode, let output flow but redact sensitive values old_stdout = $stdout $stdout = RedactingIO.new(old_stdout, method(:redact)) begin yield ensure $stdout = old_stdout end return end # Show section header if provided puts " #{DIM}#{num}#{RESET} #{COPPER}>#{RESET} #{title}" if title result = nil err = nil done = false current_line = nil # Capture stdout old_stdout = $stdout rd, wr = IO.pipe thread = Thread.new do $stdout = wr begin result = yield rescue StandardError => e err = e ensure done = true wr.close end end frame_idx = 0 buf = '' loop do # Non-blocking read from pipe begin chunk = rd.read_nonblock(4096) buf << chunk rescue IO::WaitReadable # No data available yet rescue EOFError break end # Process complete lines while (nl = buf.index("\n")) line = buf.slice!(0..nl).strip next if line.empty? line = clean_line(line) next unless line # Note lines (prefixed with ~) render as indented grey text, no spinner if line.start_with?('~') if current_line old_stdout.print "\r\e[K" old_stdout.puts " #{DIM}#{num}#{RESET} #{MINT}✓#{RESET} #{current_line}" current_line = nil end old_stdout.puts " #{DIM}#{num}#{RESET} #{DIM}#{line[1..]}#{RESET}" next end # Resolve previous sub-step if current_line old_stdout.print "\r\e[K" old_stdout.puts " #{DIM}#{num}#{RESET} #{MINT}✓#{RESET} #{current_line}" end current_line = line end # Animate spinner on current line if current_line frame = SPINNER_FRAMES[frame_idx % SPINNER_FRAMES.size] old_stdout.print "\r #{DIM}#{num}#{RESET} #{COPPER}#{frame}#{RESET} #{current_line}" old_stdout.flush end frame_idx += 1 sleep 0.06 break if done && buf.empty? end rd.close $stdout = old_stdout thread.join # Resolve the final sub-step if current_line print "\r\e[K" puts " #{DIM}#{num}#{RESET} #{MINT}✓#{RESET} #{current_line}" end raise err if err result end |
#success(message) ⇒ Object
--- Simple output (no numbering) ---
294 295 296 |
# File 'lib/odysseus/cli/ui.rb', line 294 def success() puts " #{MINT}✓#{RESET} #{}" end |
#table(headers:, rows:) ⇒ Object
--- Tables ---
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/odysseus/cli/ui.rb', line 325 def table(headers:, rows:) return if rows.empty? widths = headers.map.with_index do |h, i| [h.to_s.length, rows.map { |r| r[i].to_s.length }.max || 0].max end header_line = headers.map.with_index { |h, i| h.to_s.ljust(widths[i]) }.join(' ') puts " #{COPPER}#{header_line}#{RESET}" puts " #{widths.map { |w| '─' * w }.join(' ')}" rows.each do |row| line = row.map.with_index { |c, i| c.to_s.ljust(widths[i]) }.join(' ') puts " #{line}" end end |
#warn(message, io: $stdout) ⇒ Object
307 308 309 |
# File 'lib/odysseus/cli/ui.rb', line 307 def warn(, io: $stdout) io.puts " \e[33m!#{RESET} #{}" end |