Class: TConsole::Console
- Inherits:
-
Object
- Object
- TConsole::Console
- Defined in:
- lib/tconsole/console.rb
Constant Summary collapse
- KNOWN_COMMANDS =
["exit", "reload", "help", "info", "!failed", "!timings", "set"]
Instance Attribute Summary collapse
-
#pipe_server ⇒ Object
Returns the value of attribute pipe_server.
Instance Method Summary collapse
- #define_autocomplete ⇒ Object
- #history_file ⇒ Object
-
#initialize(config) ⇒ Console
constructor
A new instance of Console.
-
#print_help ⇒ Object
Prints a list of available commands.
-
#process_command(command) ⇒ Object
Public: Process a command however it needs to be handled.
-
#read_and_execute ⇒ Object
Returns true if the app should keep running, false otherwise.
-
#read_history ⇒ Object
Loads history from past sessions.
- #send_message(message, *args) ⇒ Object
-
#store_history ⇒ Object
Stores last 50 items in history to $HOME/.tconsole_history.
Constructor Details
#initialize(config) ⇒ Console
Returns a new instance of Console.
7 8 9 10 11 12 |
# File 'lib/tconsole/console.rb', line 7 def initialize(config) @config = config read_history define_autocomplete end |
Instance Attribute Details
#pipe_server ⇒ Object
Returns the value of attribute pipe_server.
5 6 7 |
# File 'lib/tconsole/console.rb', line 5 def pipe_server @pipe_server end |
Instance Method Details
#define_autocomplete ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tconsole/console.rb', line 14 def define_autocomplete 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 = (:autocomplete, str) end known_commands.concat(known_elements) end end |
#history_file ⇒ Object
160 161 162 |
# File 'lib/tconsole/console.rb', line 160 def history_file File.join(ENV['HOME'], ".tconsole_history") end |
#print_help ⇒ Object
Prints a list of available commands
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/tconsole/console.rb', line 117 def print_help puts puts "Available commands:" puts puts "reload # Reload your Rails environment" puts "set [variable] [value] # Sets a runtime variable (see below for details)" puts "exit # Exit the console" puts "!failed # Runs the last set of failing tests" puts "!timings [limit] # Lists the timings for the last test run, sorted." puts "[filename] [test_pattern] # Run the tests contained in the given file" puts ".[command] # Executes the given command in a subshell" puts puts "Running file sets" puts puts "File sets are sets of files that are typically run together. For example," puts "in Rails projects it's common to run `rake test:units` to run all of the" puts "tests under the units directory." puts puts "Available file sets:" @config.file_sets.each do |set, paths| puts set end puts puts "Working with test patterns:" puts puts "All of the test execution commands include an optional test_pattern argument. A" puts "test pattern can be given to filter the executed tests to only those tests whose" puts "name matches the pattern given. This is especially useful when rerunning a failing" puts "test." puts puts "Runtime Variables" puts puts "You can set runtime variables with the set command. This helps out with changing" puts "features of TConsole that you may want to change at runtime. At present, the" puts "following runtime variables are available:" puts puts "fast # Turns on fail fast mode. Values: on, off" puts end |
#process_command(command) ⇒ Object
Public: Process a command however it needs to be handled.
command - The command we need to parse and handle
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 106 107 108 109 |
# File 'lib/tconsole/console.rb', line 73 def process_command(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" (:stop) self.pipe_server = nil return :exit elsif args[0] == "reload" (:stop) return :reload elsif args[0] == "help" print_help elsif args[0] == "!failed" (:run_failed) elsif args[0] == "!timings" (:show_performance, args[1]) elsif args[0] == "info" (:run_info) elsif args[0] == "set" (:set, args[1], args[2]) elsif args[0].start_with?(".") (:shell, command[1, command.length - 1]) elsif @config.file_sets.has_key?(args[0]) (:run_file_set, args[0]) else (:run_all_tests, args) end nil end |
#read_and_execute ⇒ Object
Returns true if the app should keep running, false otherwise
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 65 66 67 68 |
# File 'lib/tconsole/console.rb', line 32 def read_and_execute if pipe_server.nil? puts "No connection to test environment. Exiting." return false end prompt = "tconsole> " trap("SIGTSTP", "SYSTEM_DEFAULT") trap("SIGCONT") do print prompt end # Run any commands that have been passed result = process_command(@config.run_command) @config.run_command = "" if result == :exit || @config.once return false elsif result == :reload return true end # The command entry loop while command = Readline.readline(prompt, false) command.strip! result = process_command(command) if result == :exit return false elsif result == :reload return true end end (:stop) false end |
#read_history ⇒ Object
Loads history from past sessions
176 177 178 179 180 181 182 |
# File 'lib/tconsole/console.rb', line 176 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(message, *args) ⇒ Object
111 112 113 114 |
# File 'lib/tconsole/console.rb', line 111 def (, *args) pipe_server.write({:action => .to_sym, :args => args}) pipe_server.read end |
#store_history ⇒ Object
Stores last 50 items in history to $HOME/.tconsole_history
165 166 167 168 169 170 171 172 173 |
# File 'lib/tconsole/console.rb', line 165 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 |