From d329e2def0cabf5cdc52719aa49c7cf63e00fbae Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Sat, 8 Dec 2018 16:51:25 +0000 Subject: Have main command loop and the type command 'subshell' share the same dispatch loop. --- telem.lua | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'telem.lua') diff --git a/telem.lua b/telem.lua index d4d5b5f..9646d84 100755 --- a/telem.lua +++ b/telem.lua @@ -36,6 +36,23 @@ function getchar() return char end +function dispatch_loop(dispatch_table, quit_char, prompt_func, err_str) + repeat + -- Show prompt and read 1 char from keyboard + io.write(prompt_func()) + local c = getchar() + if c ~= "\n" then io.write("\n") end + -- Use char as index into dispatch table + if c == "\n" then + -- pass + elseif dispatch_table[c] == nil then + print(err_str) + else + dispatch_table[c]() + end + until c == quit_char +end + -- Internals function update_boards() @@ -276,20 +293,9 @@ function do_type() current_thread_posts = get_posts(current_thread) current_post_index = #current_thread_posts do_type_show_post() - repeat - -- Show prompt and read one char - io.write("[f]irst, [n]ext, [p]rev, [l]last, [r]eply, [d]one > ") - c = getchar() - if c ~= "\n" then io.write("\n") end - -- Dispatch to sub-command - if c == "\n" then - -- pass - elseif type_dispatch[c] == nil then - print("Invalid command!") - elseif c ~= "\n" then - type_dispatch[c]() - end - until c == "d" + dispatch_loop(type_dispatch, "d", + function() return "[f]irst, [n]ext, [p]rev, [l]last, [r]eply, [d]one > " end, + "Unrecognised command!") end -- end of "type" command implementation @@ -353,17 +359,6 @@ dispatch["b"] = do_board dispatch["c"] = do_unimplemented -- Infinite loop of command dispatch -repeat - -- Show prompt and read 1 char from keyboard - io.write("["..current_board.."] COMMAND :> ") - c = getchar() - if c ~= "\n" then io.write("\n") end - -- Use char as index into dispatch table - if c == "\n" then - -- pass - elseif dispatch[c] == nil then - print("Unrecognised command, hit `h` for help.") - else - dispatch[c]() - end -until c == "q" +dispatch_loop(dispatch, "q", + function() return "["..current_board.."] COMMAND :> " end, + "Unrecognised command, hit `h` for help.") -- cgit v1.2.3