forked from rizinorg/rizin-notebook
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch_config.go
More file actions
167 lines (151 loc) · 4.78 KB
/
search_config.go
File metadata and controls
167 lines (151 loc) · 4.78 KB
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 (
"encoding/json"
"html/template"
"strconv"
"strings"
)
const (
notebookSearchSettingsPrefix = "ns:"
defaultSearchMaxResults = 200
minSearchMaxResults = 10
maxSearchMaxResults = 500
)
type notebookSearchConfig struct {
MaxResults int `json:"max_results"`
DefaultMode NotebookSearchMode `json:"default_mode"`
Surface NotebookSearchSurfaceFilter `json:"default_surface"`
CellType NotebookSearchCellTypeFilter `json:"default_cell_type"`
}
var notebookSearchSettingOrder = []string{
"max_results",
"default_mode",
"default_surface",
"default_cell_type",
}
var notebookSearchSettingLabels = map[string]string{
"max_results": "Max Results",
"default_mode": "Default Mode",
"default_surface": "Default Surface",
"default_cell_type": "Default Cell Type",
}
var notebookSearchSettingDefaults = map[string]string{
"max_results": strconv.Itoa(defaultSearchMaxResults),
"default_mode": string(NotebookSearchModeLiteral),
"default_surface": string(NotebookSearchSurfaceFilterAll),
"default_cell_type": string(NotebookSearchCellTypeFilterAll),
}
var notebookSearchSettingKinds = map[string]string{
"max_results": "number",
"default_mode": "select",
"default_surface": "select",
"default_cell_type": "select",
}
var notebookSearchSettingChoices = map[string][]map[string]string{
"default_mode": {
{"value": string(NotebookSearchModeLiteral), "label": "Literal"},
{"value": string(NotebookSearchModeRegex), "label": "Regex"},
{"value": string(NotebookSearchModeFuzzy), "label": "Fuzzy"},
},
"default_surface": {
{"value": string(NotebookSearchSurfaceFilterAll), "label": "All Surfaces"},
{"value": string(NotebookSearchSurfaceFilterContent), "label": "Input / Source"},
{"value": string(NotebookSearchSurfaceFilterOutput), "label": "Outputs"},
},
"default_cell_type": {
{"value": string(NotebookSearchCellTypeFilterAll), "label": "All Cell Types"},
{"value": string(NotebookSearchCellTypeFilterCommand), "label": "Command Cells"},
{"value": string(NotebookSearchCellTypeFilterScript), "label": "Script Cells"},
{"value": string(NotebookSearchCellTypeFilterMarkdown), "label": "Markdown Cells"},
},
}
func getNotebookSearchConfig() notebookSearchConfig {
cfg := notebookSearchConfig{
MaxResults: defaultSearchMaxResults,
DefaultMode: NotebookSearchModeLiteral,
Surface: NotebookSearchSurfaceFilterAll,
CellType: NotebookSearchCellTypeFilterAll,
}
if catalog == nil {
return cfg
}
settings, _ := catalog.GetAllSettings()
if settings == nil {
return cfg
}
for key, value := range settings {
if !strings.HasPrefix(key, notebookSearchSettingsPrefix) {
continue
}
settingKey := strings.TrimPrefix(key, notebookSearchSettingsPrefix)
switch settingKey {
case "max_results":
val, err := strconv.Atoi(value)
if err == nil {
cfg.MaxResults = clampInt(val, minSearchMaxResults, maxSearchMaxResults)
}
case "default_mode":
mode, err := normalizeNotebookSearchMode(NotebookSearchMode(value))
if err == nil {
cfg.DefaultMode = mode
}
case "default_surface":
surface, err := normalizeNotebookSearchSurfaceFilter(NotebookSearchSurfaceFilter(value))
if err == nil {
cfg.Surface = surface
}
case "default_cell_type":
cellType, err := normalizeNotebookSearchCellTypeFilter(NotebookSearchCellTypeFilter(value))
if err == nil {
cfg.CellType = cellType
}
}
}
return cfg
}
func getNotebookSearchConfigJSON() template.JS {
cfg := getNotebookSearchConfig()
bytes, err := json.Marshal(cfg)
if err != nil {
return template.JS("{}")
}
return template.JS(bytes)
}
func notebookSearchActions() []map[string]string {
cfg := getNotebookSearchConfig()
actions := make([]map[string]string, 0, len(notebookSearchSettingOrder))
for _, key := range notebookSearchSettingOrder {
value := notebookSearchSettingDefaults[key]
switch key {
case "max_results":
value = strconv.Itoa(cfg.MaxResults)
case "default_mode":
value = string(cfg.DefaultMode)
case "default_surface":
value = string(cfg.Surface)
case "default_cell_type":
value = string(cfg.CellType)
}
actions = append(actions, map[string]string{
"name": key,
"label": notebookSearchSettingLabels[key],
"value": value,
"value_display": notebookSearchSettingValueLabel(key, value),
"default_value": notebookSearchSettingDefaults[key],
"default_display": notebookSearchSettingValueLabel(key, notebookSearchSettingDefaults[key]),
})
}
return actions
}
func notebookSearchSettingValueLabel(key, value string) string {
choices, ok := notebookSearchSettingChoices[key]
if !ok {
return value
}
for _, choice := range choices {
if choice["value"] == value {
return choice["label"]
}
}
return value
}