Class: TConsole::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/tconsole/console.rb

Constant Summary collapse

KNOWN_COMMANDS =
["exit", "reload", "help", "info", "!failed", "!timings", "set"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, reporter) ⇒ Console

Returns a new instance of Console.



7
8
9
10
11
# File 'lib/tconsole/console.rb', line 7

def initialize(config, reporter)
  self.config = config
  self.reporter = reporter
  read_history
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/tconsole/console.rb', line 5

def config
  @config
end

#reporterObject

Returns the value of attribute reporter.



5
6
7
# File 'lib/tconsole/console.rb', line 5

def reporter
  @reporter
end

Instance Method Details

#define_autocomplete(pipe_server) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tconsole/console.rb', line 13

def define_autocomplete(pipe_server)
  Readline.completion_append_character = ""

  # Proc for helping us figure out autocompletes
  Readline.completion_proc = Proc.new do |str|
    known_commands = KNOWN_COMMANDS.grep(/^#{Regexp.escape(str)}/)
    known_commands.concat(@config.file_sets.keys.grep(/^#{Regexp.escape(str)}/))

    known_elements = []
    unless pipe_server.nil?
      known_elements = send_message(pipe_server, :autocomplete, str)
    end

    known_commands.concat(known_elements)
  end
end

#history_fileObject



126
127
128
# File 'lib/tconsole/console.rb', line 126

def history_file
  File.join(ENV['HOME'], ".tconsole_history")
end

#process_command(pipe_server, command) ⇒ Object

Public: Process a command however it needs to be handled.

pipe_server - The pipe server we’re working with command - The command we need to parse and handle



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tconsole/console.rb', line 70

def process_command(pipe_server, command)
  args = Shellwords.shellwords(command)

  # save the command unless we're exiting or repeating the last command
  unless args[0] == "exit" || (Readline::HISTORY.length > 0 && Readline::HISTORY[Readline::HISTORY.length - 1] == command)
    Readline::HISTORY << command
  end

  if command == ""
    # do nothing
  elsif args[0] == "exit"
    send_message(pipe_server, :stop)
    return :exit
  elsif args[0] == "reload"
    send_message(pipe_server, :stop)
    return :reload
  elsif args[0] == "help"
    reporter.help_message
  elsif args[0] == "!failed"
    send_message(pipe_server, :run_failed)
  elsif args[0] == "!timings"
    send_message(pipe_server, :show_performance, args[1])
  elsif args[0] == "info"
    send_message(pipe_server, :run_info)
  elsif args[0] == "set"
    send_message(pipe_server, :set, args[1], args[2])
  elsif args[0].start_with?(".")
    shell_command(command[1, command.length - 1])
  elsif @config.file_sets.has_key?(args[0])
    send_message(pipe_server, :run_file_set, args[0])
  else
    send_message(pipe_server, :run_all_tests, args)
  end

  nil
end

#read_and_execute(pipe_server) ⇒ Object

Returns true if the app should keep running, false otherwise



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tconsole/console.rb', line 31

def read_and_execute(pipe_server)
  prompt = "tconsole> "

  trap("SIGTSTP", "SYSTEM_DEFAULT")
  trap("SIGCONT") do
    print prompt
  end

  # Run any commands that have been passed
  result = process_command(pipe_server, @config.run_command)
  @config.run_command = ""
  if result == :exit || @config.once
    return false
  elsif result == :reload
    return true
  end

  define_autocomplete(pipe_server)

  # The command entry loop
  while command = Readline.readline(prompt, false)
    command.strip!
    result = process_command(pipe_server, command)

    if result == :exit
      return false
    elsif result == :reload
      return true
    end
  end

  send_message(:stop)
  false
end

#read_historyObject

Loads history from past sessions



142
143
144
145
146
147
148
# File 'lib/tconsole/console.rb', line 142

def read_history
  if ENV['HOME'] && File.exist?(history_file)
    File.readlines(history_file).reverse.each do |line|
      Readline::HISTORY << line
    end
  end
end

#send_message(pipe_server, message, *args) ⇒ Object



107
108
109
110
# File 'lib/tconsole/console.rb', line 107

def send_message(pipe_server, message, *args)
  pipe_server.write({:action => message.to_sym, :args => args})
  pipe_server.read
end

#shell_command(command) ⇒ Object

Internal: Runs a shell command on the console and outputs the results.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tconsole/console.rb', line 113

def shell_command(command)
  system(command)

  result = $?

  reporter.info
  if result.exitstatus == 0
    reporter.exclaim("Command exited with status code: 0")
  else
    reporter.error("Command exited with status code: #{result.exitstatus}")
  end
end

#store_historyObject

Stores last 50 items in history to $HOME/.tconsole_history



131
132
133
134
135
136
137
138
139
# File 'lib/tconsole/console.rb', line 131

def store_history
  if ENV['HOME']
    File.open(history_file, "w") do |f|
      Readline::HISTORY.to_a.reverse[0..49].each do |item|
        f.puts(item)
      end
    end
  end
end