summaryrefslogtreecommitdiff
path: root/client/buffer/buffer.go
blob: 921ad001d8482aee3fdd0ae2f680eb9f88fd6119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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) 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) AtBottom() bool {
	return b.scroll.AtFirst()
}

func (b *Buffer) AtTop() bool {
	return b.scroll.AtLast()
}

func (b *Buffer) Show(id string) (atTop bool) {
	tui.Push(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 = 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()
}