Class: MMTop::TermInput

Inherits:
Object
  • Object
show all
Defined in:
lib/mmtop/term_input.rb

Instance Method Summary collapse

Constructor Details

#initializeTermInput

Returns a new instance of TermInput.



14
15
# File 'lib/mmtop/term_input.rb', line 14

def initialize()
end

Instance Method Details

#control(config) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mmtop/term_input.rb', line 38

def control(config)
  raw(true)
  char = nil
  begin
    Timeout::timeout(config.options['sleep']) do
      char = $stdin.read(1)
    end
  rescue Timeout::Error
    return
  end

  case char
    when "\n"
      return
    when "p"
      control_mode(config)
    when "q"
      exit(0)
  end
  ensure
    raw(false)
end

#control_mode(config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mmtop/term_input.rb', line 21

def control_mode(config)
  raw(false)
  while true
    cmdline = Readline::readline('mmtop> ')
    exit if cmdline.nil?

    cmdline.strip!
    Readline::HISTORY.push(cmdline)
    return if cmdline.empty?
    c = find_command(cmdline)
    if c.nil?
      c = find_command("help")
    end
    c.run(cmdline, config)
  end
end

#find_command(cmd) ⇒ Object



17
18
19
# File 'lib/mmtop/term_input.rb', line 17

def find_command(cmd)
  MMTop::Command.commands.detect { |c| c.matches?(cmd) }
end

#raw(bool) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/mmtop/term_input.rb', line 6

def raw(bool)
  if bool
    %x{stty -echo raw}
  else
    %x{stty echo -raw}
  end
end