Skip to content

Commit

Permalink
postgres is connecting now
Browse files Browse the repository at this point in the history
  • Loading branch information
xea committed Nov 5, 2015
2 parents b2ca7c8 + 9975552 commit eebb161
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 78 deletions.
1 change: 1 addition & 0 deletions bin/toolbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../lib/service/logger'
require_relative '../lib/service/discovery'

# Base toolbox setup with a core and some of the most essential services.
core = Core.new :toolbox
core.register_service :console, ConsoleService
core.commit_stage
Expand Down
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
:discovery:
watch_dir: packages
scan_interval: 5
:console:
mode: dumb
66 changes: 58 additions & 8 deletions lib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
require_relative "console/mode_core"

# Enables user-interaction with the framework via keyboard inputs
class Console
class Console

attr_reader :interpreter

def initialize(terminal = Terminal.new)
@term = Terminal.new
@buffer = LineBuffer.new
@history = History.new ".toolbox.history"
@interpreter = Interpreter.new
@interpreter = Interpreter.new
@autocomplete = Autocomplete.new @interpreter
@running = false

Expand All @@ -42,8 +42,8 @@ def welcome
@term.puts
@term.puts "Known bugs and other #{'TODO'.green} items:"
end
# Returns the current cursor offset. This might seem

# Returns the current cursor offset. This might seem
def current_position
gen_prompt.no_colors.length + @buffer.idx
end
Expand Down Expand Up @@ -102,7 +102,7 @@ def read_input
@history.rewind
@last_tab = false
return s
when :key_question_mark
when :key_question_mark
if @buffer.idx == 0
help = generate_help
@term.clear_statusbar current_position
Expand Down Expand Up @@ -154,11 +154,9 @@ def gen_help(commands, width)
commands.map { |msg| fmt % msg }
end

local_commands, global_commands = @interpreter.generate_help
local_header = [ " Local commands" ]
local_commands = (@interpreter.modes.current_mode.available_commands + @interpreter.modes.current_accessors).map { |cmd| [ cmd.signature.to_readable, cmd.description ] }

global_header = [ " Global commands" ]
global_commands = @interpreter.modes.global_mode.available_commands.map { |cmd| [ cmd.signature.to_readable, cmd.description ] }

max_sig_length = (local_commands + global_commands).collect { |m| m[0].length }.max || 0
max_sig_length = 7 if max_sig_length < 7
Expand Down Expand Up @@ -195,3 +193,55 @@ def process_input(input)
@interpreter.process input
end
end

class DumbConsole

attr_reader :interpreter

def initialize
@term = Terminal.new
@interpreter = Interpreter.new
@autocomplete = Autocomplete.new @interpreter
@history = History.new ".toolbox.history"
@running = false

@interpreter.register_helper :history, @history
end

# Initialises the terminal and launches the CLI console
def start
@running = true
end

def stop
@running = false
end

# Prints a nice, warm welcoming banner introducing ourselves to the user.
def welcome
@term.puts "Welcome to " + "TOOLBOX".bold.green + " #{'3.0'.blue} (dummy mode)"
@term.puts
end

def main_loop
while @running do
prompt
raw_input = read_input
process_input raw_input
end
end

def read_input
gets
end

def process_input(input)
@history.append input
@interpreter.process input
end

def prompt
mode_str = "#{@interpreter.modes.current_mode.mode_id.to_s}"
"rsgi/#{mode_str}> "
end
end
Binary file added lib/console/.interpreter.rb.swn
Binary file not shown.
32 changes: 24 additions & 8 deletions lib/console/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Interpreter
def initialize(pure = false)
@modes = ModeRegistry.new
@tables = {}
@state = InterpreterState.new(@modes, @tables)
@state = InterpreterState.new(@modes, @tables, self)
@helpers = { intp: @state, out: STDOUT }

unless pure
Expand All @@ -29,7 +29,7 @@ def initialize(pure = false)

MethodSource::MethodExtensions.set_interpreter_instance self
end

# Removes unnecessary white spaces (and possibly other unwanted characters)
# from the given input string.
#
Expand Down Expand Up @@ -57,7 +57,7 @@ def process(raw_input)
def register_helper(id, helper)
@helpers[id] = helper
end

def register_mode(mode, type)
if !mode.nil? and mode.ancestors.member? BaseMode
mode_instance = mode.new
Expand All @@ -77,8 +77,8 @@ def unregister_mode(mode)
end
end

# Attempts to find an request handler for the current input. It might return nil when there's no
# corresponding handler to the input or it might return a command object containing the vital
# Attempts to find an request handler for the current input. It might return nil when there's no
# corresponding handler to the input or it might return a command object containing the vital
# information to react
def find_command(input)
available_accessors = @modes.current_accessors.find_all { |accessor| accessor.signature.matches? input }
Expand All @@ -103,7 +103,8 @@ def build_context(command = nil, user_args = {})
end

