diff options
author | Solderpunk <solderpunk@sdf.org> | 2018-12-08 16:51:25 +0000 |
---|---|---|
committer | Solderpunk <solderpunk@sdf.org> | 2018-12-08 16:51:25 +0000 |
commit | d329e2def0cabf5cdc52719aa49c7cf63e00fbae (patch) | |
tree | d022b51f3ef5e07906330f57c5e13daf349c7828 | |
parent | Consolidate all code checking that user is at a board into a function. (diff) |
Have main command loop and the type command 'subshell' share the same dispatch loop.
-rwxr-xr-x | telem.lua | 51 |
1 files changed, 23 insertions, 28 deletions
@@ -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.") |