class Termtter::CommandLine

Constants

STTY_ORIGIN
TIOCGWINSZ

Public Class Methods

start() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 9
def start
  instance.start
end
stop() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 13
def stop
  instance.stop
end

Public Instance Methods

call(command_text) click to toggle source
# File lib/plugins/defaults/command_line.rb, line 26
def call(command_text)
  # Example:
  # t.register_hook(:post_all, :point => :prepare_command) do |s|
  #   "update #{s}"
  # end
  Client.get_hooks('prepare_command').each {|hook|
    command_text = hook.call(command_text)
  }
  Client.execute(command_text)
rescue CommandNotFound => e
  hooks = Client.get_hooks('command_not_found')
  raise e if hooks.empty?
  hooks.each {|hook|
    hook.call(command_text)
  }
rescue Timeout::Error
  puts TermColor.parse("<red>Time out :(</red>")
end
call_prompt(command) click to toggle source
# File lib/plugins/another_prompt.rb, line 94
def call_prompt(command)
  Client.execute("curry #{command}")
  if buf = Readline.readline(ERB.new(prompt).result(Termtter::API.twitter.__send__(:binding)), true)
    Readline::HISTORY.pop if buf.empty?
    begin
      call(buf)
    rescue Exception => e
      Client.handle_error(e)
    end
  else
    puts
  end
ensure
  Client.execute('uncurry')
end
prompt() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 45
def prompt
  prompt_text = config.prompt
  Client.get_hooks('prepare_prompt').each {|hook|
    prompt_text = hook.call(prompt_text)
  }
  prompt_text
end
start() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 18
def start
  start_input_thread
end
start_input_thread() click to toggle source
# File lib/plugins/another_prompt.rb, line 72
def start_input_thread
  setup_readline()
  trap_setting()
  @input_thread = Thread.new do
    loop do
      begin
        value = config.plugins.another_prompt.shortcut_setting[wait_keypress]
        Client.pause
        case value
        when String
          call_prompt(value)
        when Proc
          value.call
        end
      ensure
        Client.resume
      end
    end
  end
  @input_thread.join
end
stop() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 22
def stop
  @input_thread.kill if @input_thread
end
trap_setting() click to toggle source
# File lib/plugins/another_prompt.rb, line 118
def trap_setting()
  begin
    trap("INT") do
      begin
        system "stty", STTY_ORIGIN
      ensure
        Client.execute('exit')
      end
    end
  rescue ArgumentError
  rescue Errno::ENOENT
  end
end
wait_keypress() click to toggle source
# File lib/plugins/another_prompt.rb, line 110
def wait_keypress
  system('stty', '-echo', '-icanon')
  c = STDIN.getc
  return [c].pack('c')
ensure
  system('stty', STTY_ORIGIN)
end

Private Instance Methods

do_completion(input) click to toggle source
# File lib/plugins/defaults/command_line.rb, line 72
def do_completion(input)
  input = input.sub(/^\s*/, '')

  words = []

  words = Client.commands.values.
            inject([]) {|array, command| array + [command.name] + command.aliases}.
            map(&:to_s).
            grep(/^\s*#{Regexp.quote(input)}/)

  if words.empty?
    command = Client.find_command(input)
    words = command ? command.complement(input) : []
  end

  if words.empty?
    Client.get_hooks(:completion).each do |hook|
      words << hook.call(input) rescue nil
    end
  end

  words.flatten.compact
rescue Exception => e
  Client.handle_error(e)
end
setup_readline() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 98
def setup_readline
  if Readline.respond_to?(:basic_word_break_characters=)
    Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
  end

  Readline.completion_case_fold = true

  Readline.completion_proc = lambda {|input| do_completion(input) }

  vi_or_emacs = config.editing_mode
  unless vi_or_emacs.empty?
    Readline.__send__("#{vi_or_emacs}_editing_mode")
  end

  Readline.rl_parse_and_bind('TAB: menu-complete')
end
trap_sigwinch() click to toggle source
# File lib/plugins/defaults/command_line.rb, line 141
def trap_sigwinch
  trap('WINCH') do
    dat = ''
    STDOUT.ioctl(TIOCGWINSZ, dat)
    rows, cols = dat.unpack('S!4')
    Readline.set_screen_size(rows, cols)
    ENV['LINES'] = rows.to_s
    ENV['COLUMNS'] = cols.to_s
  end
end