-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
242 lines (216 loc) · 5.51 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"strings"
"github.com/alexflint/go-arg"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/suyashkumar/dicom"
)
var version = "unknown"
type args struct {
Input string `arg:"positional" help:"The DICOM input file or directory"`
}
func (args) Version() string { return "Version " + version }
type EditMode int
const (
TreeMode EditMode = iota
CmdlineMode
)
func main() {
var args args
p := arg.MustParse(&args)
if args.Input == "" {
p.Fail("Missing DICOM input file or directory")
}
datasetsWithFilename, err := parseDicomFiles(args.Input)
if err != nil {
fmt.Printf("Error reading input: '%s'\n", err.Error())
return
}
// global state
searchText := ""
// create tree nodes with dicom tags
app := tview.NewApplication()
rootDir := args.Input
pages := tview.NewPages()
statusLine := tview.NewTextView()
tree := tview.NewTreeView()
tree, root := sortTreeByFilename(rootDir, tree, datasetsWithFilename[:])
collapseAllRecursive(root)
statusLine.SetText("Sort by filename")
cmdline := tview.NewInputField().SetFieldBackgroundColor(tcell.ColorBlack)
mainGrid := tview.NewGrid().
SetRows(-1, 1, 1).
SetColumns(-1).
SetBorders(true).
AddItem(tree, 0, 0, 1, 1, 0, 0, true).
AddItem(statusLine, 1, 0, 1, 1, 0, 0, false).
AddItem(cmdline, 2, 0, 1, 1, 0, 0, false)
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyRune:
switch event.Rune() {
case '/':
app.SetFocus(cmdline)
cmdline.SetText("/")
return nil
case ':':
app.SetFocus(cmdline)
cmdline.SetText(":")
return nil
case '?':
addAndShowHelpPage(pages)
return nil
}
}
return event
})
cmdline.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc:
cmdline.SetText("")
app.SetFocus(tree)
return nil
case tcell.KeyEnter:
cmdlineText := cmdline.GetText()
if strings.HasPrefix(cmdlineText, ":") {
if cmdlineText == ":q" {
app.Stop()
return nil
} else if cmdlineText == ":w" {
if len(datasetsWithFilename) == 1 {
writeDatasetToFile(datasetsWithFilename[0].dataset, "write_test_copy.dcm")
statusLine.SetText("saved to write_test_copy.dcm")
}
cmdline.SetText("")
app.SetFocus(tree)
}
if cmdlineText == ":" {
cmdline.SetText("")
app.SetFocus(tree)
return nil
}
}
if strings.HasPrefix(cmdlineText, "/") {
app.SetFocus(tree)
return nil
}
}
return event
})
cmdline.SetChangedFunc(func(text string) {
cmdlineText := text
if strings.HasPrefix(cmdlineText, "/") && len(cmdlineText) > 1 {
searchText = strings.ToLower(cmdlineText[1:])
jumpToNthFoundNode(searchText, 0, tree)
}
})
tree.SetSelectedFunc(func(node *tview.TreeNode) {
node.SetExpanded(!node.IsExpanded())
})
// key handlings
tree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
currentNode := tree.GetCurrentNode()
switch key := event.Key(); key {
case tcell.KeyCtrlSpace:
if isTagNode(currentNode) {
addAndShowTagEditingPage(pages, currentNode.GetReference().(*dicom.Element))
} else {
return event
}
case tcell.KeyCtrlD:
_, _, _, height := tree.GetInnerRect()
tree.Move(height / 2)
case tcell.KeyCtrlU:
_, _, _, height := tree.GetInnerRect()
tree.Move(-height / 2)
case tcell.KeyLeft:
if event.Modifiers() == tcell.ModShift {
moveToParent(tree)
} else {
collapseOrMoveToParent(tree)
}
case tcell.KeyRight:
if event.Modifiers() == tcell.ModShift {
moveToFirstChild(tree)
} else {
expandOrMoveToFirstChild(tree)
}
case tcell.KeyUp:
if event.Modifiers() == tcell.ModShift {
moveUpSameLevel(tree)
} else {
return event // not handled, pass on
}
case tcell.KeyDown:
if event.Modifiers() == tcell.ModShift {
moveDownSameLevel(tree)
} else {
return event // not handled, pass on
}
case tcell.KeyHome:
jumpToRoot(tree)
case tcell.KeyEnd:
jumpToLastVisibleNode(tree)
case tcell.KeyRune:
switch event.Rune() {
case '1':
tree, root = sortTreeByFilename(rootDir, tree, datasetsWithFilename[:])
collapseAllRecursive(root)
statusLine.SetText("Sort by filename")
case '2':
tree, root = sortTreeByTags(rootDir, tree, datasetsWithFilename[:], 0)
collapseAllLeaves(root)
statusLine.SetText("Sort by tag")
case '3':
tree, root = sortTreeByTags(rootDir, tree, datasetsWithFilename[:], 1)
collapseAllLeaves(root)
statusLine.SetText("Sort by tag, show only different tag values")
case 'q':
app.Stop()
case 'J':
moveDownSameLevel(tree)
case 'K':
moveUpSameLevel(tree)
case 'h':
collapseOrMoveToParent(tree)
case 'l':
expandOrMoveToFirstChild(tree)
case 'H':
moveToParent(tree)
case 'L':
moveToFirstChild(tree)
case '0', '^':
moveToFirstSibling(tree)
case '$':
moveToLastSibling(tree)
case 'e':
expandCurrentAndAllSiblings(tree)
case 'c':
collapseCurrentAndAllSiblings(tree)
case 'E':
currentNode.ExpandAll()
case 'C':
currentNode.CollapseAll()
case 'g':
jumpToRoot(tree)
case 'G':
jumpToLastVisibleNode(tree)
case 'n':
jumpToNextFoundNode(searchText, tree)
case 'N':
jumpToPrevFoundNode(searchText, tree)
default:
return event // not handled, pass on
}
default:
return event // not handled, pass on
}
return nil
})
pages.AddPage("main", mainGrid, true, true)
if err := app.SetRoot(pages, true).Run(); err != nil {
panic(err)
}
}