commit 7cd41b83fd66c66f1b943f9fb79bd9077e76cca7
parent 13edce3b337dd516984ef8aa2cb946303db49e44
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Wed, 30 Sep 2020 19:27:49 +0000
change how history and value retrieval works
Diffstat:
M | sss.c | | | 55 | +++++++++++++++++++++++++++++++++++++------------------ |
1 file changed, 37 insertions(+), 18 deletions(-)
diff --git a/sss.c b/sss.c
@@ -28,6 +28,7 @@ void draw_rows(void);
void colors(void);
void run_cmd(Mousectl *mc, Keyboardctl *kc);
void edit_cell(Addr addr, Mousectl *mc, Keyboardctl *kc);
+Value* hist_get_value(Stack*, Addr);
void
threadmain(int argc, char **argv)
@@ -68,9 +69,9 @@ threadmain(int argc, char **argv)
if (argc == 1) T = table_read(argv[0]);
else T = empty_table();
- push(&hp, T);
hp.size = 0;
hp.data = nil;
+ push(&hp, T);
clear();
draw_cells();
draw_columns();
@@ -145,6 +146,7 @@ usage(void)
fprint(2, "usage: %s", argv0);
threadexitsall("usage");
}
+
void
clear(void)
{
@@ -225,21 +227,22 @@ draw_cells(void)
Rectangle r;
long x, y;
char *text;
- Value v;
+ Value *v;
x = 0;
y = 0;
pt = addpt(Pt(defcwidth, defcheight), screen->r.min);
pt = addpt(pt, view);
- while (pt.y < screen->r.max.y){
+ while (pt.y < screen->r.max.y) {
if ((pt.x >= screen->r.min.x) && (pt.y >= screen->r.min.y)) {
r = rectaddpt(Rect(0,0,defcwidth, defcheight), pt);
- v = table_get(T, xy2addr(x, y));
- text = v.data;
+ v = hist_get_value(&hp, xy2addr(x, y));
+ if (v == nil) text = "";
+ else text = v->data;
draw_cell(screen, r, Ibg, Iborder, Ifg, text, -1);
};
pt.x += defcwidth;
x++;
- if (pt.x > screen->r.max.x){
+ if (pt.x > screen->r.max.x) {
pt.x = screen->r.min.x + defcwidth + view.x;
pt.y += defcheight;
y++;
@@ -261,20 +264,36 @@ run_cmd(Mousectl *mc, Keyboardctl *kc)
void
edit_cell(Addr addr, Mousectl *mc, Keyboardctl *kc)
{
- Value v;
+ Value *v;
+ Stack *N;
+ N = empty_table();
char prompt[256], buf[1024];
snprint(prompt, 256, "%ld,%ld:", addr.x, addr.y);
- v = table_get(T, addr);
+ v = hist_get_value(&hp, addr);
*buf = 0;
- if (v.data != 0) strncat(buf, v.data, 1024);
- if (enter(prompt, buf, 1024, mc, kc, 0) < 0) {
- free_value(v);
- return;
- }
- if (strcmp(v.data, buf) == 0){
- free_value(v);
- return;
+ if ((v != nil)&&(v->data != nil)) strncat(buf, v->data, 1024);
+ if (enter(prompt, buf, 1024, mc, kc, 0) < 0) return;
+ //if (strcmp(v->data, buf) == 0) return;
+ v = malloc(sizeof(Value));
+ v->addr = addr;
+ v->data = strdup(buf);
+ push(N, v);
+ push(&hp, N);
+ T = N;
+}
+
+Value*
+hist_get_value(Stack *H, Addr addr)
+{
+ Stack *T;
+ Value *v;
+ long x, y;
+ for (x = H->size - 1; x >= 0; x--) {
+ T = H->data[x];
+ for (y = 0; y < T->size; y++) {
+ v = T->data[y];
+ if (addrcmp(addr, v->addr) == 0) return v;
+ }
}
- T = table_put(T, mk_value(addr, buf));
- push(&hp, T);
+ return nil;
}