Module: Gemview::Terminal
- Defined in:
- lib/gemview/terminal.rb
Class Method Summary collapse
- .choose(message:, choices:, per_page: 6) {|String| ... } ⇒ Object
-
.clear_screen ⇒ Object
Clears the screen using escape codes.
- .confirm(question:) ⇒ Boolean
- .page(content) ⇒ Object
-
.prettify_markdown(text) ⇒ String
A best effort attempt to format and highlight markdown text.
Class Method Details
.choose(message:, choices:, per_page: 6) {|String| ... } ⇒ Object
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(, choice_list, per_page) choice ? yield(choice) : break end end |
.clear_screen ⇒ Object
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
12 13 14 |
# File 'lib/gemview/terminal.rb', line 12 def self.confirm(question:) TTY::Prompt.new.yes?(question) end |
.page(content) ⇒ Object
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.
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 |