forked from mop-tracker/mop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_editor.go
100 lines (81 loc) · 3.02 KB
/
column_editor.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
// Copyright (c) 2013-2016 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
package mop
import `github.com/michaeldv/termbox-go`
// ColumnEditor handles column sort order. When activated it highlights
// current column name in the header, then waits for arrow keys (choose
// another column), Enter (reverse sort order), or Esc (exit).
type ColumnEditor struct {
screen *Screen // Pointer to Screen so we could use screen.Draw().
quotes *Quotes // Pointer to Quotes to redraw them when the sort order changes.
layout *Layout // Pointer to Layout to redraw stock quotes header.
profile *Profile // Pointer to Profile where we save newly selected sort order.
}
// Returns new initialized ColumnEditor struct. As part of initialization it
// highlights current column name (as stored in Profile).
func NewColumnEditor(screen *Screen, quotes *Quotes) *ColumnEditor {
editor := &ColumnEditor{
screen: screen,
quotes: quotes,
layout: screen.layout,
profile: quotes.profile,
}
editor.selectCurrentColumn()
return editor
}
// Handle takes over the keyboard events and dispatches them to appropriate
// column editor handlers. It returns true when user presses Esc.
func (editor *ColumnEditor) Handle(event termbox.Event) bool {
defer editor.redrawHeader()
switch event.Key {
case termbox.KeyEsc:
return editor.done()
case termbox.KeyEnter:
editor.execute()
case termbox.KeyArrowLeft:
editor.selectLeftColumn()
case termbox.KeyArrowRight:
editor.selectRightColumn()
}
return false
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) selectCurrentColumn() *ColumnEditor {
editor.profile.selectedColumn = editor.profile.SortColumn
editor.redrawHeader()
return editor
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) selectLeftColumn() *ColumnEditor {
editor.profile.selectedColumn--
if editor.profile.selectedColumn < 0 {
editor.profile.selectedColumn = editor.layout.TotalColumns() - 1
}
return editor
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) selectRightColumn() *ColumnEditor {
editor.profile.selectedColumn++
if editor.profile.selectedColumn > editor.layout.TotalColumns()-1 {
editor.profile.selectedColumn = 0
}
return editor
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) execute() *ColumnEditor {
if editor.profile.Reorder() == nil {
editor.screen.Draw(editor.quotes)
}
return editor
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) done() bool {
editor.profile.selectedColumn = -1
return true
}
//-----------------------------------------------------------------------------
func (editor *ColumnEditor) redrawHeader() {
editor.screen.DrawLine(0, 4, editor.layout.Header(editor.profile))
termbox.Flush()
}