summaryrefslogtreecommitdiff
path: root/client/ui.go
blob: 740cc541e530429339ff01c3ad919d377b6efe33 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main

import (
	"citrons.xyz/talk/client/window"
	"citrons.xyz/talk/tui"
	"zgo.at/termfo/keys"
	"os"
)

func (a *application) getWin() window.Window {
	win := a.windowCache.Open(a.currentWindow)
	if win == nil {
		return emptyWindow {a.currentWindow}
	}
	return win
}

func (a *application) pushPrompt(p window.Prompt) {
	a.prompts = append(a.prompts, p)
}

func (a *application) removePrompt(p window.Prompt) {
	for i := len(a.prompts) - 1; i >= 0; i-- {
		if i < len(a.prompts) - 1 {
			a.prompts = append(a.prompts[:i], a.prompts[i + 1:]...)
		} else {
			a.prompts = a.prompts[:i]
		}
	}
}

func (a *application) getPrompt() window.Prompt {
	if len(a.prompts) > 0 {
		return a.prompts[len(a.prompts) - 1]
	}
	return a.getWin()
}

func (a *application) onInput(ev tui.Event) {
	tui.Selected = "input"

	win := a.getWin()
	prompt := a.getPrompt()
	prompt.Input().Update(ev)

	buf := win.Buffer()
	buf.Scroll(-ev.Mouse.Scroll * 2)
	scroll := tui.Size().Height - 5
	switch ev.Key {
	case keys.PageUp:
		buf.Scroll(scroll)
	case keys.PageDown:
		buf.Scroll(-scroll)
	case keys.Enter:
		input := prompt.Input()
		if !input.IsEmpty() {
			is, text := isCommand(input.Text())
			if !is {
				prompt.Send(text)
			} else {
				a.processCommand(text)
				input.SetText("")
			}
		}
	case '0' | keys.Alt:
		a.currentWindow = cmdWindowLocation {}
	}

}

func (a *application) showNickBox() {
	tui.Push("username", tui.Box {Width: tui.TextSize, Height: tui.TextSize})
	tui.Text("[", nil)
	name := a.getNick()
	name = string([]rune(name)[:8])
	tui.Text(name, nil)
	tui.Text("] ", nil)
	tui.Pop()
}

func (a *application) show() {
	tui.Clear()
	s := tui.Size()
	tui.Push("", tui.Box {
		Width: tui.BoxSize(s.Width), Height: tui.BoxSize(s.Height),
		Style: &tui.Style {Fg: tui.White, Bg: colorDefault[false]},
	})

	if a.currentWindow != (cmdWindowLocation{}) {
		a.cmdWindow.showPreview()
	}

	win := a.getWin()
	prompt := a.getPrompt()

	a.getWin().Buffer().Show("buffer")
	tui.Push("input border", tui.Box {
		Width: tui.Fill, Height: 1, Dir: tui.Left,
		Style: &tui.Style {Bg: tui.White, Fg: tui.Black},
	})

	if win.Buffer().ScrollPos() > 0 {
		tui.Push("", tui.Box {Width: tui.TextSize, Height: 1, NoWrap: true})
		if win.Buffer().AtTop() {
			tui.Text("[TOP]", nil)
		} else {
			tui.Text("[MORE]", nil)
		}
		tui.Pop()
	}
	tui.Push("", tui.Box {Width: tui.Fill, Height: 1})
	tui.Pop()

	tui.Push("status", tui.Box {Width: tui.TextSize, Height: 1, NoWrap: true})
	prompt.ShowStatusLine()
	tui.Pop()

	tui.Pop()

	tui.Push("input container", tui.Box {
		Width: tui.Fill, Height: tui.Children, Dir: tui.Right,
	})
	a.showNickBox()
	prompt.Input().Show("input")
	tui.Pop()

	tui.Pop()
	tui.DrawLayout()
	if tui.Present() != nil {
		os.Exit(-1)
	}
}