Skip to content

Commit 3c48b1e

Browse files
committed
Reduce complexity by breaking larger functions down
This commit reduces complexity by breaking bigger functions into smaller, and moving some state around, etc. There should be no functional change.
1 parent 6f7499f commit 3c48b1e

File tree

3 files changed

+148
-102
lines changed

3 files changed

+148
-102
lines changed

chooseui/chooseui.go

+68-43
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ type ChooseUI struct {
2121

2222
// The users' choice.
2323
chosen string
24+
25+
// app is the global application
26+
app *tview.Application
27+
28+
// list contains the global list of text-entries.
29+
list *tview.List
30+
31+
// inputField contains the global text-input field.
32+
inputField *tview.InputField
2433
}
2534

2635
// New creates a new UI, allowing the user to select from the available options.
@@ -30,81 +39,69 @@ func New(choices []string) *ChooseUI {
3039
}
3140

3241
// Choose launches our user interface.
33-
func (ui *ChooseUI) Choose() string {
42+
func (ui *ChooseUI) SetupUI() {
3443

3544
//
3645
// Create the console-GUI application.
3746
//
38-
app := tview.NewApplication()
47+
ui.app = tview.NewApplication()
3948

4049
//
4150
// Create a list to hold our files.
4251
//
43-
list := tview.NewList()
44-
list.ShowSecondaryText(false)
45-
list.SetWrapAround(false)
52+
ui.list = tview.NewList()
53+
ui.list.ShowSecondaryText(false)
54+
ui.list.SetWrapAround(false)
4655

4756
//
4857
// Add all the choices to it.
4958
//
5059
for _, entry := range ui.Choices {
51-
list.AddItem(entry, "", ' ', nil)
60+
ui.list.AddItem(entry, "", ' ', nil)
5261
}
5362

54-
//
55-
// If the user presses return in the list then choose that item.
56-
//
57-
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
58-
if event.Key() == tcell.KeyEnter {
59-
selected := list.GetCurrentItem()
60-
ui.chosen, _ = list.GetItemText(selected)
61-
app.Stop()
62-
}
63-
return event
64-
})
65-
6663
//
6764
// Create a filter input-view
6865
//
69-
inputField := tview.NewInputField().
66+
ui.inputField = tview.NewInputField().
7067
SetLabel("Filter: ").
7168
SetDoneFunc(func(key tcell.Key) {
7269
if key == tcell.KeyEnter {
7370

7471
// get the selected index
75-
selected := list.GetCurrentItem()
72+
selected := ui.list.GetCurrentItem()
7673

7774
// less than the entry count?
78-
if list.GetItemCount() > 0 {
79-
ui.chosen, _ = list.GetItemText(selected)
75+
if ui.list.GetItemCount() > 0 {
76+
ui.chosen, _ = ui.list.GetItemText(selected)
8077
}
81-
app.Stop()
78+
ui.app.Stop()
8279
}
8380
})
8481

8582
//
8683
// Setup the filter-function, to filter the list to
8784
// only matches present in the input-field
8885
//
89-
inputField.SetAutocompleteFunc(func(currentText string) (entries []string) {
86+
ui.inputField.SetAutocompleteFunc(func(currentText string) (entries []string) {
9087
// Get text
9188
input := strings.TrimSpace(currentText)
9289

9390
// empty? All items should be visible
9491
if input == "" {
95-
list.Clear()
92+
ui.list.Clear()
9693
for _, entry := range ui.Choices {
97-
list.AddItem(entry, "", ' ', nil)
94+
ui.list.AddItem(entry, "", ' ', nil)
9895
}
9996
return
10097
}
10198

10299
// Otherwise filter by input
103100
input = strings.ToLower(input)
104-
list.Clear()
101+
ui.list.Clear()
105102
for _, entry := range ui.Choices {
106103
if strings.Contains(strings.ToLower(entry), input) {
107-
list.AddItem(entry, "", ' ', nil)
104+
ui.list.AddItem(entry, "", ' ', nil)
108105
}
109106
}
110107

@@ -120,64 +117,92 @@ func (ui *ChooseUI) Choose() string {
120117
// Create a layout grid, add the filter-box and the list.
121118
//
122119
grid := tview.NewFlex().SetFullScreen(true).SetDirection(tview.FlexRow)
123-
grid.AddItem(inputField, 1, 0, true)
124-
grid.AddItem(list, 0, 1, false)
120+
grid.AddItem(ui.inputField, 1, 0, true)
121+
grid.AddItem(ui.list, 0, 1, false)
125122
grid.AddItem(help, 2, 1, false)
126123

124+
ui.app.SetRoot(grid, true).SetFocus(grid).EnableMouse(true)
125+
126+
}
127+
128+
func (ui *ChooseUI) SetupKeyBinding() {
129+
130+
//
131+
// If the user presses return in the list then choose that item.
132+
//
133+
ui.list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
134+
if event.Key() == tcell.KeyEnter {
135+
selected := ui.list.GetCurrentItem()
136+
ui.chosen, _ = ui.list.GetItemText(selected)
137+
ui.app.Stop()
138+
}
139+
return event
140+
})
141+
127142
//
128143
// Global keyboard handler, use "TAB" to switch focus.
129144
//
130145
// Arrows and HOME/END work as expected regardless of focus-state
131146
//
132-
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
147+
ui.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
133148
switch event.Key() {
134149

135150
// Home
136151
case tcell.KeyHome:
137-
list.SetCurrentItem(0)
152+
ui.list.SetCurrentItem(0)
138153

139154
// End
140155
case tcell.KeyEnd:
141-
list.SetCurrentItem(list.GetItemCount())
156+
ui.list.SetCurrentItem(ui.list.GetItemCount())
142157

143158
// Up arrow
144159
case tcell.KeyUp:
145-
selected := list.GetCurrentItem()
160+
selected := ui.list.GetCurrentItem()
146161
if selected > 0 {
147162
selected--
148163
} else {
149-
selected = list.GetItemCount()
164+
selected = ui.list.GetItemCount()
150165
}
151-
list.SetCurrentItem(selected)
166+
ui.list.SetCurrentItem(selected)
152167
return nil
153168

154169
// Down arrow
155170
case tcell.KeyDown:
156-
selected := list.GetCurrentItem()
171+
selected := ui.list.GetCurrentItem()
157172
selected++
158-
list.SetCurrentItem(selected)
173+
ui.list.SetCurrentItem(selected)
159174
return nil
160175

161176
// TAB
162177
case tcell.KeyTab, tcell.KeyBacktab:
163-
if list.HasFocus() {
164-
app.SetFocus(inputField)
178+
if ui.list.HasFocus() {
179+
ui.app.SetFocus(ui.inputField)
165180
} else {
166-
app.SetFocus(list)
181+
ui.app.SetFocus(ui.list)
167182
}
168183
return nil
169184

170185
// Escape
171186
case tcell.KeyEscape:
172-
app.Stop()
187+
ui.app.Stop()
173188
}
174189
return event
175190
})
176191

192+
}
193+
194+
// Choose launches our user interface.
195+
func (ui *ChooseUI) Choose() string {
196+
197+
ui.SetupUI()
198+
199+
ui.SetupKeyBinding()
200+
177201
//
178202
// Launch the application.
179203
//
180-
if err := app.SetRoot(grid, true).SetFocus(grid).EnableMouse(true).Run(); err != nil {
204+
err := ui.app.Run()
205+
if err != nil {
181206
panic(err)
182207
}
183208

cmd_feeds.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,24 @@ func (t *feedsCommand) FindFeeds(base string) ([]string, error) {
5454
// Get the body.
5555
defer response.Body.Close()
5656

57+
// Create a parser
5758
z := html.NewTokenizer(response.Body)
5859

60+
// Use the parser to get the links
61+
ret, err = t.runparser(z, base)
62+
63+
// Nothing found?
64+
if len(ret) == 0 {
65+
return ret, ErrNoFeeds
66+
}
67+
return ret, nil
68+
}
69+
70+
// runparser uses the given parser to look for feeds, and returns those it fouind
71+
func (t *feedsCommand) runparser(z *html.Tokenizer, base string) ([]string, error) {
72+
73+
ret := []string{}
74+
5975
for {
6076
tt := z.Next()
6177
switch tt {
@@ -92,11 +108,6 @@ func (t *feedsCommand) FindFeeds(base string) ([]string, error) {
92108
}
93109
}
94110

95-
// Nothing found?
96-
if len(ret) == 0 {
97-
return ret, ErrNoFeeds
98-
}
99-
return ret, nil
100111
}
101112

102113
// Execute is invoked if the user specifies `feeds` as the subcommand.

cmd_todo.go

+64-54
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,67 @@ func (t *todoCommand) scanPath(path string) error {
9494
return err
9595
}
9696

97+
// processLine outputs any matching lines; those that contain a date and a TODO/FIXME reference.
98+
func (t *todoCommand) processLine(path string, line string) error {
99+
100+
// Does this line contain TODO, or FIXME? If not return
101+
if !strings.Contains(line, "TODO") && !strings.Contains(line, "FIXME") {
102+
return nil
103+
}
104+
105+
// remove leading/trailing space
106+
line = strings.TrimSpace(line)
107+
108+
// Does it contain a date?
109+
match := t.reg.FindStringSubmatch(line)
110+
111+
// OK we have a date.
112+
if len(match) >= 2 {
113+
114+
// The date we've found
115+
date := match[1]
116+
117+
var found time.Time
118+
var err error
119+
120+
// Split by "/" to find the number
121+
// of values we've got:
122+
//
123+
// "DD/MM/YYYY"
124+
// "MM/YYYY"
125+
// "YYYY"
126+
parts := strings.Split(date, "/")
127+
128+
switch len(parts) {
129+
case 3:
130+
found, err = time.Parse("02/01/2006", date)
131+
if err != nil {
132+
return fmt.Errorf("failed to parse %s:%s", date, err)
133+
}
134+
case 2:
135+
found, err = time.Parse("01/2006", date)
136+
if err != nil {
137+
return fmt.Errorf("failed to parse %s:%s", date, err)
138+
}
139+
case 1:
140+
found, err = time.Parse("2006", date)
141+
if err != nil {
142+
return fmt.Errorf("failed to parse %s:%s", date, err)
143+
}
144+
default:
145+
return fmt.Errorf("unknown date-format %s", date)
146+
}
147+
148+
// If the date we've parsed is before today
149+
// then we alert on the line.
150+
if found.Before(t.now) {
151+
fmt.Printf("%s:%s\n", path, line)
152+
}
153+
}
154+
155+
return nil
156+
}
157+
97158
// processFile opens a file and reads line by line for a date.
98159
func (t *todoCommand) processFile(path string) error {
99160

@@ -129,60 +190,9 @@ func (t *todoCommand) processFile(path string) error {
129190
// Process each line
130191
for scanner.Scan() {
131192

132-
// The line we're operating upon.
133-
line := scanner.Text()
134-
135-
// Does this line contain TODO, or FIXME?
136-
if strings.Contains(line, "TODO") || strings.Contains(line, "FIXME") {
137-
138-
// remove leading/trailing space
139-
line = strings.TrimSpace(line)
140-
141-
// Does it contain a date?
142-
match := t.reg.FindStringSubmatch(line)
143-
144-
// OK we have a date.
145-
if len(match) >= 2 {
146-
147-
// The date we've found
148-
date := match[1]
149-
150-
var found time.Time
151-
152-
// Split by "/" to find the number
153-
// of values we've got:
154-
//
155-
// "DD/MM/YYYY"
156-
// "MM/YYYY"
157-
// "YYYY"
158-
parts := strings.Split(date, "/")
159-
160-
switch len(parts) {
161-
case 3:
162-
found, err = time.Parse("02/01/2006", date)
163-
if err != nil {
164-
return fmt.Errorf("failed to parse %s:%s", date, err)
165-
}
166-
case 2:
167-
found, _ = time.Parse("01/2006", date)
168-
if err != nil {
169-
return fmt.Errorf("failed to parse %s:%s", date, err)
170-
}
171-
case 1:
172-
found, _ = time.Parse("2006", date)
173-
if err != nil {
174-
return fmt.Errorf("failed to parse %s:%s", date, err)
175-
}
176-
default:
177-
return fmt.Errorf("unknown date-format %s", date)
178-
}
179-
180-
// If the date we've parsed is before today
181-
// then we alert on the line.
182-
if found.Before(t.now) {
183-
fmt.Printf("%s:%s\n", path, line)
184-
}
185-
}
193+
err := t.processLine(path, scanner.Text())
194+
if err != nil {
195+
return err
186196
}
187197
}
188198

0 commit comments

Comments
 (0)