Skip to content

Commit

Permalink
added dummy console
Browse files Browse the repository at this point in the history
  • Loading branch information
xea committed Nov 3, 2015
1 parent d5555c3 commit 9975552
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
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
20 changes: 15 additions & 5 deletions lib/console/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -155,6 +155,12 @@ 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
Expand All @@ -168,12 +174,16 @@ def initialize(modes = {}, tables = {}, interpreter = nil)
end

def quit
exit 0
exit 0
end

def context
@interpreter.build_context
end

def generate_help
@interpreter.generate_help
end
end

module MethodSource::MethodExtensions
Expand Down
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

12 changes: 7 additions & 5 deletions lib/service/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ConfigService < Service

optional_features :logger, :console
provided_features :config
provided_features :config

def initialize(filename = nil)
super
Expand Down Expand Up @@ -86,6 +86,8 @@ def feature_up(feature, service)

class ConfigProxy < SimpleService

attr_reader :spawn_id

def initialize(spawn_id, service)
super
@spawn_id = spawn_id
Expand All @@ -99,18 +101,18 @@ def dump
def [](key)
if @spawn_id.nil?
lookup_key, spawn_id = key.split('/').reverse
@service.get(spawn_id.to_sym, lookup_key)
@service.get(spawn_id.to_sym, lookup_key.to_s)
else
@service.get(@spawn_id, key)
@service.get(@spawn_id, key.to_s)
end
end

def []=(key, value)
if @spawn_id.nil?
lookup_key, spawn_id = key.split('/').reverse
@service.set(spawn_id.to_sym, lookup_key, value)
@service.set(spawn_id.to_sym, lookup_key.to_s, value)
else
@service.set(@spawn_id, key, value)
@service.set(@spawn_id, key.to_s, value)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/service/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ def err
class ConsoleService < SimpleService

required_features :framework, :console_host
optional_features :config
provided_features :console

def start
sleep 0.1 # <- wtf hack to allow asynchronous calls, Celluloid srsly?
@running = true
@console_host.out.puts 'Console service started'
@console = Console.new

if !@config.nil? and @config[:mode] == "dumb"
@console = DumbConsole.new
else
@console = Console.new
end
@console.interpreter.register_helper :framework, @framework
end

Expand Down

0 comments on commit 9975552

Please sign in to comment.