summaryrefslogtreecommitdiff
path: root/client/cmd_window.go
blob: 261b550a1ae66d5962ec30af4fbd1698687e029e (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main

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

type cmdWindowLocation struct {}

type cmdWindow struct {
	buf buffer.Buffer
	input tui.TextInput
	login bool
}

type logMsg struct {
	index int
	text string
	logType logType
}
var lastIndex = 0

type logType int
const (
	logInfo = iota
	logErr
	logCmd
)

func (m logMsg) Id() string {
	return fmt.Sprintf("log.%d", m.index)
}

func (m logMsg) Show(odd bool) {
	var style *tui.Style
	switch m.logType {
	case logErr:
		style = &tui.Style {Bg: colorErr[odd], Fg: tui.White}
	case logCmd:
		style = &tui.Style {Bg: colorCmd[odd], Fg: tui.White}
	}

	tui.Push("", tui.Box {
		Width: tui.Fill, Height: tui.Children, Style: style, Dir: tui.Right,
	})

	tui.Push("", tui.Box {Width: tui.TextSize, Height: tui.TextSize})
	tui.Text("* ", nil)
	tui.Pop()

	tui.Push("", tui.Box {Width: tui.Fill, Height: tui.TextSize})
	tui.Text(m.text, nil)
	tui.Pop()

	tui.Pop()
}

func (l cmdWindowLocation) CreateWindow() window.Window {
	return &globalApp.cmdWindow
}

func (w *cmdWindow) Location() window.Location {
	return cmdWindowLocation {}
}

func (w *cmdWindow) Kill() {}

func (w *cmdWindow) Buffer() *buffer.Buffer {
	return &w.buf
}

func (w *cmdWindow) Input() *tui.TextInput {
	return &w.input
}

func (w *cmdWindow) Send(text string) {
	if w.login {
		w.login = false
		previousText := w.input.Text()
		w.input.SetText("")
		globalApp.auth(text, func(success bool) {
			if !success {
				w.loginMode()
				w.input.SetText(previousText)
			}
		})
	}
	if text == ":wq" {
		globalApp.quit = true
	}
}

func (w *cmdWindow) ShowStatusLine() {
	if !w.login {
		tui.Text("command window", &tui.Style {
			Bg: tui.White, Fg: tui.Black, Italic: true,
		})
	} else {
		tui.Text("[", nil)
		tui.Text("login", &tui.Style {
			Bg: tui.White, Fg: tui.Blue, Bold: true,
		})
		tui.Text("]", nil)
		tui.Text(" username:", nil)
	}
}

func (w *cmdWindow) showPreview() {
	bottom := w.buf.Bottom()
	if bottom == nil {
		return
	}
	msg := bottom.Msg()

	tui.Push("command window container", tui.Box {
		Width: tui.Fill, Height: tui.Children,
	})

	tui.Push(msg.Id(), tui.Box {Width: tui.Fill, Height: tui.Children})
	msg.Show(false)
	tui.Pop()

	tui.Push("command window border", tui.Box {
		Width: tui.Fill, Height: 1, Dir: tui.Left,
		Style: &tui.Style {Bg: tui.White, Fg: tui.Black},
	})

	tui.Push("", tui.Box {Width: tui.Fill, Height: 1})
	tui.Pop()

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

	tui.Pop()

	tui.Pop()
}

func (w *cmdWindow) loginMode() {
	w.login = true
	w.input.SetText("")
}

func (w *cmdWindow) info(f string, a ...any) {
	lastIndex++
	w.buf.Add(logMsg {lastIndex, fmt.Sprintf(f, a...), logInfo})
}

func (w *cmdWindow) err(f string, a ...any) {
	lastIndex++
	w.buf.Add(logMsg {lastIndex, fmt.Sprintf(f, a...), logErr})
}

func (w *cmdWindow) cmd(f string, a ...any) {
	lastIndex++
	w.buf.Add(logMsg {lastIndex, fmt.Sprintf(f, a...), logCmd})
}