-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
167 lines (139 loc) · 2.74 KB
/
input.go
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
163
164
165
166
167
package main
import (
"strings"
"github.com/jacokoo/fff/model"
"github.com/jacokoo/fff/ui"
"github.com/mattn/go-runewidth"
)
var (
input = make(chan rune)
inputQuit = make(chan bool)
inputer Inputer
)
// Inputer for input
type Inputer interface {
Name() string
Get() string
Append(ch rune)
Delete() bool
End(bool)
}
type nameInputer struct {
title string
name string
action func(string)
}
func deleteLastChar(str string) (string, int) {
chs := []rune(str)
ch := chs[len(chs)-1]
width := runewidth.RuneWidth(ch)
return string(chs[:len(chs)-1]), width
}
func newNameInput(title string, action func(string)) *nameInputer {
return &nameInputer{title, "", action}
}
func (n *nameInputer) Name() string {
return n.title
}
func (n *nameInputer) Get() string {
return n.name
}
func (n *nameInputer) Append(ch rune) {
n.name += string(ch)
}
func (n *nameInputer) Delete() bool {
if len(n.name) == 0 {
return false
}
nn, _ := deleteLastChar(n.name)
n.name = nn
return true
}
func (n *nameInputer) End(abort bool) {
if len(n.name) == 0 {
return
}
if !abort {
go n.action(n.name)
}
n.name = ""
}
type columnInputer struct {
model.Column
}
func (co *columnInputer) Name() string {
return "FILTER"
}
func (co *columnInputer) Get() string {
return co.Filter()
}
func (co *columnInputer) Append(ch rune) {
fi := co.Filter() + string(ch)
co.SetFilter(fi)
co.Update()
ui.ColumnContentChangeEvent.Send(co.Column)
}
func (co *columnInputer) Delete() bool {
f := co.Filter()
if len(f) == 0 {
return false
}
nn, _ := deleteLastChar(f)
co.SetFilter(nn)
co.Update()
ui.ColumnContentChangeEvent.Send(co.Column)
return true
}
func (co *columnInputer) End(abort bool) {
}
func handleInputKey() {
for {
select {
case ch := <-input:
inputer.Append(ch)
ui.InputChangeEvent.Send([]string{inputer.Name(), inputer.Get()})
case <-inputQuit:
return
}
}
}
func enterInputMode(in Inputer) {
changeMode(ModeInput)
inputer = in
ui.InputChangeEvent.Send([]string{inputer.Name(), inputer.Get()})
go handleInputKey()
}
func quitInputMode(abort bool) {
inputer.End(abort)
inputer = nil
ui.QuitInputEvent.Send(wo.CurrentGroup().Current())
inputQuit <- true
changeMode(ModeNormal)
}
func inputDelete() {
b := inputer.Delete()
if !b {
quitInputMode(true)
return
}
ui.InputChangeEvent.Send([]string{inputer.Name(), inputer.Get()})
}
type requestHandler struct {
isPassword bool
*nameInputer
}
func (rh *requestHandler) Get() string {
if rh.isPassword {
return strings.Repeat("*", len(rh.name))
}
return rh.name
}
func handleUserRequest() {
for {
req := <-model.RequestCh
rh := &requestHandler{req.IsPassword, &nameInputer{req.Title, "", func(end string) {
model.ResponseCh <- end
}}}
enterInputMode(rh)
}
}