7 Commits

Author SHA1 Message Date
Hadi 64b36e716c Merge branch 'main' of github.com:anotherhadi/usbguard-tui 2026-05-11 20:10:07 +02:00
Hadi 85184dafca Add FUNDING.yml
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
2026-05-11 20:10:01 +02:00
Hadi 6db3a32758 Merge branch 'main' of github.com:anotherhadi/usbguard-tui 2026-05-06 14:42:02 +02:00
Hadi 1ac92a5ace Print nixos rules on exit
Signed-off-by: Hadi <hadi@example.com>
2026-05-06 14:41:50 +02:00
Hadi 2c54df832c Use the hash instead of ID to get the Status
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
2026-05-05 19:47:01 +02:00
Hadi ecd12f18e0 Remove dead code
Signed-off-by: Hadi <hadi@example.com>
2026-05-05 09:50:49 +02:00
Hadi 787d4ac0f1 fmt
Signed-off-by: Hadi <hadi@example.com>
2026-05-05 09:42:34 +02:00
7 changed files with 136 additions and 76 deletions
+1
View File
@@ -0,0 +1 @@
ko_fi: anotherhadi
+3 -3
View File
@@ -29,7 +29,7 @@ func ListDevices() ([]Device, error) {
} }
d, err := parseLine(line) d, err := parseLine(line)
if err == nil { if err == nil {
d.Permanent = rules[d.VidPid] == d.Status d.Permanent = rules[d.Hash] == d.Status
devices = append(devices, d) devices = append(devices, d)
} }
} }
@@ -47,8 +47,8 @@ func listRules() map[string]Status {
continue continue
} }
d, err := parseLine(line) d, err := parseLine(line)
if err == nil { if err == nil && d.Hash != "" {
rules[d.VidPid] = d.Status rules[d.Hash] = d.Status
} }
} }
return rules return rules
+6
View File
@@ -20,6 +20,7 @@ type Device struct {
Name string Name string
Status Status Status Status
VidPid string VidPid string
Hash string
Permanent bool Permanent bool
} }
@@ -57,6 +58,7 @@ func parseLine(line string) (Device, error) {
Name: name, Name: name,
Status: status, Status: status,
VidPid: extractUnquoted(rest, "id"), VidPid: extractUnquoted(rest, "id"),
Hash: extractField(rest, "hash"),
}, nil }, nil
} }
@@ -74,6 +76,10 @@ func extractField(rule, field string) string {
return rest[:end] return rest[:end]
} }
func NixOSRule(dev Device, status Status) string {
return fmt.Sprintf("%s id %s name \"%s\"", status, dev.VidPid, dev.Name)
}
func extractUnquoted(rule, field string) string { func extractUnquoted(rule, field string) string {
prefix := field + " " prefix := field + " "
idx := strings.Index(rule, prefix) idx := strings.Index(rule, prefix)
+11 -20
View File
@@ -2,7 +2,6 @@ package ui
import ( import (
"fmt" "fmt"
"image/color"
"io" "io"
"charm.land/bubbles/v2/list" "charm.land/bubbles/v2/list"
@@ -11,11 +10,10 @@ import (
"github.com/anotherhadi/usbguard-tui/internal/guard" "github.com/anotherhadi/usbguard-tui/internal/guard"
) )
// deviceDelegate renders device list items with status colors.
type deviceDelegate struct{} type deviceDelegate struct{}
func (d deviceDelegate) Height() int { return 2 } func (d deviceDelegate) Height() int { return 2 }
func (d deviceDelegate) Spacing() int { return 0 } func (d deviceDelegate) Spacing() int { return 0 }
func (d deviceDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } func (d deviceDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) { func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
@@ -26,19 +24,13 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
selected := index == m.Index() selected := index == m.Index()
var clr color.Color colorMap := statusColors
if selected { if selected {
var ok bool colorMap = statusColorsSelected
clr, ok = statusColorsSelected[dev.Status] }
if !ok { clr, ok := colorMap[dev.Status]
clr = colorMuted if !ok {
} clr = colorMuted
} else {
var ok bool
clr, ok = statusColors[dev.Status]
if !ok {
clr = colorMuted
}
} }
var nameStyle, descStyle lipgloss.Style var nameStyle, descStyle lipgloss.Style
@@ -69,23 +61,22 @@ 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 { type actionItem struct {
label string label string
fn func(int, bool) error fn func(int, bool) error
permanent bool permanent bool
status guard.Status status guard.Status
nixos bool
} }
func (a actionItem) Title() string { return a.label } func (a actionItem) Title() string { return a.label }
func (a actionItem) Description() string { return "" } func (a actionItem) Description() string { return "" }
func (a actionItem) FilterValue() string { return a.label } func (a actionItem) FilterValue() string { return a.label }
// actionDelegate renders single-line action items.
type actionDelegate struct{} type actionDelegate struct{}
func (d actionDelegate) Height() int { return 1 } func (d actionDelegate) Height() int { return 1 }
func (d actionDelegate) Spacing() int { return 0 } func (d actionDelegate) Spacing() int { return 0 }
func (d actionDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } func (d actionDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) { func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
+102 -49
View File
@@ -1,6 +1,7 @@
package ui package ui
import ( import (
"fmt"
"strings" "strings"
"time" "time"
@@ -25,6 +26,7 @@ type (
devicesMsg []guard.Device devicesMsg []guard.Device
daemonStatusMsg string daemonStatusMsg string
actionMsg struct{ err error } actionMsg struct{ err error }
nixRuleMsg struct{ rule string }
) )
type Model struct { type Model struct {
@@ -38,8 +40,11 @@ type Model struct {
notice string notice string
selectedDev *guard.Device selectedDev *guard.Device
rulesManaged bool rulesManaged bool
pendingRules []string
} }
func (m Model) PendingRules() []string { return m.pendingRules }
func New() Model { func New() Model {
l := list.New(nil, deviceDelegate{}, 0, 0) l := list.New(nil, deviceDelegate{}, 0, 0)
l.SetShowHelp(false) l.SetShowHelp(false)
@@ -62,7 +67,7 @@ func New() Model {
rulesManaged := guard.IsRulesManaged() rulesManaged := guard.IsRulesManaged()
notice := "" notice := ""
if rulesManaged { if rulesManaged {
notice = "Rules managed by NixOS config: permanent actions not available." notice = "Rules managed by NixOS config: permanent actions will print NixOS rules on exit."
listKeys.AllowPerm.SetEnabled(false) listKeys.AllowPerm.SetEnabled(false)
listKeys.BlockPerm.SetEnabled(false) listKeys.BlockPerm.SetEnabled(false)
listKeys.RejectPerm.SetEnabled(false) listKeys.RejectPerm.SetEnabled(false)
@@ -79,19 +84,24 @@ func New() Model {
} }
func makeActionList(rulesManaged bool) list.Model { func makeActionList(rulesManaged bool) list.Model {
items := []list.Item{ var items []list.Item
actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, if rulesManaged {
actionItem{"block", guard.BlockDevice, false, guard.Blocked},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected},
}
if !rulesManaged {
items = []list.Item{ items = []list.Item{
actionItem{"allow", guard.AllowDevice, false, guard.Allowed}, actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed}, actionItem{"allow (perm)", nil, true, guard.Allowed, true},
actionItem{"block", guard.BlockDevice, false, guard.Blocked}, actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked}, actionItem{"block (perm)", nil, true, guard.Blocked, true},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected}, actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected}, actionItem{"reject (perm)", nil, true, guard.Rejected, true},
}
} else {
items = []list.Item{
actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed, false},
actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked, false},
actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected, false},
} }
} }
l := list.New(items, actionDelegate{}, 24, len(items)) l := list.New(items, actionDelegate{}, 24, len(items))
@@ -133,6 +143,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.daemonStatus = string(msg) m.daemonStatus = string(msg)
return m, nil return m, nil
case nixRuleMsg:
m.state = stateList
m.selectedDev = nil
m.pendingRules = append(m.pendingRules, msg.rule)
count := len(m.pendingRules)
if count == 1 {
m.notice = "1 NixOS rule queued (printed on exit)"
} else {
m.notice = fmt.Sprintf("%d NixOS rules queued (printed on exit)", count)
}
return m, nil
case actionMsg: case actionMsg:
m.state = stateList m.state = stateList
m.selectedDev = nil m.selectedDev = nil
@@ -164,22 +186,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
case tea.MouseWheelMsg: case tea.MouseWheelMsg:
if m.state == statePopup { return m.updateMouseWheel(msg)
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
} }
if m.state == stateList { if m.state == stateList {
@@ -216,18 +223,11 @@ func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
m.state = statePopup m.state = statePopup
return m, nil return m, nil
} }
case id >= 0 && key.Matches(msg, listKeys.Allow): }
return m, doAction(id, guard.AllowDevice, false) if id >= 0 {
case id >= 0 && !m.rulesManaged && key.Matches(msg, listKeys.AllowPerm): if cmd := m.deviceActionCmd(msg, id); cmd != nil {
return m, doAction(id, guard.AllowDevice, true) return m, cmd
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)
} }
} }
var cmd tea.Cmd var cmd tea.Cmd
@@ -244,6 +244,10 @@ func (m Model) updatePopup(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
case key.Matches(msg, listKeys.Open): case key.Matches(msg, listKeys.Open):
if item := m.actionList.SelectedItem(); item != nil { if item := m.actionList.SelectedItem(); item != nil {
a := item.(actionItem) a := item.(actionItem)
if a.nixos && m.selectedDev != nil {
rule := guard.NixOSRule(*m.selectedDev, a.status)
return m, func() tea.Msg { return nixRuleMsg{rule: rule} }
}
return m, doAction(m.selectedDev.ID, a.fn, a.permanent) return m, doAction(m.selectedDev.ID, a.fn, a.permanent)
} }
} }
@@ -301,8 +305,13 @@ func (m Model) renderActionSelect() string {
title := popupTitleStyle.Foreground(color).Width(innerW).Render(dev.Name) title := popupTitleStyle.Foreground(color).Width(innerW).Render(dev.Name)
hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel") hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel")
content := strings.Join([]string{title, m.actionList.View(), "", hint}, "\n") parts := []string{title, m.actionList.View(), ""}
return popupStyle.Width(innerW).Render(content) if m.rulesManaged {
nixosHint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("[NixOS: perm rules printed on exit]")
parts = append(parts, nixosHint)
}
parts = append(parts, hint)
return popupStyle.Width(innerW).Render(strings.Join(parts, "\n"))
} }
func (m Model) popupOuterWidth() int { func (m Model) popupOuterWidth() int {
@@ -322,15 +331,12 @@ func (m Model) actionListInnerWidth() int {
func (m Model) defaultNotice() string { func (m Model) defaultNotice() string {
if m.rulesManaged { if m.rulesManaged {
return "Rules managed by NixOS config: permanent actions not available." return "Rules managed by NixOS config: permanent actions will print NixOS rules on exit."
} }
return "" return ""
} }
func (m Model) actionItemCount() int { func (m Model) actionItemCount() int {
if m.rulesManaged {
return 3
}
return 6 return 6
} }
@@ -341,8 +347,12 @@ func (m Model) actionItemCount() int {
func (m *Model) updateActionListSize() { func (m *Model) updateActionListSize() {
items := m.actionItemCount() items := m.actionItemCount()
innerW := m.actionListInnerWidth() innerW := m.actionListInnerWidth()
// popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7 // popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7; +1 for NixOS footer
available := m.height - 7 - 2 // 2 lines margin overhead := 7
if m.rulesManaged {
overhead = 8
}
available := m.height - overhead - 2 // 2 lines margin
if available >= items { if available >= items {
m.actionList.SetShowPagination(false) m.actionList.SetShowPagination(false)
m.actionList.SetSize(innerW, items) m.actionList.SetSize(innerW, items)
@@ -391,3 +401,46 @@ func doAction(id int, fn func(int, bool) error, permanent bool) tea.Cmd {
return actionMsg{err: fn(id, permanent)} 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
}
+1 -3
View File
@@ -48,7 +48,5 @@ var (
popupTitleStyle = lipgloss.NewStyle().Bold(true).MarginBottom(1) popupTitleStyle = lipgloss.NewStyle().Bold(true).MarginBottom(1)
keyHintStyle = lipgloss.NewStyle().Foreground(colorAccent).Bold(true) warnStyle = lipgloss.NewStyle().Foreground(colorRejected)
warnStyle = lipgloss.NewStyle().Foreground(colorRejected)
errStyle = lipgloss.NewStyle().Foreground(colorBlocked).Bold(true)
) )
+12 -1
View File
@@ -23,8 +23,19 @@ func main() {
} }
p := tea.NewProgram(ui.New()) p := tea.NewProgram(ui.New())
if _, err := p.Run(); err != nil { m, err := p.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
if fm, ok := m.(ui.Model); ok {
if rules := fm.PendingRules(); len(rules) > 0 {
fmt.Println("# Add to your NixOS configuration:")
fmt.Println("services.usbguard.rules = lib.mkAfter ''")
for _, rule := range rules {
fmt.Println(" ", rule)
}
fmt.Println("'';")
}
}
} }