From b3a01b28cbfe43ef22168a2f81803dccd4e56864 Mon Sep 17 00:00:00 2001 From: citrons Date: Fri, 30 May 2025 17:47:35 -0500 Subject: simple client --- client/buffer/buffer.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 client/buffer/buffer.go (limited to 'client/buffer/buffer.go') 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 +} -- cgit v1.2.3