standardize key order in .tlv files

This will eliminate some spurious git diffs I keep having to clean up.
This commit is contained in:
Kartik K. Agaram 2022-02-09 09:18:05 -08:00
parent b328ffc5e5
commit e552571b1e
5 changed files with 57 additions and 40 deletions

View File

@ -187,7 +187,8 @@
> curses.init_pair(14, 7, 6)
> curses.init_pair(15, -1, 15)
>end
- main:
- __teliva_timestamp: original
main:
>function main()
> init_colors()
>
@ -196,4 +197,3 @@
> update(window)
> end
>end
__teliva_timestamp: original

View File

@ -34,7 +34,6 @@
> window:attroff(curses.A_BOLD)
> curses.refresh()
>end
__teliva_note: foo
- __teliva_timestamp: original
menu:
>menu = {

View File

@ -128,6 +128,30 @@ void emit_multiline_string(FILE* out, const char* value) {
}
}
static const char* special_history_keys[] = {
"__teliva_timestamp",
"__teliva_note",
"__teliva_undo",
NULL,
};
/* save key and its value at top of stack to out
* no stack side effects */
static void save_tlv_key(lua_State* L, const char* key, FILE* out) {
if (strcmp(key, "__teliva_undo") == 0) {
fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1));
return;
}
const char* value = lua_tostring(L, -1);
if (strchr(value, ' ') || strchr(value, '\n')) {
fprintf(out, "%s:\n", key);
emit_multiline_string(out, value);
}
else {
fprintf(out, "%s: %s\n", key, value);
}
}
void save_tlv(lua_State* L, char* filename) {
lua_getglobal(L, "teliva_program");
int history_array = lua_gettop(L);
@ -162,23 +186,24 @@ void save_tlv(lua_State* L, char* filename) {
lua_rawgeti(L, history_array, i);
int table = lua_gettop(L);
int first = 1;
// standardize order of special keys
for (int k = 0; special_history_keys[k]; ++k) {
lua_getfield(L, table, special_history_keys[k]);
if (!lua_isnil(L, -1)) {
if (first) fprintf(out, "- ");
else fprintf(out, " ");
first = 0;
save_tlv_key(L, special_history_keys[k], out);
}
lua_pop(L, 1);
}
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (is_special_history_key(lua_tostring(L, -2)))
continue;
if (first) fprintf(out, "- ");
else fprintf(out, " ");
first = 0;
const char* key = lua_tostring(L, -2);
if (strcmp(key, "__teliva_undo") == 0) {
fprintf(out, "%s: %ld\n", key, lua_tointeger(L, -1));
continue;
}
const char* value = lua_tostring(L, -1);
if (strchr(value, ' ') || strchr(value, '\n')) {
fprintf(out, "%s:\n", key);
emit_multiline_string(out, value);
}
else {
fprintf(out, "%s: %s\n", key, value);
}
save_tlv_key(L, lua_tostring(L, -2), out);
}
lua_pop(L, 1);
}
@ -187,13 +212,6 @@ void save_tlv(lua_State* L, char* filename) {
lua_pop(L, 1);
}
static const char* special_history_keys[] = {
"__teliva_timestamp",
"__teliva_undo",
"__teliva_note",
NULL,
};
int is_special_history_key(const char* key) {
for (const char** curr = special_history_keys; *curr != NULL; ++curr) {
if (strcmp(*curr, key) == 0)

View File

@ -189,7 +189,8 @@
> curses.init_pair(14, 7, 6)
> curses.init_pair(15, -1, 15)
>end
- main:
- __teliva_timestamp: original
main:
>function main()
> init_colors()
>
@ -198,4 +199,3 @@
> update(window)
> end
>end
__teliva_timestamp: original

30
zet.tlv
View File

@ -695,7 +695,9 @@
> check_eq(cursor_up('abcdefg\nhij', 11, 5), 8, 'cursor_up to wrapping line: final char')
> check_eq(cursor_up('abcdefg\nhij', 12, 5), 8, 'cursor_up to wrapping line: to shorter line')
>end
- render:
- __teliva_timestamp:
>Wed Feb 9 08:15:25 2022
render:
>function render(window)
> window:clear()
> local lines, cols = window:getmaxyx()
@ -743,9 +745,9 @@
> end
> curses.refresh()
>end
__teliva_timestamp:
>Wed Feb 9 08:15:25 2022
- main:
- __teliva_timestamp:
>Wed Feb 9 08:15:35 2022
main:
>function main()
> init_colors()
> current_zettel_id = zettels.root
@ -756,14 +758,12 @@
> update(window)
> end
>end
__teliva_timestamp:
>Wed Feb 9 08:15:35 2022
- __teliva_note:
- __teliva_timestamp:
>Wed Feb 9 08:16:24 2022
__teliva_note:
>get rid of commandline
>
>There's a reason vim hides it. Confusing to have two cursors on screen.
__teliva_timestamp:
>Wed Feb 9 08:16:24 2022
editz:
>function editz()
> menu = { {'^e', 'back to browsing'},}
@ -778,7 +778,9 @@
> quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
> end
>end
- editz_render:
- __teliva_timestamp:
>Wed Feb 9 08:22:20 2022
editz_render:
>function editz_render(window, s, cursor, top, minbottom, left, right)
> local h, w = window:getmaxyx()
> local cursor_y, cursor_x, cursor_c = 0, 0, 'c'
@ -822,8 +824,6 @@
> end
> window:mvaddstr(cursor_y, cursor_x, cursor_c)
>end
__teliva_timestamp:
>Wed Feb 9 08:22:20 2022
- __teliva_timestamp:
>Wed Feb 9 08:25:05 2022
editz:
@ -842,12 +842,12 @@
> end
> curses.curs_set(0)
>end
- __teliva_note:
- __teliva_timestamp:
>Wed Feb 9 08:28:13 2022
__teliva_note:
>stop simulating the cursor
>
>editz_render is now much simpler
__teliva_timestamp:
>Wed Feb 9 08:28:13 2022
editz_update:
>function editz_update(window, prose, cursor)
> local key = curses.getch()