Module: SnippetCli::UI

Defined in:
lib/snippet_cli/ui.rb

Constant Summary collapse

BASE_FLAGS =
['--border=rounded', '--padding=0 4'].freeze
PROMPT_STYLE =
{ padding: '0 1', margin: '0' }.freeze
STYLE_FLAGS =
{
  info: [],
  hint: ['--border-foreground=220'],
  success: ['--border-foreground=46', '--bold'],
  warning: ['--border-foreground=220', '--foreground=220', '--bold'],
  error: ['--border-foreground=196', '--foreground=196', '--bold'],
  preview: []
}.freeze

Class Method Summary collapse

Class Method Details

.deliver(yaml, label:, context: nil) ⇒ Object

Delivers YAML output: pipes to stdout if piped, or displays with a label if interactive. context must respond to #pipe_output (nil = interactive mode).



71
72
73
74
75
76
77
78
79
# File 'lib/snippet_cli/ui.rb', line 71

def self.deliver(yaml, label:, context: nil)
  pipe = context&.pipe_output
  if pipe
    pipe.print yaml
  else
    info("#{label} YAML below.")
    format_code(yaml)
  end
end

.error(text) ⇒ Object



28
# File 'lib/snippet_cli/ui.rb', line 28

def self.error(text)   = gum_style(text, *STYLE_FLAGS[:error])

.format_code(text, language: 'yaml') ⇒ Object



61
62
63
64
65
66
67
# File 'lib/snippet_cli/ui.rb', line 61

def self.format_code(text, language: 'yaml')
  Gum::Command.run_display_only('format', '--type=code', "--language=#{language}", input: text)
  puts
rescue Gum::Error
  puts text
  puts
end

.hint(text) ⇒ Object



25
# File 'lib/snippet_cli/ui.rb', line 25

def self.hint(text)    = gum_style(text, *STYLE_FLAGS[:hint])

.info(text) ⇒ Object



24
# File 'lib/snippet_cli/ui.rb', line 24

def self.info(text)    = gum_style(text, *STYLE_FLAGS[:info])

.note(text) ⇒ Object



20
21
22
# File 'lib/snippet_cli/ui.rb', line 20

def self.note(text)
  puts "\e[38;5;231m#{text}\e[0m"
end

.preview(text) ⇒ Object



29
# File 'lib/snippet_cli/ui.rb', line 29

def self.preview(text) = gum_style(text, *STYLE_FLAGS[:preview])

.success(text) ⇒ Object



26
# File 'lib/snippet_cli/ui.rb', line 26

def self.success(text) = gum_style(text, *STYLE_FLAGS[:success])

.transient_error(text) ⇒ Object



44
45
46
47
# File 'lib/snippet_cli/ui.rb', line 44

def self.transient_error(text)
  error(text)
  erase_lambda(text.lines.count + 2)
end

.transient_info(text) ⇒ Object

Renders an info box and returns a lambda that erases it via line-count tracking. The info box is always rendered; the clear lambda is a no-op when not a TTY.



51
52
53
54
# File 'lib/snippet_cli/ui.rb', line 51

def self.transient_info(text)
  info(text)
  erase_lambda(text.lines.count + 2)
end

.transient_note(text) ⇒ Object

Renders a warning and returns a lambda that erases it via line-count tracking. The warning is always rendered; the clear lambda is a no-op when not a TTY.



33
34
35
36
37
# File 'lib/snippet_cli/ui.rb', line 33

def self.transient_note(text)
  puts "\e[38;5;231m #{text}\e[0m"
  puts
  erase_lambda(2)
end

.transient_warning(text) ⇒ Object



39
40
41
42
# File 'lib/snippet_cli/ui.rb', line 39

def self.transient_warning(text)
  warning(text)
  erase_lambda(text.lines.count + 2)
end

.warning(text) ⇒ Object



27
# File 'lib/snippet_cli/ui.rb', line 27

def self.warning(text) = gum_style(text, *STYLE_FLAGS[:warning])