Module: Aspera::Cli::TerminalFormatter
- Includes:
- FormatterInterface
- Defined in:
- lib/aspera/cli/terminal_formatter.rb
Overview
Terminal formatter with ANSI colors and Unicode support
Class Method Summary collapse
-
.check_row(row) ⇒ Object
Prepare table row for terminal display (word wrap arrays).
-
.hint ⇒ Object
Prefix for hint messages (evaluated on each call, as colors can be enabled or disabled by option).
-
.markdown_text(match) ⇒ Object
Convert Markdown to terminal format (bold -> blue,
code-> bold). -
.special_format(what) ⇒ Object
Format special values with colors (dim for empty, reverse for others).
-
.tick(yes) ⇒ Object
Format boolean with colored symbol (+/- or Y/ ).
Class Method Details
.check_row(row) ⇒ Object
Prepare table row for terminal display (word wrap arrays)
42 43 44 45 46 |
# File 'lib/aspera/cli/terminal_formatter.rb', line 42 def check_row(row) row.each_key do |k| row[k] = row[k].map { |i| WordWrap.ww(i.to_s, 120).chomp }.join("\n") if row[k].is_a?(Array) end end |
.hint ⇒ Object
Prefix for hint messages (evaluated on each call, as colors can be enabled or disabled by option)
19 20 21 |
# File 'lib/aspera/cli/terminal_formatter.rb', line 19 def hint 'HINT:'.bg(:green).white.blink end |
.markdown_text(match) ⇒ Object
Convert Markdown to terminal format (bold -> blue, code -> bold)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aspera/cli/terminal_formatter.rb', line 50 def markdown_text(match) if match.is_a?(String) match = Markdown::FORMATS.match(match) Aspera.assert(match, 'markdown text does not match any known format') end Aspera.assert_type(match, MatchData) if match[:entity] Aspera.assert_values(match[:entity], %w[bsol]) '\\' elsif match[:bold] match[:bold].to_s.blue elsif match[:code] match[:code].to_s.bold else Aspera.error_unexpected_value(match.to_s) end end |
.special_format(what) ⇒ Object
Format special values with colors (dim for empty, reverse for others)
36 37 38 39 |
# File 'lib/aspera/cli/terminal_formatter.rb', line 36 def special_format(what) result = "<#{what}>" return %w[null empty].any? { |s| what.include?(s) } ? result.faint : result.inverse end |
.tick(yes) ⇒ Object
Format boolean with colored symbol (+/- or Y/ )
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/aspera/cli/terminal_formatter.rb', line 24 def tick(yes) result = if Environment.terminal_supports_unicode? yes ? "\u2713" : "\u2717" else yes ? 'Y' : ' ' end return result.green if yes return result.red end |