class Picky::Terminal

A simple terminal based search.

Attributes

client[R]

Public Class Methods

new(given_uri, id_amount = nil) click to toggle source
# File lib/picky-client/tools/terminal.rb, line 9
def initialize given_uri, id_amount = nil
  check_highline_gem
  check_picky_client_gem

  require 'uri'
  uri = URI.parse given_uri

  # If the user gave a whole url without http, add that and reparse.
  #
  unless uri.path
    uri = URI.parse "http://#{given_uri}"
  end

  # If the user gave a path without / in front, add one.
  #
  unless uri.path =~ /^\//
    uri.path = "/#{uri.path}"
  end

  @searches  = 0
  @durations = 0
  @current_text  = ''
  @cursor_offset = 0
  @last_ids      = ''
  @id_amount     = id_amount && Integer(id_amount) || 20
  @client = Picky::Client.new uri

  install_trap
end

Public Instance Methods

add_text(text) click to toggle source

Add the given text to the current text.

# File lib/picky-client/tools/terminal.rb, line 123
def add_text text
  @current_text << text
end
backspace() click to toggle source

Delete one character.

# File lib/picky-client/tools/terminal.rb, line 101
def backspace
  chop_text
  print "\e[1D \e[1D"
  flush
end
chop_text() click to toggle source

Chop off one character.

# File lib/picky-client/tools/terminal.rb, line 117
def chop_text
  @current_text.chop!
end
clear_ids() click to toggle source

Clear the result ids.

# File lib/picky-client/tools/terminal.rb, line 160
def clear_ids
  move_to_ids
  write @ids_clearing_string ||= " "*200
end
flush() click to toggle source

Flush to STDOUT.

# File lib/picky-client/tools/terminal.rb, line 68
def flush
  STDOUT.flush
end
install_trap() click to toggle source

Install the Ctrl-C handler.

# File lib/picky-client/tools/terminal.rb, line 54
def install_trap
  Signal.trap('INT') do
    print "\e[100D"
    flush
    puts "\n"
    puts "You performed #{@searches} searches, totalling #{"%.3f" % @durations} seconds."
    print "\e[100D"
    flush
    exit
  end
end
left(amount = 1) click to toggle source

Position cursor amount to the left.

# File lib/picky-client/tools/terminal.rb, line 74
def left amount = 1
  print "\e[#{amount}D"
  flush
end
log(results) click to toggle source

Log a search.

# File lib/picky-client/tools/terminal.rb, line 167
def log results
  @searches += 1
  @durations += (results[:duration] || 0)
end
move_to(position) click to toggle source

Move cursor to position.

# File lib/picky-client/tools/terminal.rb, line 88
def move_to position
  relative = position - @cursor_offset
  if relative > 0
    right relative
  else
    left relative
  end
  @cursor_offset = position
  flush
end
move_to_ids() click to toggle source

Move to the id area.

# File lib/picky-client/tools/terminal.rb, line 144
def move_to_ids
  move_to 12 + @current_text.size
end
right(amount = 1) click to toggle source

Position cursor amount to the right.

# File lib/picky-client/tools/terminal.rb, line 81
def right amount = 1
  print "\e[#{amount}C"
  flush
end
run() click to toggle source

Run the terminal.

Note: Uses a simple loop to handle input.

# File lib/picky-client/tools/terminal.rb, line 201
def run
  puts "Type and see the result count update. Press enter for the first #{@id_amount} result ids."
  puts "Break with Ctrl-C."

  search_and_write

  loop do
    input = get_character

    case input
    when 127
      backspace
      search_and_write
    when 13
      search_and_write true
    else # All other.
      type_search input.chr
      search_and_write
    end
  end
end
search_and_write(full = false) click to toggle source

Perform a search and write the results.

Handles 404s and connection problems.

# File lib/picky-client/tools/terminal.rb, line 182
def search_and_write full = false
  results = search full
  results.extend Picky::Convenience

  log results

  full ? write_ids(results) : clear_ids

  write_results results
rescue Errno::ECONNREFUSED => e
  write "Please start a Picky server listening to #{@client.path}."
rescue Yajl::ParseError => e
  write "Got a 404. Maybe the path #{@client.path} isn't a correct one?"
end
write(text) click to toggle source

Write the text to the input area.

# File lib/picky-client/tools/terminal.rb, line 109
def write text
  @cursor_offset += text.size
  print text
  flush
end
write_ids(results) click to toggle source

Write the result ids.

# File lib/picky-client/tools/terminal.rb, line 150
def write_ids results
  move_to_ids
  write "=> #{(results.total ? results.ids(@id_amount) : []).inspect}"
rescue StandardError => e
  p e.message
  p e.backtrace
end
write_results(results) click to toggle source

Write the amount of result ids.

# File lib/picky-client/tools/terminal.rb, line 136
def write_results results
  move_to 0
  write "%9d" % (results && results.total || 0)
  move_to 10 + @current_text.size
end