summaryrefslogtreecommitdiff
path: root/client/channel_list.go
blob: 10b07f185f64903b36d9e8d0acdf56c2c6bab825 (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
package main

import (
	"citrons.xyz/talk/proto"
	"citrons.xyz/talk/tui"
	"sort"
)

type channelList []channelListEntry

type channelListEntry struct {
	name string
	location channelLocation
}

func (cl *channelList) Len() int {
	return len(*cl)
}

func (cl *channelList) Less(i, j int) bool {
	return (*cl)[i].name < (*cl)[j].name
}

func (cl *channelList) Swap(i, j int) {
	x := (*cl)[i]
	(*cl)[i] = (*cl)[j]
	(*cl)[j] = x
}

func (cl *channelList) setChannels(cs []proto.Object) {
	*cl = nil
	for _, c := range cs {
		*cl = append(*cl, channelListEntry {
			c.Fields[""], channelLocation {c.Id},
		})
	}
	sort.Sort(cl)
}

func (cl *channelList) remove(location channelLocation) {
	for i := len(*cl) - 1; i >= 0; i-- {
		if (*cl)[i].location != location {
			continue
		}
		if i < len(*cl) - 1 {
			*cl = append((*cl)[:i], (*cl)[i + 1:]...)
		} else {
			*cl = (*cl)[:i]
		}
		break
	}
}

func (cl *channelList) traverse(direction int) {
	if len(*cl) == 0 {
		return
	}

	var i int
	for i = 0; i < len(*cl); i++ {
		if (*cl)[i].location == globalApp.currentWindow {
			break
		}
	}
	if i == len(*cl) {
		i = 0
	} else {
		i += direction
		i %= len(*cl)
		if i < 0 {
			i = len(*cl) + i 
		}
	}
	globalApp.goTo((*cl)[i].location)
}

func (cl *channelList) add(name string, location channelLocation) {
	cl.remove(location)
	entry := channelListEntry {name, location}
	*cl = append([]channelListEntry {entry}, *cl...)
}

func (cl *channelList) show() {
	tui.Push("channel list", tui.Box {Width: 12, Height: tui.Fill})
	for i, entry := range *cl {
		var style *tui.Style
		if entry.location == globalApp.currentWindow {
			style = &tui.Style {Fg: tui.Black, Bg: tui.White}
		}

		tui.Push("channel list." + entry.location.id, tui.Box {
			Width: tui.Fill, Height: 1, NoWrap: true, Style: style,
		})
		ch := globalApp.cache.Get(entry.location.id)
		if ch != nil {
			entry.name = ch.Fields[""]
			(*cl)[i] = entry
		}
		tui.Text(entry.name, nil)
		tui.Pop()
	}
	tui.Pop()
}