package buffer import ( "citrons.xyz/talk/tui" ) type Buffer struct { top *List bottom *List scroll tui.ScrollState Closed bool } type Message interface { Id() string Show(odd bool) } type List struct { msg Message odd bool prev *List next *List } func (l List) Prev() *List { return l.prev } func (l List) Next() *List { return l.next } func (l List) IsOdd() bool { return l.odd } func (l List) Msg() Message { return l.msg } func (b *Buffer) Top() *List { return b.top } func (b *Buffer) Bottom() *List { return b.bottom } func (b *Buffer) Add(msg Message) { l := &List {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 := List {msg: msg} if b.top != nil { b.top.prev = &l l.next = b.top l.odd = !b.top.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) ScrollPos() int { return b.scroll.Get() } func (b *Buffer) AtBottom() bool { return b.scroll.AtFirst() } func (b *Buffer) AtTop() bool { return b.scroll.AtLast() } func (b *Buffer) Show(id string) (atTop bool) { mouse := tui.Push(id, tui.Box { Width: tui.Fill, Height: tui.Fill, Dir: tui.Up, Overflow: true, Style: &tui.Style {Fg: tui.White, Bg: 232}, Scroll: &b.scroll, }) b.scroll.ByMouse(mouse, true) defer tui.Pop() for m := b.bottom; m != nil; m = m.prev { var bg int32 = 232 if m.odd { bg = 233 } 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 b.scroll.AtLast() }