diff options
author | Solderpunk <solderpunk@sdf.org> | 2018-12-08 14:54:22 +0000 |
---|---|---|
committer | Solderpunk <solderpunk@sdf.org> | 2018-12-08 14:54:22 +0000 |
commit | 13b57cc9f6cd093d28ddfd1ed69b30b2e153f6f2 (patch) | |
tree | 7b3ad47dc1ca7f61be273791f498baafe272342d | |
parent | Declare and explain global vars at start of program. (diff) |
General tidy up.
Diffstat (limited to '')
-rwxr-xr-x | telem.lua | 95 |
1 files changed, 50 insertions, 45 deletions
@@ -23,20 +23,13 @@ current_thread = {} -- Active thread table, always an element of current_board_ current_thread_posts = {} -- Array of post tables, containing posts associated with current_thread current_post_index = nil -- Integer index into current_thread_posts +-- Utility functions + function cat_file(filename) io.input(filename) - welcome = io.read("*all") + contents = io.read("*all") io.input(io.stdin) - io.write(welcome) -end - -function show_welcome() - print("::::::::::: This is telem ver."..telemver.." :::::::::::") - cat_file(path.join(_BBS_ROOT, "docs", "welcome")) -end - -function show_prompt() - io.write("["..current_board.."] COMMAND :> ") + io.write(contents) end function getchar() @@ -46,6 +39,18 @@ function getchar() return char end +-- Internals + +function update_boards() + for board in lfs.dir(path.join(_BBS_ROOT, "boards")) do + if string.sub(board, 1, 1) ~= "." then + boards[board] = true + table.insert(board_names, board) + end + end + table.sort(board_names) +end + function get_threads(board) local threads = {} for threaddir in lfs.dir(path.join(_BBS_ROOT, "boards", board)) do @@ -91,6 +96,8 @@ function get_posts(thread) return posts end +-- Commands + function do_board() -- Creates a new (empty) board @@ -138,11 +145,8 @@ function do_help2() cat_file(path.join(_BBS_ROOT, "docs", "help2")) end -function do_unimplemented() - print("Sorry, this command is not (yet) implemented!") -end - function do_list() + update_boards() for _,b in pairs(board_names) do local threads = -3 -- Don't want to count "topic" file or "." or ".." for topic in lfs.dir(path.join(_BBS_ROOT, "boards", b)) do @@ -154,7 +158,7 @@ end function do_messages() if current_board == "" then - print("Not at any board") + print("Not at any board! Hit `l` to list boards, `g` to go to one.") else current_board_threads = get_threads(current_board) for i, thread in ipairs(current_board_threads) do @@ -198,7 +202,9 @@ function do_new() print("Post submitted.") end --- Type stuff below +-- The "type" command is implemented using multiple functions +-- do_type is the entry point, which runs a command dispatch loop that +-- runs various do_type_xxx functions. function do_type_first() current_post_index = 1 @@ -239,6 +245,17 @@ function do_type_reply() do_type_show_post() end +function do_type_show_post() + local post = current_thread_posts[current_post_index] + print("SUBJECT: " .. current_thread.subject) + print("AUTHOR: " .. post.author) + print("POSTED: " .. os.date("%H:%M %B %d, %Y", post.timestamp)) + print("--------------------------------") + cat_file(post.filename) + print("--------------------------------") + print(string.format("Viewing post %d of %d in thread", current_post_index, #current_thread_posts)) +end + type_dispatch = {} type_dispatch["f"] = do_type_first type_dispatch["l"] = do_type_last @@ -248,7 +265,7 @@ type_dispatch["r"] = do_type_reply type_dispatch["d"] = function() return end function do_type() - io.write("Display which thread? ") + io.write("Read which thread? ") local thread_id = tonumber(io.read()) if not thread_id or thread_id < 0 or thread_id > #current_board_threads then print("Invalid thread index!") @@ -259,9 +276,11 @@ function do_type() current_post_index = #current_thread_posts do_type_show_post() repeat - show_type_prompt() + -- Show prompt and read one char + io.write("[f]irst, [n]ext, [p]rev, [l]last, [r]eply, [d]one > ") c = getchar() io.write("\n") + -- Dispatch to sub-command if type_dispatch[c] == nil then print("Invalid command!") else @@ -270,20 +289,7 @@ function do_type() until c == "d" end -function show_type_prompt() - io.write("[f]irst, [n]ext, [p]rev, [l]last, [r]eply, [d]one > ") -end - -function do_type_show_post() - local post = current_thread_posts[current_post_index] - print("SUBJECT: " .. current_thread.subject) - print("AUTHOR: " .. post.author) - print("POSTED: " .. os.date("%H:%M %B %d, %Y", post.timestamp)) - print("--------------------------------") - cat_file(post.filename) - print("--------------------------------") - print(string.format("Viewing post %d of %d in thread", current_post_index, #current_thread_posts)) -end +-- end of "type" command implementation function do_quit() print("Goodbye!") @@ -310,8 +316,8 @@ function do_rules() cat_file(path.join(_BBS_ROOT, "docs", "rules")) end -function do_stub() - print("Stub!") +function do_unimplemented() + print("Sorry, this command is not (yet) implemented!") end function do_who() @@ -320,15 +326,12 @@ end -- MAIN PROGRAM BODY BELOW -show_welcome() +-- Show welcome message +print("::::::::::: This is telem ver."..telemver.." :::::::::::") +cat_file(path.join(_BBS_ROOT, "docs", "welcome")) -for board in lfs.dir(path.join(_BBS_ROOT, "boards")) do - if string.sub(board, 1, 1) ~= "." then - boards[board] = true - table.insert(board_names, board) - end -end -table.sort(board_names) +-- Initialise global vars representing BBS state +update_boards() -- Build dispatch table mapping chars to functions dispatch = {} @@ -351,11 +354,13 @@ dispatch["c"] = do_unimplemented -- Infinite loop of command dispatch repeat - show_prompt() + -- Show prompt and read 1 char from keyboard + io.write("["..current_board.."] COMMAND :> ") c = getchar() io.write("\n") + -- Use char as index into dispatch table if dispatch[c] == nil then - print("What?") + print("Unrecognised command, hit `h` for help.") else dispatch[c]() end |