class MisterBin::Terminal

Attributes

options[R]
runner[R]

Public Class Methods

new(runner, options=nil) click to toggle source
# File lib/mister_bin/terminal.rb, line 11
def initialize(runner, options=nil)
  @runner = runner
  @options = options || {}
end

Public Instance Methods

on(command, &block) click to toggle source
# File lib/mister_bin/terminal.rb, line 16
def on(command, &block)
  reserved_commands[command] = block
end
start() click to toggle source
# File lib/mister_bin/terminal.rb, line 20
def start
  Readline.completion_append_character = " "
  Readline.completion_proc = autocomplete_handler if autocomplete

  welcome_messages
  loop { break unless safe_input_loop }
end

Private Instance Methods

autocomplete() click to toggle source
# File lib/mister_bin/terminal.rb, line 30
def autocomplete
  @autocomplete ||= options[:autocomplete]&.sort
end
autocomplete_handler() click to toggle source
# File lib/mister_bin/terminal.rb, line 34
def autocomplete_handler
  @autocomplete_handler ||= proc do |s|
    # :nocov:
    autocomplete.grep(/#{Regexp.escape(s)}/)
    # :nocov:
  end
end
disable_system_shell() click to toggle source
# File lib/mister_bin/terminal.rb, line 42
def disable_system_shell
  options[:disable_system_shell]
end
execute(input) click to toggle source
# File lib/mister_bin/terminal.rb, line 46
def execute(input)
  if exit_commands.include? input
    say exit_message if exit_message
    false
  else
    execute_command input
    true
  end
end
execute_command(input) click to toggle source
# File lib/mister_bin/terminal.rb, line 56
def execute_command(input)
  command = Shellwords.shellwords input

  if reserved_commands.include? command.first
    reserved_commands[command.first].call command[1..-1]
  elsif !disable_system_shell and command.first&.start_with? system_character
    system input[1..-1]
  else
    runner.run command
  end
end
exit_commands() click to toggle source
# File lib/mister_bin/terminal.rb, line 72
def exit_commands
  @exit_commands ||= options[:exit_commands] || ['exit', 'q']
end
exit_message() click to toggle source
# File lib/mister_bin/terminal.rb, line 68
def exit_message
  @exit_message ||= options[:exit_message]
end
header() click to toggle source
# File lib/mister_bin/terminal.rb, line 76
def header
  @header ||= options[:header]
end
input_loop() click to toggle source
# File lib/mister_bin/terminal.rb, line 80
def input_loop
  while input = Readline.readline(prompt, true) do
    break unless execute input
  end
end
prompt() click to toggle source
# File lib/mister_bin/terminal.rb, line 86
def prompt
  @prompt ||= options[:prompt] || "\n> "
end
reserved_commands() click to toggle source
# File lib/mister_bin/terminal.rb, line 90
def reserved_commands
  @reserved_commands ||= {}
end
safe_input_loop() click to toggle source
# File lib/mister_bin/terminal.rb, line 94
def safe_input_loop
  input_loop
# :nocov:
rescue Interrupt
  say exit_message if exit_message
  false
rescue => e
  puts e.backtrace.reverse if ENV['DEBUG']
  say "!txtred!#{e.class}"
  say e.message
  true
# :nocov:
end
show_usage() click to toggle source
# File lib/mister_bin/terminal.rb, line 108
def show_usage
  options[:show_usage]
end
system_character() click to toggle source
# File lib/mister_bin/terminal.rb, line 112
def system_character
  @system_character ||= options[:system_character] || '/'
end
welcome_messages() click to toggle source
# File lib/mister_bin/terminal.rb, line 116
def welcome_messages
  say header if header
  runner.run if show_usage
end