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 }