summaryrefslogtreecommitdiff
path: root/client/buffer
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2025-05-30 17:47:35 -0500
committercitrons <citrons@mondecitronne.com>2025-05-30 17:47:35 -0500
commitb3a01b28cbfe43ef22168a2f81803dccd4e56864 (patch)
tree042a79f70fe1b6f08bc3ba6b0ea2dde127b1a1be /client/buffer
parent194c63381a8b19f6a9198ff30a307b65acc2af76 (diff)
simple client
Diffstat (limited to 'client/buffer')
-rw-r--r--client/buffer/buffer.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/client/buffer/buffer.go b/client/buffer/buffer.go
new file mode 100644
index 0000000..bb782fb
--- /dev/null
+++ b/client/buffer/buffer.go
@@ -0,0 +1,94 @@
+package buffer
+
+import (
+ "citrons.xyz/talk/tui"
+)
+
+type Buffer struct {
+ id string
+ top *bufList
+ bottom *bufList
+ scroll tui.ScrollState
+ Closed bool
+}
+
+type Message interface {
+ Id() string
+ Show(odd bool)
+}
+
+type bufList struct {
+ msg Message
+ odd bool
+ prev *bufList
+ next *bufList
+}
+
+func New(id string) Buffer {
+ return Buffer {id: id}
+}
+
+func (b *Buffer) Add(msg Message) {
+ l := &bufList {msg: msg}
+ if b.bottom != nil {
+ b.bottom.next = l
+ l.prev = b.bottom
+ l.odd = !b.bottom.odd
+ }
+ b.bottom = l
+ if b.top == nil {
+ b.top = b.bottom
+ }
+}
+
+func (b *Buffer) AddTop(msg Message) {
+ l := bufList {msg: msg}
+ if b.top != nil {
+ b.top.prev = &l
+ l.next = b.top
+ l.odd = !b.bottom.odd
+ }
+ b.top = &l
+ if b.bottom == nil {
+ b.bottom = b.top
+ }
+}
+
+func (b *Buffer) Scroll(amnt int) {
+ b.scroll.Scroll(amnt)
+}
+
+func (b *Buffer) ScrollBottom() {
+ b.scroll.ToStart()
+}
+
+func (b *Buffer) AtBottom() bool {
+ return b.scroll.AtFirst()
+}
+
+func (b *Buffer) AtTop() bool {
+ return b.scroll.AtLast()
+}
+
+func (b *Buffer) Show() (atTop bool) {
+ tui.Push(b.id, tui.Box {
+ Width: tui.Fill, Height: tui.Fill, Dir: tui.Up, Overflow: true,
+ Scroll: &b.scroll,
+ })
+ defer tui.Pop()
+
+ for m := b.bottom; m != nil; m = m.prev {
+ var bg int32 = tui.Black
+ if m.odd {
+ bg = 236
+ }
+ tui.Push(m.msg.Id(), tui.Box {
+ Width: tui.Fill, Height: tui.Children,
+ Style: &tui.Style {Fg: tui.White, Bg: bg},
+ })
+ m.msg.Show(m.odd)
+ tui.Pop()
+ }
+
+ return atTop
+}