aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ui
diff options
context:
space:
mode:
authorMoritz Poldrack <git@moritz.sh>2022-07-31 14:32:48 +0200
committerRobin Jarry <robin@jarry.cc>2022-08-04 21:58:01 +0200
commit978d35d356e8752bdd272884df48a6289d88b40a (patch)
tree3910243e688ef503159d07ce44b22cfea5d6c6fd /lib/ui
parentc882cf9960be691fe55617b87cdfcfbabd5d5557 (diff)
downloadaerc-978d35d356e8752bdd272884df48a6289d88b40a.tar.gz
lint: homogenize operations and minor fixes (gocritic)
Apply GoDoc comment policy (comments for humans should have a space after the //; machine-readable comments shouldn't) Use strings.ReplaceAll instead of strings.Replace when appropriate Remove if/else chains by replacing them with switches Use short assignment/increment notation Replace single case switches with if statements Combine else and if when appropriate Signed-off-by: Moritz Poldrack <moritz@poldrack.dev> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ui')
-rw-r--r--lib/ui/borders.go3
-rw-r--r--lib/ui/grid.go3
-rw-r--r--lib/ui/popover.go7
-rw-r--r--lib/ui/stack.go3
-rw-r--r--lib/ui/tab.go19
-rw-r--r--lib/ui/textinput.go18
-rw-r--r--lib/ui/ui.go3
7 files changed, 24 insertions, 32 deletions
diff --git a/lib/ui/borders.go b/lib/ui/borders.go
index 2c689886..92e29b06 100644
--- a/lib/ui/borders.go
+++ b/lib/ui/borders.go
@@ -76,8 +76,7 @@ func (bordered *Bordered) Draw(ctx *Context) {
}
func (bordered *Bordered) MouseEvent(localX int, localY int, event tcell.Event) {
- switch content := bordered.content.(type) {
- case Mouseable:
+ if content, ok := bordered.content.(Mouseable); ok {
content.MouseEvent(localX, localY, event)
}
}
diff --git a/lib/ui/grid.go b/lib/ui/grid.go
index 2d195711..1eac2ee8 100644
--- a/lib/ui/grid.go
+++ b/lib/ui/grid.go
@@ -150,8 +150,7 @@ func (grid *Grid) Draw(ctx *Context) {
}
func (grid *Grid) MouseEvent(localX int, localY int, event tcell.Event) {
- switch event := event.(type) {
- case *tcell.EventMouse:
+ if event, ok := event.(*tcell.EventMouse); ok {
invalid := grid.invalid
grid.mutex.RLock()
diff --git a/lib/ui/popover.go b/lib/ui/popover.go
index 7a539de1..8c8c4cda 100644
--- a/lib/ui/popover.go
+++ b/lib/ui/popover.go
@@ -16,13 +16,14 @@ func (p *Popover) Draw(ctx *Context) {
width = ctx.Width() - p.x
}
- if p.y+p.height+1 < ctx.Height() {
+ switch {
+ case p.y+p.height+1 < ctx.Height():
// draw below
subcontext = ctx.Subcontext(p.x, p.y+1, width, p.height)
- } else if p.y-p.height >= 0 {
+ case p.y-p.height >= 0:
// draw above
subcontext = ctx.Subcontext(p.x, p.y-p.height, width, p.height)
- } else {
+ default:
// can't fit entirely above or below, so find the largest available
// vertical space and shrink to fit
if p.y > ctx.Height()-p.y {
diff --git a/lib/ui/stack.go b/lib/ui/stack.go
index d3722354..5ccf13bc 100644
--- a/lib/ui/stack.go
+++ b/lib/ui/stack.go
@@ -43,8 +43,7 @@ func (stack *Stack) Draw(ctx *Context) {
func (stack *Stack) MouseEvent(localX int, localY int, event tcell.Event) {
if len(stack.children) > 0 {
- switch element := stack.Peek().(type) {
- case Mouseable:
+ if element, ok := stack.Peek().(Mouseable); ok {
element.MouseEvent(localX, localY, event)
}
}
diff --git a/lib/ui/tab.go b/lib/ui/tab.go
index 0c6b3f5b..df76ccca 100644
--- a/lib/ui/tab.go
+++ b/lib/ui/tab.go
@@ -219,27 +219,28 @@ func (tabs *Tabs) moveTabPriv(to int, relative bool) {
}
tab := tabs.tabs[from]
- if to > from {
+ switch {
+ case to > from:
copy(tabs.tabs[from:to], tabs.tabs[from+1:to+1])
for i, h := range tabs.history {
if h == from {
tabs.history[i] = to
}
if h > from && h <= to {
- tabs.history[i] -= 1
+ tabs.history[i]--
}
}
- } else if from > to {
+ case from > to:
copy(tabs.tabs[to+1:from+1], tabs.tabs[to:from])
for i, h := range tabs.history {
if h == from {
tabs.history[i] = to
}
if h >= to && h < from {
- tabs.history[i] += 1
+ tabs.history[i]++
}
}
- } else {
+ default:
return
}
@@ -339,7 +340,7 @@ func (tabs *Tabs) removeHistory(index int) {
continue
}
if item > index {
- item = item - 1
+ item--
}
// dedup
if i > 0 && len(newHist) > 0 && item == newHist[len(newHist)-1] {
@@ -399,8 +400,7 @@ func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) {
}
unfocus := func() { changeFocus(false) }
refocus := func() { changeFocus(true) }
- switch event := event.(type) {
- case *tcell.EventMouse:
+ if event, ok := event.(*tcell.EventMouse); ok {
switch event.Buttons() {
case tcell.Button1:
selectedTab, ok := strip.clicked(localX, localY)
@@ -484,8 +484,7 @@ func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event)
content.parent.m.Lock()
tab := content.tabs[content.curIndex]
content.parent.m.Unlock()
- switch tabContent := tab.Content.(type) {
- case Mouseable:
+ if tabContent, ok := tab.Content.(Mouseable); ok {
tabContent.MouseEvent(localX, localY, event)
}
}
diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go
index 8f8f00d0..70dcb3f5 100644
--- a/lib/ui/textinput.go
+++ b/lib/ui/textinput.go
@@ -148,10 +148,8 @@ func (ti *TextInput) drawPopover(ctx *Context) {
}
func (ti *TextInput) MouseEvent(localX int, localY int, event tcell.Event) {
- switch event := event.(type) {
- case *tcell.EventMouse:
- switch event.Buttons() {
- case tcell.Button1:
+ if event, ok := event.(*tcell.EventMouse); ok {
+ if event.Buttons() == tcell.Button1 {
if localX >= len(ti.prompt)+1 && localX <= len(ti.text[ti.scroll:])+len(ti.prompt)+1 {
ti.index = localX - len(ti.prompt) - 1
ti.ensureScroll()
@@ -190,7 +188,7 @@ func (ti *TextInput) ensureScroll() {
func (ti *TextInput) insert(ch rune) {
left := ti.text[:ti.index]
right := ti.text[ti.index:]
- ti.text = append(left, append([]rune{ch}, right...)...)
+ ti.text = append(left, append([]rune{ch}, right...)...) //nolint:gocritic // intentional append to different slice
ti.index++
ti.ensureScroll()
ti.Invalidate()
@@ -323,8 +321,7 @@ func (ti *TextInput) OnFocusLost(onFocusLost func(ti *TextInput)) {
}
func (ti *TextInput) Event(event tcell.Event) bool {
- switch event := event.(type) {
- case *tcell.EventKey:
+ if event, ok := event.(*tcell.EventKey); ok {
switch event.Key() {
case tcell.KeyBackspace, tcell.KeyBackspace2:
ti.invalidateCompletions()
@@ -464,8 +461,7 @@ func (c *completions) prev() {
}
func (c *completions) Event(e tcell.Event) bool {
- switch e := e.(type) {
- case *tcell.EventKey:
+ if e, ok := e.(*tcell.EventKey); ok {
switch e.Key() {
case tcell.KeyTab:
if len(c.options) == 1 && c.idx >= 0 {
@@ -496,7 +492,7 @@ func (c *completions) Event(e tcell.Event) bool {
}
func findStem(words []string) string {
- if len(words) <= 0 {
+ if len(words) == 0 {
return ""
}
if len(words) == 1 {
@@ -519,7 +515,7 @@ func findStem(words []string) string {
return stem
}
}
- stem = stem + string(r)
+ stem += string(r)
stemLen++
}
}
diff --git a/lib/ui/ui.go b/lib/ui/ui.go
index 1f618a6f..596000a5 100644
--- a/lib/ui/ui.go
+++ b/lib/ui/ui.go
@@ -87,8 +87,7 @@ func (state *UI) Tick() bool {
select {
case event := <-state.tcEvents:
- switch event := event.(type) {
- case *tcell.EventResize:
+ if event, ok := event.(*tcell.EventResize); ok {
state.screen.Clear()
width, height := event.Size()
state.ctx = NewContext(width, height, state.screen, state.onPopover)