From 07bf55b460dc6e78a02268c3147aab931845f237 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 26 Nov 2021 18:44:24 -0800 Subject: [PATCH] save timestamp on change; show in recent changes --- chesstv.tlv | 17 +++++++++++++++++ counter.tlv | 18 ++++++++++++------ hanoi.tlv | 12 ++++++++++++ life.tlv | 15 +++++++++++++++ src/lua.c | 22 ++++++++++++++++++++-- 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/chesstv.tlv b/chesstv.tlv index 87ef1d4..18208ad 100644 --- a/chesstv.tlv +++ b/chesstv.tlv @@ -1,5 +1,6 @@ teliva_program = { { + __teliva_timestamp = [==[original]==], window = [==[ window = curses.stdscr() -- animation-based app @@ -7,10 +8,12 @@ window:nodelay(true) lines, cols = window:getmaxyx()]==], }, { + __teliva_timestamp = [==[original]==], current_game = [==[ current_game = {}]==], }, { + __teliva_timestamp = [==[original]==], piece_glyph = [==[ piece_glyph = { -- for legibility, white pieces also use unicode glyphs for black pieces @@ -30,6 +33,7 @@ piece_glyph = { }]==], }, { + __teliva_timestamp = [==[original]==], top_player = [==[ function top_player(current_game) if current_game.players[1].color == "black" then @@ -39,6 +43,7 @@ function top_player(current_game) end]==], }, { + __teliva_timestamp = [==[original]==], bottom_player = [==[ function bottom_player(current_game) if current_game.players[1].color == "white" then @@ -48,6 +53,7 @@ function bottom_player(current_game) end]==], }, { + __teliva_timestamp = [==[original]==], render_player = [==[ function render_player(y, x, player) curses.mvaddstr(y, x, player.user.name) @@ -56,6 +62,7 @@ function render_player(y, x, player) end]==], }, { + __teliva_timestamp = [==[original]==], render_square = [==[ function render_square(current_game, rank, file, highlighted_squares) -- decide whether to highlight @@ -78,6 +85,7 @@ function render_square(current_game, rank, file, highlighted_squares) end]==], }, { + __teliva_timestamp = [==[original]==], render_fen_rank = [==[ function render_fen_rank(rank, fen_rank, highlighted_squares) local file = 1 @@ -116,6 +124,7 @@ function render_fen_rank(rank, fen_rank, highlighted_squares) end]==], }, { + __teliva_timestamp = [==[original]==], render_time = [==[ function render_time(y, x, seconds) if seconds == nil then return end @@ -124,6 +133,7 @@ function render_time(y, x, seconds) end]==], }, { + __teliva_timestamp = [==[original]==], render_board = [==[ function render_board(current_game) --? curses.mvaddstr(1, 50, dump(current_game.fen)) @@ -141,6 +151,7 @@ function render_board(current_game) end]==], }, { + __teliva_timestamp = [==[original]==], parse_lm = [==[ function parse_lm(move) --? curses.mvaddstr(4, 50, move) @@ -153,6 +164,7 @@ function parse_lm(move) end]==], }, { + __teliva_timestamp = [==[original]==], render = [==[ function render(chunk) local o = json.decode(chunk) @@ -175,6 +187,7 @@ function render(chunk) end]==], }, { + __teliva_timestamp = [==[original]==], init_colors = [==[ function init_colors() -- colors @@ -196,6 +209,7 @@ function init_colors() end]==], }, { + __teliva_timestamp = [==[original]==], main = [==[ function main() init_colors() @@ -214,6 +228,7 @@ function main() end]==], }, { + __teliva_timestamp = [==[original]==], utf8 = [==[ -- https://stackoverflow.com/questions/7983574/how-to-write-a-unicode-symbol-in-lua function utf8(decimal) @@ -235,6 +250,7 @@ function utf8(decimal) end]==], }, { + __teliva_timestamp = [==[original]==], split = [==[ function split(s, pat) result = {} @@ -245,6 +261,7 @@ function split(s, pat) end]==], }, { + __teliva_timestamp = [==[original]==], dump = [==[ -- https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console function dump(o) diff --git a/counter.tlv b/counter.tlv index 566314e..532e1e1 100644 --- a/counter.tlv +++ b/counter.tlv @@ -1,14 +1,17 @@ teliva_program = { { - window = [==[ + __teliva_timestamp = [==[original]==], + window = [==[ window = curses.stdscr()]==], }, { - n = [==[ + __teliva_timestamp = [==[original]==], + n = [==[ n = 0]==], }, { - render = [==[ + __teliva_timestamp = [==[original]==], + render = [==[ function render(window) window:clear() window:attron(curses.A_BOLD) @@ -21,11 +24,13 @@ function render(window) end]==], }, { - menu = [==[ + __teliva_timestamp = [==[original]==], + menu = [==[ menu = {Enter="increment"}]==], }, { - update = [==[ + __teliva_timestamp = [==[original]==], + update = [==[ function update(window) local key = curses.getch() if key == 10 then @@ -34,7 +39,8 @@ function update(window) end]==], }, { - main = [==[ + __teliva_timestamp = [==[original]==], + main = [==[ function main() for i=1,7 do curses.init_pair(i, 0, i) diff --git a/hanoi.tlv b/hanoi.tlv index cc1d33d..8bb8e7e 100644 --- a/hanoi.tlv +++ b/hanoi.tlv @@ -1,5 +1,6 @@ teliva_program = { { + __teliva_timestamp = [==[original]==], render = [==[ function render(window) window:clear() @@ -13,6 +14,7 @@ function render(window) end]==], }, { + __teliva_timestamp = [==[original]==], lines = [==[ function lines(window) local lines, cols = window:getmaxyx() @@ -20,16 +22,19 @@ function lines(window) end]==], }, { + __teliva_timestamp = [==[original]==], pop = [==[ function pop(array) return table.remove(array) end]==], }, { + __teliva_timestamp = [==[original]==], window = [==[ window = curses.stdscr()]==], }, { + __teliva_timestamp = [==[original]==], render_tower = [==[ function render_tower(window, line, col, tower_index, tower) window:attron(curses.A_BOLD) @@ -51,10 +56,12 @@ function render_tower(window, line, col, tower_index, tower) end]==], }, { + __teliva_timestamp = [==[original]==], tower = [==[ tower = {{6, 5, 4, 3, 2}, {}, {}}]==], }, { + __teliva_timestamp = [==[original]==], render_disk = [==[ function render_disk(window, line, col, size) col = col-size+1 @@ -67,6 +74,7 @@ function render_disk(window, line, col, size) end]==], }, { + __teliva_timestamp = [==[original]==], main = [==[ function main() for i=1,7 do @@ -81,6 +89,7 @@ end ]==], }, { + __teliva_timestamp = [==[original]==], len = [==[ function len(array) local result = 0 @@ -91,6 +100,7 @@ function len(array) end]==], }, { + __teliva_timestamp = [==[original]==], update = [==[ function update(window) window:mvaddstr(lines(window)-2, 5, "tower to remove top disk from? ") @@ -101,6 +111,7 @@ function update(window) end]==], }, { + __teliva_timestamp = [==[original]==], make_move = [==[ function make_move(from, to) local disk = pop(tower[from]) @@ -108,6 +119,7 @@ function make_move(from, to) end]==], }, { + __teliva_timestamp = [==[original]==], cols = [==[ function cols(window) local lines, cols = window:getmaxyx() diff --git a/life.tlv b/life.tlv index 4cae45b..46b3644 100644 --- a/life.tlv +++ b/life.tlv @@ -13,6 +13,7 @@ end ]==], }, { + __teliva_timestamp = [==[original]==], window = [==[ window = curses.stdscr() -- animation-based app @@ -20,6 +21,7 @@ window:nodelay(true) lines, cols = window:getmaxyx()]==], }, { + __teliva_timestamp = [==[original]==], grid_char = [==[ -- grab a 4x2 chunk of grid function grid_char(line, col) @@ -31,6 +33,7 @@ function grid_char(line, col) end]==], }, { + __teliva_timestamp = [==[original]==], print_grid_char = [==[ function print_grid_char(window, x) result = {} @@ -43,6 +46,7 @@ function print_grid_char(window, x) end]==], }, { + __teliva_timestamp = [==[original]==], glyph = [==[ -- look up the braille pattern corresponding to a 4x2 chunk of grid -- https://en.wikipedia.org/wiki/Braille_Patterns @@ -68,6 +72,7 @@ glyph = { }]==], }, { + __teliva_timestamp = [==[original]==], utf8 = [==[ -- https://stackoverflow.com/questions/7983574/how-to-write-a-unicode-symbol-in-lua function utf8(decimal) @@ -89,6 +94,7 @@ function utf8(decimal) end]==], }, { + __teliva_timestamp = [==[original]==], grid_char_to_glyph_index = [==[ -- convert a chunk of grid into a number function grid_char_to_glyph_index(g) @@ -98,6 +104,7 @@ function grid_char_to_glyph_index(g) end]==], }, { + __teliva_timestamp = [==[original]==], render = [==[ function render(window) window:clear() @@ -111,6 +118,7 @@ end ]==], }, { + __teliva_timestamp = [==[original]==], state = [==[ function state(line, col) if line < 1 or line > table.getn(grid) or col < 1 or col > table.getn(grid[1]) then @@ -120,6 +128,7 @@ function state(line, col) end]==], }, { + __teliva_timestamp = [==[original]==], num_live_neighbors = [==[ function num_live_neighbors(line, col) return state(line-1, col-1) + state(line-1, col) + state(line-1, col+1) + @@ -128,6 +137,7 @@ function num_live_neighbors(line, col) end]==], }, { + __teliva_timestamp = [==[original]==], step = [==[ function step() local new_grid = {} @@ -148,6 +158,7 @@ function step() end]==], }, { + __teliva_timestamp = [==[original]==], sleep = [==[ function sleep(a) local sec = tonumber(os.clock() + a); @@ -156,6 +167,7 @@ function sleep(a) end]==], }, { + __teliva_timestamp = [==[original]==], file_exists = [==[ function file_exists(filename) local f = io.open(filename, "r") @@ -168,6 +180,7 @@ function file_exists(filename) end]==], }, { + __teliva_timestamp = [==[original]==], load_file = [==[ function load_file(window, filename) io.input(filename) @@ -190,6 +203,7 @@ function load_file(window, filename) end]==], }, { + __teliva_timestamp = [==[original]==], update = [==[ menu = {arrow="pan"} @@ -230,6 +244,7 @@ function update(window, c) end]==], }, { + __teliva_timestamp = [==[original]==], main = [==[ function main() for i=1,7 do diff --git a/src/lua.c b/src/lua.c index ad1ded6..718b226 100644 --- a/src/lua.c +++ b/src/lua.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #define lua_c @@ -26,6 +27,9 @@ #include "lualib.h" +#define BG(i) (COLOR_PAIR((i)+8)) +#define FG(i) (COLOR_PAIR(i)) + static lua_State *globalL = NULL; @@ -407,6 +411,13 @@ static void update_definition (lua_State *L, const char *name, char *new_content lua_pushstring(L, new_contents); assert(strlen(name) > 0); lua_setfield(L, -2, name); + /* include timestamp at which binding was created */ + time_t t; + time(&t); + char time_string[50] = {0}; + ctime_r(&t, time_string); + lua_pushstring(L, time_string); + lua_setfield(L, -2, "__teliva_timestamp"); /* append the new table to the history of mutations */ int history_array_size = luaL_getn(L, history_array); ++history_array_size; @@ -531,7 +542,16 @@ void render_recent_changes (lua_State *L, int history_array, int start_index, in int t = lua_gettop(L); for (lua_pushnil(L); lua_next(L, t) != 0; lua_pop(L, 1)) { const char *definition_name = lua_tostring(L, -2); + if (is_special_history_key(definition_name)) continue; addstr(definition_name); + /* save timestamp of binding if available */ + lua_getfield(L, t, "__teliva_timestamp"); + if (!lua_isnil(L, -1)) { + attron(FG(7)); + printw(" %s", lua_tostring(L, -1)); + attroff(FG(7)); + } + lua_pop(L, 1); y++; const char *definition_contents = lua_tostring(L, -1); int x = 1; @@ -641,8 +661,6 @@ static int is_current_definition(lua_State *L, const char *definition_name, int } -#define BG(i) (COLOR_PAIR((i)+8)) -#define FG(i) (COLOR_PAIR(i)) void draw_definition_name (const char *definition_name) { attron(BG(7)); addstr(definition_name);