Module: Gemview::Terminal

Defined in:
lib/gemview/terminal.rb

Class Method Summary collapse

Class Method Details

.choose(message:, choices:, per_page: 6) {|String| ... } ⇒ Object

Parameters:

  • prompt (String)
  • choices (Array<String, Hash>, Proc)

    where all choices are unique

Yields:

  • (String)

    yields until the user exits the prompt gracefully



32
33
34
35
36
37
38
# File 'lib/gemview/terminal.rb', line 32

def self.choose(message:, choices:, per_page: 6)
  loop do
    choice_list = choices.is_a?(Proc) ? choices.call : choices
    choice = selector.select(message, choice_list, per_page)
    choice ? yield(choice) : break
  end
end

.clear_screenObject

Clears the screen using escape codes.



6
7
8
# File 'lib/gemview/terminal.rb', line 6

def self.clear_screen
  print "\e[2J\e[f"
end

.confirm(question:) ⇒ Boolean

Parameters:

  • question (String)

Returns:

  • (Boolean)


12
13
14
# File 'lib/gemview/terminal.rb', line 12

def self.confirm(question:)
  TTY::Prompt.new.yes?(question)
end

.page(content) ⇒ Object

Parameters:

  • content (String)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gemview/terminal.rb', line 17

def self.page(content)
  command = [
    "less",
    "--clear-screen", # make sure everything is top-justified
    "--RAW-CONTROL-CHARS", # correctly interpret ANSI control sequences
    "--tilde", # don't show tildes on lines after the output
    "--prompt='(press h for help or q to quit)'"
  ].join(" ")

  TTY::Pager::SystemPager.new(command: command).page(content)
end

.prettify_markdown(text) ⇒ String

A best effort attempt to format and highlight markdown text. If it’s unsuccessful, it will return the original text.

Parameters:

  • text (String)

Returns:

  • (String)


48
49
50
51
52
# File 'lib/gemview/terminal.rb', line 48

def self.prettify_markdown(text)
  TTY::Markdown.parse(text, color: TTY_COLOR)
rescue # Return the raw markdown if parsing fails
  text
end