-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.go
92 lines (74 loc) · 2.25 KB
/
search.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
package main
import (
"image"
"strings"
ui "github.com/gizak/termui/v3"
)
// Search manages target search functionality and rendering
type Search struct {
ui.Block
active bool
content string
}
// NewSearch constructs a Search and sets active to false and content to an empty string
func NewSearch() *Search {
return &Search{active: false, content: ""}
}
// SetActive sets the Search to be active or inactive
func (search *Search) SetActive(value bool) {
if !value {
search.content = ""
}
search.active = value
}
// AppendStringToContent appends a given string to content if the Search is active
func (search *Search) AppendStringToContent(str string) {
if search.active {
search.content += str
}
}
// Pop removes the last character of Search content if it is not empty
func (search *Search) Pop() {
var contentLength int = len(search.content)
if contentLength > 0 {
search.content = search.content[:contentLength-1]
}
}
// GetContent returns the content relative to the given maximum number of characters the search bar can render
func (search *Search) GetContent(maximum int) string {
var contentLength int = len(search.content)
if contentLength > maximum {
return search.content[contentLength-maximum:]
}
return search.content
}
// Draw creates and renders the filter string to the buffer based on content
func (search *Search) Draw(buf *ui.Buffer) {
if !search.active {
return
}
style := ui.NewStyle(ui.ColorWhite, ui.ColorClear)
cursorStyle := ui.NewStyle(ui.ColorWhite, ui.ColorWhite)
voidStyle := ui.NewStyle(ui.ColorBlack, ui.ColorBlack)
label := "Search: ["
maximumContentLength := search.Max.X - search.Min.X - len(label) - 2
if maximumContentLength <= 0 {
return
}
// Subtract 1 to take into account cursor
content := search.GetContent(maximumContentLength)
// Fill with initial content including start of search block
label += content
p := image.Pt(search.Min.X, search.Min.Y)
buf.SetString(label, style, p)
p.X += len(label)
// Render cursor
buf.SetString(" ", cursorStyle, p)
p.X++
// Potentially fill the rest of the search block
remaining := maximumContentLength - len(content)
buf.SetString(strings.Repeat(" ", remaining), voidStyle, p)
p.X += remaining
// Render end of search block
buf.SetString("]", style, p)
}