summaryrefslogtreecommitdiff
path: root/client/window/window.go
blob: ba7202613547dae6e331e9f1c2b1b49388e66c44 (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
package window

import (
	"citrons.xyz/talk/client/buffer"
	"citrons.xyz/talk/tui"
)

type Location interface {
	CreateWindow() Window
}

type Window interface {
	Prompt
	Location() Location
	Kill()
	Buffer() *buffer.Buffer
}

type Prompt interface {
	Input() *tui.TextInput
	Send(text string)
	ShowStatusLine()
}

type WindowCache struct {
	windows map[Location]Window
}

func NewCache() WindowCache {
	return WindowCache {make(map[Location]Window)}
}

func (wc *WindowCache) Open(l Location) Window {
	if wc.windows[l] == nil {
		wc.windows[l] = l.CreateWindow()
	}
	return wc.windows[l]
}

func (wc *WindowCache) Evict(l Location) {
	if wc.windows[l] != nil {
		wc.windows[l].Kill()
	}
	delete(wc.windows, l)
}

func (wc *WindowCache) Get(l Location) Window {
	return wc.windows[l]
}