def lookup_args(user_args)
Hash[*(user_args.map { |k, v| [ k.gsub(/^[:$#*](.*)[+!]?/, "\\1").to_sym, lookup_arg(k, v) ] }.flatten)]
preprocessed_map = user_args.map { |k, v| [k.to_s.gsub(/[?]$/, ""), v] }
Hash[*(preprocessed_map.map { |k, v| [ k.gsub(/^[:$#*](.*)[+!]?/, "\\1").to_sym, lookup_arg(k, v) ] }.flatten)]
end

def lookup_arg(key, arg)
Expand Down Expand Up @@ -155,19 +156,34 @@ def resolve_args(command, ctx)
end
end unless method.nil?
end

def generate_help
local_commands = (modes.current_mode.available_commands + modes.current_accessors).map { |cmd| [ cmd.signature.to_readable, cmd.description ] }
global_commands = modes.global_mode.available_commands.map { |cmd| [ cmd.signature.to_readable, cmd.description ] }
[ local_commands, global_commands ]
end
end

class InterpreterState

attr_reader :modes, :tables

def initialize(modes = {}, tables = {})
def initialize(modes = {}, tables = {}, interpreter = nil)
@modes = modes
@tables = tables
@interpreter = interpreter
end

def quit
exit 0
exit 0
end

def context
@interpreter.build_context
end

def generate_help
@interpreter.generate_help
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/console/mode_debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ModeDebug < BaseMode
register_command(:mode_core, "core", "Enter core management mode") { |intp| intp.modes.enter_mode :core }
register_command(:exit_mode, "exit", "Exit current mode") { |intp| intp.modes.exit_mode }
register_command(:launch_debugger, "debugger", "Launch interactive debugger (pry)")
register_command(:show_helpers, "show helpers", "Show table of registered helpers")
register_command(:show_tables, "show tables", "Show table of registered data tables")

def construct
@mode_active = false
Expand All @@ -24,4 +26,14 @@ def pre_exit
def launch_debugger
binding.pry
end

def show_helpers(intp, out)
t = PrinTable.new
out.puts t.print_table([ "ID", "CLASS" ], intp.context.helpers.map { |id, helper| [ id.to_s, helper.class.name ] })
end

def show_tables(intp, out)
t = PrinTable.new
out.puts t.print_table([ "TABLE ID" ], intp.context.tables.map { |id, table| [ id ] })
end
end
10 changes: 8 additions & 2 deletions lib/console/mode_global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ class ModeGlobal < BaseMode
})

register_command(:quit_app, "quit", "Immediately quit application") { |intp| intp.quit }
register_command(:show_help, "help", "Show help about available commands")

# Debug mode is allowed anywhere in the shell
register_command(:mode_debug, "debug", "Enter debug mode", scope(true) { |intp| intp.modes.current_mode.mode_id != :debug }) { |intp| intp.modes.enter_mode :debug }

# This command is generated when other commands can't be assigned to the current request
def command_not_found
Command.new(:not_found, "", "") { puts "not found" }
Command.new(:not_found, "", "") { puts "not found" }
end

def show_help(intp, out)
pt = PrinTable.new
local_cmd, global_cmd = intp.generate_help
out.puts pt.print(["PATTERN", "DESCRIPTION"], local_cmd)
out.puts pt.print(["PATTERN", "DESCRIPTION"], global_cmd)
end
end

13 changes: 0 additions & 13 deletions lib/console/mode_home.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,4 @@ class ModeHome < ModeGlobal
}
})

register_command(:mode_browse, "browse $objecttype :objectid", "Browse objects") do |objecttype, objectid|
p objecttype
p objectid
end

register_command(:set_global_var, "set :varname :value") { |varname, value| @global_vars[varname] = value }
register_command(:get_global_var, "get :varname") { |varname| puts @global_vars[varname] }

def construct
@global_vars = {}
end

end

11 changes: 7 additions & 4 deletions lib/console/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def print_table(headers, data, style = nil)
end
end

def print_simple_table(headers, data)
def print_simple_table(headers, data, &aggregator)

print_fmt_table(headers, data, { v: " ", sh: false, pl: 0, pr: 0 })
print_fmt_table(headers, data, { v: " ", sh: false, pl: 0, pr: 0 }, aggregator)
end

def print_raw_table(headers, data)
Expand All @@ -48,7 +48,7 @@ def print_raw_table(headers, data)
result
end

def print_fmt_table(headers, data, fmt = {})
def print_fmt_table(headers, data, fmt = {}, aggregator = nil)
# pl - padding left, pr - padding right, sh: separate headers
default_fmt = { node: "+", h: "-", v: "|", pl: 1, pr: 1, sh: true }
fmt = default_fmt.merge fmt
Expand Down Expand Up @@ -79,7 +79,10 @@ def print_fmt_separator(lengths, deco)
result << print_fmt_separator(field_lengths, fmt) if fmt[:sh]
end

result += data.map { |row| print_fmt_row(row, field_lengths.map { |x| x * -1 }, fmt) }
result += data.map { |row|
aggregator.yield row unless aggregator.nil?
print_fmt_row(row, field_lengths.map { |x| x * -1 }, fmt)
}
else
[]
end
Expand Down
Loading

0 comments on commit eebb161

Please sign in to comment.