/
spottinglist.go
199 lines (177 loc) · 4.4 KB
/
spottinglist.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
package ui
import (
"fmt"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/nsf/termbox-go"
"github.com/tzneal/ham-go/cmd/termlog/input"
)
type SpottingList struct {
yPos int
maxLines int
maxEntries int // maximum number of dxSpots to keep
theme Theme
offset int
selected int
focused bool
controller Controller
onTune func(freq float64)
mu sync.Mutex
dxSpots []SpotRecord
expiration time.Duration
}
type SpotRecord struct {
Source string
Frequency float64
Station string
Comment string
Time time.Time
Location string
}
func NewSpottingList(yPos int, maxLines int, expiration time.Duration, theme Theme) *SpottingList {
ql := &SpottingList{
yPos: yPos,
maxLines: maxLines,
theme: theme,
expiration: expiration,
maxEntries: 100,
}
return ql
}
func (d *SpottingList) AddSpot(msg SpotRecord) {
msg.Comment = strings.TrimSpace(msg.Comment)
msg.Location = strings.TrimSpace(msg.Location)
msg.Station = strings.TrimSpace(msg.Station)
d.mu.Lock()
defer d.mu.Unlock()
updated := false
for i, s := range d.dxSpots {
// identical spot, so ignore it
if msg == s {
return
}
// same callsign, but later time
if msg.Station == s.Station {
if msg.Time.After(s.Time) {
d.dxSpots[i] = msg
updated = true
} else if msg.Time.Before(s.Time) || msg.Time == s.Time {
// older spot, so just ignore
return
}
break
}
}
if !updated {
d.dxSpots = append(d.dxSpots, msg)
}
// Sort oldest first
sort.Slice(d.dxSpots, func(i, j int) bool {
return !d.dxSpots[i].Time.After(d.dxSpots[j].Time)
})
// filter out any spots that are older than the expiration time
removeAfter := -1
expireTime := time.Now().Add(-d.expiration)
for i := 0; i < len(d.dxSpots); i++ {
if d.dxSpots[i].Time.After(expireTime) {
removeAfter = i
break
}
}
if removeAfter != -1 {
d.dxSpots = d.dxSpots[removeAfter:]
}
// possibly remove the oldest one
if len(d.dxSpots) > d.maxEntries {
copy(d.dxSpots, d.dxSpots[1:])
d.dxSpots = d.dxSpots[0 : len(d.dxSpots)-1]
}
}
func (d *SpottingList) Redraw() {
w, _ := termbox.Size()
for line := 0; line < d.maxLines-1; line++ {
idx := len(d.dxSpots) - line - 1 - d.offset
curLine := d.yPos + line
fg := termbox.ColorWhite
bg := termbox.ColorDefault
// draw selected lines differently while focused
if d.selected == d.offset+line && d.focused {
fg = termbox.ColorBlack
bg = termbox.ColorWhite
}
if idx >= 0 && idx < len(d.dxSpots) {
spot := d.dxSpots[idx]
xPos := 0
Clear(xPos, curLine, xPos+w-xPos, curLine, fg, bg)
DrawText(xPos, curLine, trunc(spot.Source, 4), fg, bg)
xPos += 5
DrawText(xPos, curLine, spot.Time.Format("15:04"), fg, bg)
xPos += 6
freqStr := strconv.FormatFloat(spot.Frequency, 'f', 2, 64)
if strings.HasSuffix(freqStr, ".00") {
freqStr = freqStr[0 : len(freqStr)-3]
}
DrawText(xPos, curLine, freqStr, fg, bg)
xPos += 10
DrawText(xPos, curLine, trunc(spot.Station, 11), fg, bg)
xPos += 12
DrawText(xPos, curLine, trunc(spot.Comment, 29), fg, bg)
xPos += 30
DrawText(xPos, curLine, spot.Location, fg, bg)
} else {
Clear(0, curLine, w-1, curLine, termbox.ColorDefault, termbox.ColorDefault)
}
}
Clear(0, d.yPos+d.maxLines-1, w, d.yPos+d.maxLines-1, d.theme.QSOListHeaderFG, d.theme.QSOListHeaderBG)
label := fmt.Sprintf("%d spots", len(d.dxSpots))
DrawText(0, d.yPos+d.maxLines-1, label, d.theme.QSOListHeaderFG, d.theme.QSOListHeaderBG)
}
func trunc(comment string, l int) string {
if len(comment) <= l {
return comment
}
return comment[0:l]
}
func (d *SpottingList) SetController(c Controller) {
d.controller = c
}
func (d *SpottingList) Focus(b bool) {
d.focused = b
if b {
termbox.HideCursor()
}
}
func (d *SpottingList) HandleEvent(key input.Key) {
switch key {
case input.KeyTab:
d.controller.FocusNext()
case input.KeyShiftTab:
d.controller.FocusPrevious()
case input.KeyEnter:
if d.selected >= 0 && d.selected < len(d.dxSpots) {
if d.onTune != nil {
d.onTune(d.dxSpots[len(d.dxSpots)-d.selected-1].Frequency / 1e3)
}
}
case input.KeyArrowUp:
if d.selected > 0 {
d.selected--
if d.selected < d.offset {
d.offset--
}
}
case input.KeyArrowDown:
if d.selected+d.offset < len(d.dxSpots)-1 {
d.selected++
if d.selected > d.offset+d.maxLines-2 {
d.offset++
}
}
}
}
func (d *SpottingList) OnTune(fn func(freq float64)) {
d.onTune = fn
}