From ecd12f18e05b291dd6e7d58f95c0f427d51354ab Mon Sep 17 00:00:00 2001 From: Hadi Date: Tue, 5 May 2026 09:50:49 +0200 Subject: [PATCH] Remove dead code Signed-off-by: Hadi --- internal/ui/delegate.go | 22 +++------- internal/ui/model.go | 91 ++++++++++++++++++++++++++--------------- internal/ui/styles.go | 4 +- 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/internal/ui/delegate.go b/internal/ui/delegate.go index be26425..ee825b0 100644 --- a/internal/ui/delegate.go +++ b/internal/ui/delegate.go @@ -2,7 +2,6 @@ package ui import ( "fmt" - "image/color" "io" "charm.land/bubbles/v2/list" @@ -11,7 +10,6 @@ import ( "github.com/anotherhadi/usbguard-tui/internal/guard" ) -// deviceDelegate renders device list items with status colors. type deviceDelegate struct{} func (d deviceDelegate) Height() int { return 2 } @@ -26,19 +24,13 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I selected := index == m.Index() - var clr color.Color + colorMap := statusColors if selected { - var ok bool - clr, ok = statusColorsSelected[dev.Status] - if !ok { - clr = colorMuted - } - } else { - var ok bool - clr, ok = statusColors[dev.Status] - if !ok { - clr = colorMuted - } + colorMap = statusColorsSelected + } + clr, ok := colorMap[dev.Status] + if !ok { + clr = colorMuted } var nameStyle, descStyle lipgloss.Style @@ -69,7 +61,6 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I ) } -// actionItem represents a device policy action in the select popup. type actionItem struct { label string fn func(int, bool) error @@ -81,7 +72,6 @@ func (a actionItem) Title() string { return a.label } func (a actionItem) Description() string { return "" } func (a actionItem) FilterValue() string { return a.label } -// actionDelegate renders single-line action items. type actionDelegate struct{} func (d actionDelegate) Height() int { return 1 } diff --git a/internal/ui/model.go b/internal/ui/model.go index c9eba84..8268b35 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -79,12 +79,14 @@ func New() Model { } func makeActionList(rulesManaged bool) list.Model { - items := []list.Item{ - actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, - actionItem{"block", guard.BlockDevice, false, guard.Blocked}, - actionItem{"reject", guard.RejectDevice, false, guard.Rejected}, - } - if !rulesManaged { + var items []list.Item + if rulesManaged { + items = []list.Item{ + actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, + actionItem{"block", guard.BlockDevice, false, guard.Blocked}, + actionItem{"reject", guard.RejectDevice, false, guard.Rejected}, + } + } else { items = []list.Item{ actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed}, @@ -164,22 +166,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case tea.MouseWheelMsg: - if m.state == statePopup { - switch msg.Button { - case tea.MouseWheelUp: - m.actionList.CursorUp() - case tea.MouseWheelDown: - m.actionList.CursorDown() - } - } else { - switch msg.Button { - case tea.MouseWheelUp: - m.list.CursorUp() - case tea.MouseWheelDown: - m.list.CursorDown() - } - } - return m, nil + return m.updateMouseWheel(msg) } if m.state == stateList { @@ -216,18 +203,11 @@ func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { m.state = statePopup return m, nil } - case id >= 0 && key.Matches(msg, listKeys.Allow): - return m, doAction(id, guard.AllowDevice, false) - case id >= 0 && !m.rulesManaged && key.Matches(msg, listKeys.AllowPerm): - return m, doAction(id, guard.AllowDevice, true) - case id >= 0 && key.Matches(msg, listKeys.Block): - return m, doAction(id, guard.BlockDevice, false) - case id >= 0 && !m.rulesManaged && key.Matches(msg, listKeys.BlockPerm): - return m, doAction(id, guard.BlockDevice, true) - case id >= 0 && key.Matches(msg, listKeys.Reject): - return m, doAction(id, guard.RejectDevice, false) - case id >= 0 && !m.rulesManaged && key.Matches(msg, listKeys.RejectPerm): - return m, doAction(id, guard.RejectDevice, true) + } + if id >= 0 { + if cmd := m.deviceActionCmd(msg, id); cmd != nil { + return m, cmd + } } } var cmd tea.Cmd @@ -391,3 +371,46 @@ func doAction(id int, fn func(int, bool) error, permanent bool) tea.Cmd { return actionMsg{err: fn(id, permanent)} } } + +type actionBinding struct { + binding key.Binding + fn func(int, bool) error + perm bool + needsWritable bool +} + +var deviceActionBindings = []actionBinding{ + {listKeys.Allow, guard.AllowDevice, false, false}, + {listKeys.AllowPerm, guard.AllowDevice, true, true}, + {listKeys.Block, guard.BlockDevice, false, false}, + {listKeys.BlockPerm, guard.BlockDevice, true, true}, + {listKeys.Reject, guard.RejectDevice, false, false}, + {listKeys.RejectPerm, guard.RejectDevice, true, true}, +} + +func (m Model) deviceActionCmd(msg tea.KeyPressMsg, id int) tea.Cmd { + for _, b := range deviceActionBindings { + if (!b.needsWritable || !m.rulesManaged) && key.Matches(msg, b.binding) { + return doAction(id, b.fn, b.perm) + } + } + return nil +} + +func (m Model) updateMouseWheel(msg tea.MouseWheelMsg) (tea.Model, tea.Cmd) { + switch msg.Button { + case tea.MouseWheelUp: + if m.state == statePopup { + m.actionList.CursorUp() + } else { + m.list.CursorUp() + } + case tea.MouseWheelDown: + if m.state == statePopup { + m.actionList.CursorDown() + } else { + m.list.CursorDown() + } + } + return m, nil +} diff --git a/internal/ui/styles.go b/internal/ui/styles.go index 66a7bf9..a201951 100644 --- a/internal/ui/styles.go +++ b/internal/ui/styles.go @@ -48,7 +48,5 @@ var ( popupTitleStyle = lipgloss.NewStyle().Bold(true).MarginBottom(1) - keyHintStyle = lipgloss.NewStyle().Foreground(colorAccent).Bold(true) - warnStyle = lipgloss.NewStyle().Foreground(colorRejected) - errStyle = lipgloss.NewStyle().Foreground(colorBlocked).Bold(true) + warnStyle = lipgloss.NewStyle().Foreground(colorRejected) )