sim

the sim text editor
git clone git://ssnf.xyz/sim
Log | Files | Refs | README

commit 9145ce46f2ff45bed35fd920c2ad701d891db168
parent 8e29881a071c05910d1cd7a0cb1b955be0c9d58e
Author: ssnf <ssnf@ssnf.xyz>
Date:   Tue, 28 Jan 2025 23:25:17 +0000

implement msg()

Diffstat:
Msim.c | 48+++++++++++++++++++++++++++++++++++-------------
1 file changed, 35 insertions(+), 13 deletions(-)

diff --git a/sim.c b/sim.c @@ -11,6 +11,8 @@ #define FILECOUNT 8 #define LENGTH(a) (sizeof(a)/sizeof(a[0])) #define ISDIRTY(f) (((f).di == (f).i) ? 0 : '*') +#define RED(a) CSI "31m" a CSI "0m" +#define GREEN(a) CSI "32m" a CSI "0m" typedef long Posn; @@ -91,13 +93,14 @@ static void buf_add( static void change(int arg); static uint charsiz(char c, ulong wx); static void count(int arg); +static void curmov(ushort x, ushort y); static uint curpos(void); static void delete(int arg); static void dot(int arg); static void redo(int arg); static void escape(int c); static void init(void); -static void input(String* s, ushort line, char* msg); +static void input(String* s, uint line, char* prefix); static void insert(int arg); static int isword(uchar c); static void file_close(int arg); @@ -114,6 +117,7 @@ static void fr_insure(Frame* fr, ushort n); static void fr_update(void); static void fr_zero(Frame*); static void gmove(int arg); +static void msg(uint line, char* fmt, ...); static void paste(int arg); static void pline(int arg); static void resize(void); @@ -225,7 +229,7 @@ blind_writer(ushort line, ushort offset, ushort top, ushort bot) o = offset + bot - 1; else if (fr->n) o = fr->n - 1; - printf(CSI "%uH", offset > line ? 0 : line - offset); + curmov(0, offset > line ? 0 : line - offset); fwrite(&f->s.s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0 , 1, stdout ); @@ -303,6 +307,12 @@ count(int arg) counter += arg; } +static void +curmov(ushort x, ushort y) +{ + printf(CSI "%hu;%huH", y, x); +} + static uint curpos(void) { @@ -426,12 +436,12 @@ escape(int c) } static void -input(String* s, ushort line, char* msg) +input(String* s, uint line, char* prefix) { uchar c; for (;;) { - printf(CSI "%uH" EL "%s%s", line, msg, s->s); + msg(line, "%s%s", prefix, s->s); switch (c = fgetc(stdin)) { case Esc: str_zero(s); @@ -540,9 +550,8 @@ file_close(int arg) if (arg != -1) f = &file[arg]; if (ISDIRTY(*f)) { - printf( - CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, - f->name.n ? f->name.s : "-unnamed-" + msg(w.wy / 2, RED("Save %s?") " [y/n]" + , f->name.n ? f->name.s : "-unnamed-" ); if (fgetc(stdin) == 'y') file_save(arg); @@ -587,7 +596,7 @@ static void file_open(int arg) { file_close(-1); - input(&f->name, w.wy/2, CSI "32m$ " CSI "m"); + input(&f->name, w.wy / 2, GREEN("$ ")); file_load(f); } @@ -599,7 +608,7 @@ file_save(int arg) if (arg == -1) arg = f - file; if (!file[arg].name.n) { - input(&file[arg].name, w.wy/2, "File name: "); + input(&file[arg].name, w.wy / 2, "File name: "); if (!file[arg].name.n) return; } @@ -698,6 +707,7 @@ fr_insure(Frame* fr, ushort n) static void fr_update(void) { + static char stat[128]; Posn p0, p1; uint half; @@ -710,16 +720,16 @@ fr_update(void) fr_calc(); fwrite(ED, sizeof(ED), 1, stdout); blind_writer(half, fr->cur, half, half + (w.wy % 2)); - status: - printf(CSI "%uH", w.wy); - printf(STATUS); +status: + snprintf(stat, w.wx, STATUS); /*i dont care. TODO: care*/ + msg(w.wy, "%s", stat); for (p0 = fr->a[fr->cur].p0, p1 = 1; p0 < f->dot.p1; ++p0) { if (f->s.s[p0] == '\t' && p1 % w.t) p1 += (w.t + 1) - (p1 % w.t); else ++p1; } - printf(CSI "%u;%luH", half, p1); + curmov(p1, half); return; } @@ -854,6 +864,18 @@ gmove(int arg) } static void +msg(uint line, char* fmt, ...) +{ + va_list ap; + + curmov(0, line); + printf(EL); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} + +static void paste(int arg) { str_insert(&f->s, &istr, f->dot.p0);