Commit: a6b8f7b4e0ea79ec2b58cbb746bfde2993011efa
Parent: 7041d1c39b7230d118a70270536b218fc5ba67c4
Author: Randy Palamar
Date: Sun, 18 Aug 2024 14:08:08 -0600
adjust in progress line start and end when committing to ringbuf
Diffstat:
4 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/os_unix.c b/os_unix.c
@@ -259,8 +259,9 @@ os_child_exited(os_child c)
}
static size
-os_read_from_child(os_child c, RingBuf *rb, size unprocessed_bytes)
+os_read_from_child(os_child c, TermView *tv, size unprocessed_bytes)
{
+ RingBuf *rb = &tv->log;
size r = 0, total_bytes_read = 0, remaining = rb->cap - unprocessed_bytes;
ASSERT(rb->widx < rb->cap);
@@ -271,7 +272,7 @@ os_read_from_child(os_child c, RingBuf *rb, size unprocessed_bytes)
} while (r != -1);
ASSERT(total_bytes_read <= rb->cap);
- commit_to_rb(rb, total_bytes_read);
+ commit_to_rb(tv, total_bytes_read);
return total_bytes_read;
}
diff --git a/terminal.c b/terminal.c
@@ -85,16 +85,9 @@ line_length(Line *l)
return l->end - l->start;
}
-/* NOTE: this will adjust the starting so that the line looks like a linear array */
static s8
line_to_s8(Line *l, RingBuf *rb)
{
- if (l->end < l->start) {
- if (l->start >= rb->buf) l->start -= rb->cap;
- else l->end += rb->cap;
- ASSERT(l->end < rb->buf + 2 * rb->cap && l->end > rb->buf - rb->cap);
- ASSERT(l->start < rb->buf + 2 * rb->cap && l->start > rb->buf - rb->cap);
- }
ASSERT(l->start <= l->end);
s8 result = {.len = l->end - l->start, .data = l->start};
@@ -789,8 +782,8 @@ split_raw_input_to_lines(Term *t, s8 raw)
TermView *nv = t->views + t->view_idx;
size nstart = nv->log.widx;
mem_copy(raw, (s8){nv->log.cap, nv->log.buf + nstart});
- commit_to_rb(&tv->log, -raw.len);
- commit_to_rb(&nv->log, raw.len);
+ commit_to_rb(tv, -raw.len);
+ commit_to_rb(nv, raw.len);
raw.data = nv->log.buf + nstart;
init_line(nv->lines.buf + nv->lines.widx, raw.data, t->cursor.state);
tv = nv;
diff --git a/util.c b/util.c
@@ -47,20 +47,27 @@ alloc_(Arena *a, size len, size align, size count)
return mem_clear(p, 0, count * len);
}
+/* NOTE: This performs wrapping of the ring buffer as needed; since a line could be in
+ * progress this must also adjust the start and end of the current line */
static void
-commit_to_rb(RingBuf *rb, size len)
+commit_to_rb(TermView *tv, size len)
{
- ASSERT(len <= rb->cap);
+ ASSERT(len <= tv->log.cap);
- rb->widx += len;
- rb->filled += len;
+ tv->log.widx += len;
+ tv->log.filled += len;
- CLAMP(rb->filled, 0, rb->cap);
- if (rb->widx >= rb->cap)
- rb->widx -= rb->cap;
+ CLAMP(tv->log.filled, 0, tv->log.cap);
+ if (tv->log.widx >= tv->log.cap) {
+ tv->log.widx -= tv->log.cap;
- ASSERT(rb->filled >= 0);
- ASSERT(rb->widx >= 0 && rb->widx < rb->cap);
+ size line = tv->lines.widx;
+ tv->lines.buf[line].start -= tv->log.cap;
+ tv->lines.buf[line].end -= tv->log.cap;
+ }
+
+ ASSERT(tv->log.filled >= 0);
+ ASSERT(tv->log.widx >= 0 && tv->log.widx < tv->log.cap);
}
static void
diff --git a/vtgl.c b/vtgl.c
@@ -424,7 +424,8 @@ do_terminal(Term *t, Arena a)
glfwSetWindowShouldClose(t->gl.window, GL_TRUE);
return;
}
- t->unprocessed_bytes += os_read_from_child(t->child, rb, t->unprocessed_bytes);
+ t->unprocessed_bytes += os_read_from_child(t->child, t->views + t->view_idx,
+ t->unprocessed_bytes);
s8 raw = {
.len = t->unprocessed_bytes,
.data = rb->buf + (rb->widx - t->unprocessed_bytes)