aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolderpunk <solderpunk@sdf.org>2018-12-10 19:28:07 +0000
committerSolderpunk <solderpunk@sdf.org>2018-12-10 19:28:07 +0000
commitb543cae8ec8185920aa88b7571e038b2143058ce (patch)
tree2181cf1b93e1d25dec36de835b9ac440185f9f34
parentFail gracefully in the face of a corrupted thread. (diff)
Initial implementation of coloured output.
-rwxr-xr-xtelem.lua61
1 files changed, 47 insertions, 14 deletions
diff --git a/telem.lua b/telem.lua
index f4aec34..33909bd 100755
--- a/telem.lua
+++ b/telem.lua
@@ -16,6 +16,14 @@ _EDITOR = os.getenv("EDITOR")
if not _EDITOR then
_EDITOR = "/usr/bin/vim.tiny"
end
+_COLOURS = {
+ red=31,
+ green=32,
+ yellow=33,
+ blue=34,
+ magenta=35,
+ cyan=36,
+}
-- Global var declarations
@@ -27,6 +35,7 @@ current_board_threads = {} -- Array of thread tables, containing threads associa
current_thread = {} -- Active thread table, always an element of current_board_threads
current_thread_posts = {} -- Array of post tables, containing posts associated with current_thread
current_post_index = nil -- Integer index into current_thread_posts
+colours = true -- Boolean, controls whether to use ANSI colours
-- Utility functions
@@ -41,6 +50,11 @@ function getchar()
return char
end
+function colourise(colour, str)
+ if not colours or _COLOURS[colour] == nil then return str end
+ return string.char(27) .. "[" .. tostring(_COLOURS[colour]) .. "m" .. str .. string.char(27) .. "[0m"
+end
+
function dispatch_loop(dispatch_table, quit_char, prompt_func, err_str)
repeat
-- Show prompt and read 1 char from keyboard
@@ -188,6 +202,16 @@ function do_board()
print("Board created.")
end
+function do_colour()
+ if colours then
+ colours = false
+ print("Coloured text disabled.")
+ else
+ colours = true
+ print(colourise("red", "Coloured text enabled."))
+ end
+end
+
function do_go()
io.write("Go to which board? (use name or index) ")
local board = string.upper(io.read())
@@ -219,9 +243,9 @@ end
function do_list()
update_boards()
- print(string.format("%s %s %s %s", stringx.ljust("ID",3),
+ print(colourise("magenta", string.format("%s %s %s %s", stringx.ljust("ID",3),
stringx.ljust("Board name", 20),
- stringx.ljust("Board topic" ,50), "Thread count")
+ stringx.ljust("Board topic" ,50), "Thread count"))
)
print(string.rep("-",79))
for i,board in ipairs(boards) do
@@ -230,7 +254,7 @@ function do_list()
threads = threads +1
end
print(string.format("%3d %s %s [%3d threads]", i,
- stringx.ljust(board.name,20),
+ colourise("cyan", stringx.ljust(board.name,20)),
stringx.ljust(board.topic,50), threads)
)
end
@@ -245,15 +269,15 @@ function do_messages()
return
end
-- Headers
- print(string.format("%s %s %s %s", stringx.ljust("ID",3), stringx.ljust("Created", 14),
- stringx.ljust("Author",16), stringx.ljust("Subject",50)))
+ print(colourise("magenta", string.format("%s %s %s %s", stringx.ljust("ID",3), stringx.ljust("Created", 8),
+ stringx.ljust("Author",16), stringx.ljust("Subject",50))))
-- Separator
print(string.rep("-",79))
-- Messages
for i, thread in ipairs(current_board_threads) do
print(string.format("%3d %s %s %s [%3d posts]", i, os.date("%x", thread.timestamp),
- stringx.ljust(thread.author,16),
- stringx.ljust(thread.subject,50),
+ colourise("yellow", stringx.ljust(thread.author,16)),
+ stringx.ljust(thread.subject .. updated_str,50),
thread.post_count))
end
-- Separator
@@ -302,7 +326,7 @@ end
function do_scan()
for _, board in ipairs(boards) do
- io.write("Scanning " .. stringx.ljust(board.name,20))
+ io.write("Scanning " .. colourise("cyan", stringx.ljust(board.name,20)))
local threads = get_threads(board)
local updated_threads = 0
for _, thread in ipairs(threads) do
@@ -313,7 +337,7 @@ function do_scan()
if updated_threads == 0 then
print("No new posts")
else
- print(tostring(updated_threads) .. " threads updated!")
+ print(colourise("green", tostring(updated_threads) .. " thread(s) updated!"))
end
end
end
@@ -416,6 +440,17 @@ type_dispatch["s"] = do_type_save
type_dispatch["w"] = do_type_whole
type_dispatch["d"] = function() return end
+function type_prompt_func()
+ return "["..colourise("red","f").."]irst, " ..
+ "["..colourise("red","n").."]ext, " ..
+ "["..colourise("red","p").."]rev, " ..
+ "["..colourise("red","l").."]ast, " ..
+ "["..colourise("red","w").."]hole, " ..
+ "["..colourise("red","r").."]eply, " ..
+ "["..colourise("red","s").."]ave, " ..
+ "["..colourise("red","d").."]one > "
+end
+
function do_type()
if not check_at_board() then return end
io.write("Read which thread? ")
@@ -432,9 +467,7 @@ function do_type()
end
current_post_index = #current_thread_posts
do_type_show_post()
- dispatch_loop(type_dispatch, "d",
- function() return "[f]irst, [n]ext, [p]rev, [l]last, [w]hole, [r]eply, [s]ave, [d]one > " end,
- "Unrecognised command!")
+ dispatch_loop(type_dispatch, "d", type_prompt_func, "Unrecognised command!")
end
-- end of "type" command implementation
@@ -496,12 +529,12 @@ dispatch["M"] = do_unimplemented
dispatch["n"] = do_new
dispatch["r"] = do_reply
dispatch["b"] = do_board
-dispatch["c"] = do_unimplemented
+dispatch["c"] = do_colour
-- Infinite loop of command dispatch
function prompt()
if current_board then
- return "["..current_board.name.."] COMMAND :> "
+ return "["..colourise("cyan", current_board.name).."] COMMAND :> "
else
return "[] COMMAND :> "
end