summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWormHeamer2026-01-01 17:59:54 -0500
committerWormHeamer2026-01-01 17:59:54 -0500
commit4018b4f3b06ea31a19239118085d049f319df128 (patch)
treed523efd760cd30ab07234c7022685d415ac10b04
parent82fb77ca52d097ebf399924e3b8b6231362885af (diff)
normalize buffer paths, add buffer select, show basename in tabs
-rw-r--r--main.c49
1 files changed, 43 insertions, 6 deletions
diff --git a/main.c b/main.c
index 6879999..d6e39b1 100644
--- a/main.c
+++ b/main.c
@@ -81,11 +81,13 @@ typedef struct {
Editor e = { 0 };
+static Str normalize_path(Str s);
int ed_buf_open(Editor *e, const char *path) {
+ Str paths = { 0 };
if (path) {
- Str s = str_from_cstr(path);
+ paths = normalize_path(str_from_cstr(path));
for (u32 i = 0; i < e->bufn; i++) {
- if (str_eql(e->buf[i].path, s)) return i;
+ if (str_eql(e->buf[i].path, paths)) return i;
}
}
@@ -94,7 +96,7 @@ int ed_buf_open(Editor *e, const char *path) {
b.arena = arena_init(1L << 30);
b.txt = FREELIST_NEW(&e->txt_free, &b.arena);
if (path) {
- b.path = str_dup(str_from_cstr(path), &b.arena);
+ b.path = str_dup(paths, &b.arena);
b.type = ED_BUF_FILE;
txt_load(b.txt, path);
} else {
@@ -452,6 +454,14 @@ void find_view_window(TxtLoc l, TxtLoc *start, TxtLoc *end, u32 lines) {
*end = b;
}
+static Str basename(Str s) {
+ for (;;) {
+ Cut c = str_cut(s, '/');
+ if (!c.tail.n) return c.head;
+ s = c.tail;
+ }
+}
+
void draw_buf(EditBuf *eb) {
int lmarg = 0;
int x = lmarg, y = 0;
@@ -471,7 +481,8 @@ void draw_buf(EditBuf *eb) {
for (u32 i = 0; i < e.bufn; i++) {
EditBuf *b = &e.buf[i];
VuiAttr a = i == e.bufi ? sel : norm;
- x += vui_aprintf(x, y, a, " %.*s", (int)b->path.n, b->path.s);
+ Str p = basename(b->path);
+ x += vui_aprintf(x, y, a, " %.*s", (int)p.n, p.s);
if (b->type == ED_BUF_FILE && b->txt->dirty) x += vui_putsa(x, y, "* ", a);
else vui_chra(x++, y, ' ', a);
}
@@ -1163,8 +1174,11 @@ int select_opt(Str *optv, u32 optc, Str prompt) {
Str select_file_in(const char *path) {
DIR *d = opendir(path);
if (!d) return (Str) { 0, 0 };
- struct dirent *de;
DYNARR(Str) opt = { 0 };
+ struct dirent *de;
+
+again:
+ opt.n = 0;
DA_APUSH(&opt, &e.scratch, S(".."));
while ((de = readdir(d))) {
if (de->d_name[0] == '.') continue;
@@ -1173,7 +1187,16 @@ Str select_file_in(const char *path) {
}
int o = select_opt(opt.v, opt.n, S("File: "));
if (o == -1) return (Str) { 0, 0 };
- return opt.v[o];
+
+ Str s = str_from_cstr(path);
+ str_catc(&s, '/', &e.scratch);
+ str_cat(&s, opt.v[o], &e.scratch);
+ s = normalize_path(s);
+
+ path = str_to_cstr(s, &e.scratch);
+ d = opendir(path);
+ if (d) goto again;
+ return s;
}
Str select_file(void) {
@@ -1181,6 +1204,16 @@ Str select_file(void) {
return s;
}
+int select_buf(void) {
+ DYNARR(Str) opt = { 0 };
+ for (u32 i = 0; i < e.bufn; i++) {
+ DA_APUSH(&opt, &e.scratch, e.buf[i].path);
+ }
+ int o = select_opt(opt.v, opt.c, S("Buffer: "));
+ if (o == -1) return -1;
+ return o;
+}
+
int main(int argc, const char **argv) {
ed_init(&e);
@@ -1348,6 +1381,10 @@ int main(int argc, const char **argv) {
e.bufi = ed_buf_open(&e, str_to_cstr(s, &e.scratch));
}
} break;
+ case 'b': {
+ int b = select_buf();
+ if (b != -1) e.bufi = b;
+ } break;
case 'm':
build_file(eb->path, 1);
break;