class TerminalMenu

Constants

VERSION

Public Class Methods

new(title: '', description: '', width: 80, fg: 'white', bg: 'black', stdin: STDIN, stdout: STDOUT, &selected_callback) click to toggle source

Public: Initializes new TerminalMenu instance.

block - Optional block that will get called with label once some option

is selected

Options:

title - The title of this menu (default: '')
description - The description of this menu (default: '')
width - The width of this menu in chars (default: 80)
fg - Foreground color of this menu (default: 'white')
bg - Background color of this menu (default: 'black')
stdin - Input IO instance (default: STDIN)
stdout - Output IO instance (default: STDOUT)
# File lib/terminal-menu.rb, line 19
def initialize(title: '', description: '', width: 80,
               fg: 'white', bg: 'black',
               stdin: STDIN, stdout: STDOUT, &selected_callback)
  @title = title
  @description = description
  @width = width
  @selected_callback = selected_callback || -> (arg) {}
  @fg = fg
  @bg = bg
  @stdin = stdin
  @stdout = stdout

  @selected_index = 0

  @options = [{label: 'exit', callback:  ->{}}]
  @last_char = []
end

Public Instance Methods

add(label, &blk) click to toggle source

Public: Adds a new item to menu options.

label - Label for this menu option. blk - Optional block that will be called once this option is selected.

# File lib/terminal-menu.rb, line 41
def add(label, &blk)
  # preserve exit as the last option
  last = @options.pop
  @options << {label: label, callback: blk || -> (arg) {}}
  @options << last
end
quit() click to toggle source

Public: Quits this menu.

# File lib/terminal-menu.rb, line 60
def quit
  @stdin.close
  @stdout.close
end
show() click to toggle source

Public: Starts read-eval-print loop.

# File lib/terminal-menu.rb, line 49
def show
  while true do
    clear_screen
    print_screen
    get_input
  end
rescue IOError
  # do nothing, program has ended
end

Private Instance Methods

clear_screen() click to toggle source
# File lib/terminal-menu.rb, line 146
def clear_screen
  @stdout.puts "\e[0m"
  @stdout.puts "\e[2J"
  @stdout.puts "\ec"
end
get_input() click to toggle source
# File lib/terminal-menu.rb, line 117
def get_input
  char = @stdin.getch

  @last_char << char # special characters (like up arrow) get sent
                     # as mulitple characters, so we have to store past
                     # few characters somewhere

  case
  when @last_char.join == "\e[A" || char == 'k' # up arrow or k
    go_up
    @last_char = []
  when @last_char.join == "\e[B" || char == 'j' # down arrow or j
    go_down
    @last_char = []
  when char == "\r" # enter
    if @selected_index == @options.length - 1
      quit
      return
    end

    @options[@selected_index][:callback].call(@options[@selected_index][:label])
    @selected_callback.call(@options[@selected_index][:label])
  when char == "\u0003" || char == "q" # ctrl+c or q
    quit
  when !@last_char.include?("\e") # not a special character, clear array
    @last_char = []
  end
end
go_down() click to toggle source
# File lib/terminal-menu.rb, line 67
def go_down
  if @selected_index == @options.length - 1
    @selected_index = 0
  else
    @selected_index += 1
  end
end
go_up() click to toggle source
# File lib/terminal-menu.rb, line 75
def go_up
  if @selected_index == 0
    @selected_index = @options.length - 1
  else
    @selected_index -= 1
  end
end
print(text, inverted: false) click to toggle source
print_delimiter() click to toggle source
print_header() click to toggle source
print_options() click to toggle source
print_screen() click to toggle source