-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
113 lines (93 loc) · 2.26 KB
/
command.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
package civ
import (
"strings"
)
func (c *Civ) ExecuteCommand() {
// Quick exit if there is no actual query
if len(c.ql.query) <= 0 {
return
}
a := strings.Fields(string(c.ql.query))
command := a[0]
vargs := a[1:]
if strings.HasPrefix("show", command) {
c.executeShowCommand(vargs)
} else if strings.HasPrefix("show_only", command) {
c.executeShowOnlyCommand(vargs)
} else if strings.HasPrefix("hide", command) {
c.executeHideCommand(vargs)
} else if strings.HasPrefix("reset", command) {
c.executeResetCommand(vargs)
} else if strings.HasPrefix("exit", command) {
c.executeExitCommand(vargs)
}
}
func (c *Civ) executeExitCommand(vargs []string) {
c.table.outputStdout = true
c.terminate = true
}
// Hide the given columns
func (c *Civ) executeHideCommand(vargs []string) {
for _, a := range vargs {
if i, ok := c.table.FindColName(a); ok {
c.table.AddDisabledCol(i)
}
}
}
// Show only the given colums by hidding other columns
func (c *Civ) executeShowOnlyCommand(vargs []string) {
var hideCols []string
for _, a := range c.table.header.cols {
show := false
for _, s := range vargs {
if a.data == s {
show = true
}
}
if !show {
hideCols = append(hideCols, a.data)
}
}
c.executeHideCommand(hideCols)
}
func (c *Civ) executeResetCommand(vargs []string) {
// Make all columsn visible
c.table.ResetDisabledCol()
// Make all rows visible
c.table.ResetVisibility()
}
func (c *Civ) executeShowCommand(vargs []string) {
for _, a := range vargs {
if i, ok := c.table.FindColName(a); ok {
c.table.RemoveDisabledCol(i)
}
}
}
func (c *Civ) executeSearchCommand() {
searchWord := string(c.ql.query)
for _r, row := range c.table.contents {
matched := false
for _c, cell := range row.cols {
if idx := strings.Index(cell.data, searchWord); idx != -1 {
c.table.SetMatched(_r, _c, idx, idx+len(searchWord))
matched = true
} else {
c.table.SetMatched(_r, _c, -1, -1)
}
}
row.hasMatched = matched
}
}
func (c *Civ) executeFilterCommand() {
searchWord := string(c.ql.query)
for _r, row := range c.table.contents {
found := false
for _, cell := range row.cols {
if idx := strings.Index(cell.data, searchWord); idx != -1 {
found = true
break
}
}
c.table.SetRowVisibility(_r, found)
}
}