mirror of
https://github.com/anotherhadi/usbguard-tui.git
synced 2026-05-11 22:02:34 +02:00
+14
-13
@@ -2,11 +2,12 @@ package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io"
|
||||
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"charm.land/bubbles/v2/list"
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||
)
|
||||
|
||||
@@ -25,18 +26,18 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
||||
|
||||
selected := index == m.Index()
|
||||
|
||||
var color lipgloss.Color
|
||||
var clr color.Color
|
||||
if selected {
|
||||
var ok bool
|
||||
color, ok = statusColorsSelected[dev.Status]
|
||||
clr, ok = statusColorsSelected[dev.Status]
|
||||
if !ok {
|
||||
color = colorMuted
|
||||
clr = colorMuted
|
||||
}
|
||||
} else {
|
||||
var ok bool
|
||||
color, ok = statusColors[dev.Status]
|
||||
clr, ok = statusColors[dev.Status]
|
||||
if !ok {
|
||||
color = colorMuted
|
||||
clr = colorMuted
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +46,7 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
||||
nameStyle = lipgloss.NewStyle().
|
||||
Border(lipgloss.NormalBorder(), false, false, false, true).
|
||||
BorderForeground(colorAccent).
|
||||
Foreground(color).
|
||||
Foreground(clr).
|
||||
Bold(true).
|
||||
PaddingLeft(1)
|
||||
descStyle = lipgloss.NewStyle().
|
||||
@@ -54,7 +55,7 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
||||
Foreground(colorMuted).
|
||||
PaddingLeft(1)
|
||||
} else {
|
||||
nameStyle = lipgloss.NewStyle().Foreground(color).PaddingLeft(2)
|
||||
nameStyle = lipgloss.NewStyle().Foreground(clr).PaddingLeft(2)
|
||||
descStyle = lipgloss.NewStyle().Foreground(colorMuted).PaddingLeft(2)
|
||||
}
|
||||
|
||||
@@ -89,11 +90,11 @@ func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
||||
return
|
||||
}
|
||||
if index == m.Index() {
|
||||
color, ok := statusColorsSelected[a.status]
|
||||
clr, ok := statusColorsSelected[a.status]
|
||||
if !ok {
|
||||
color = colorAccent
|
||||
clr = colorAccent
|
||||
}
|
||||
fmt.Fprintf(w, " %s", lipgloss.NewStyle().Bold(true).Foreground(color).Render("> "+a.label))
|
||||
fmt.Fprintf(w, " %s", lipgloss.NewStyle().Bold(true).Foreground(clr).Render("> "+a.label))
|
||||
} else {
|
||||
fmt.Fprintf(w, " %s", a.label)
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package ui
|
||||
|
||||
import "github.com/charmbracelet/bubbles/key"
|
||||
import "charm.land/bubbles/v2/key"
|
||||
|
||||
type listKeyMap struct {
|
||||
Open key.Binding
|
||||
|
||||
+45
-15
@@ -4,12 +4,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"charm.land/bubbles/v2/help"
|
||||
"charm.land/bubbles/v2/key"
|
||||
"charm.land/bubbles/v2/list"
|
||||
"charm.land/bubbles/v2/textinput"
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
type state int
|
||||
@@ -45,17 +46,23 @@ func New() Model {
|
||||
l.SetShowStatusBar(true)
|
||||
l.SetShowTitle(false)
|
||||
l.DisableQuitKeybindings()
|
||||
// free j/k for our shortcuts
|
||||
l.KeyMap.CursorUp = key.NewBinding(key.WithKeys("up", "k"), key.WithHelp("↑/k", "up"))
|
||||
l.KeyMap.CursorDown = key.NewBinding(key.WithKeys("down"), key.WithHelp("↓", "down"))
|
||||
l.FilterInput.PromptStyle = lipgloss.NewStyle().Foreground(colorAccent)
|
||||
l.FilterInput.Cursor.Style = lipgloss.NewStyle().Foreground(colorAccent)
|
||||
|
||||
l.Styles = list.DefaultStyles(true)
|
||||
filterStyles := textinput.DefaultStyles(true)
|
||||
filterStyles.Focused.Prompt = filterStyles.Focused.Prompt.Foreground(colorAccent)
|
||||
filterStyles.Blurred.Prompt = filterStyles.Blurred.Prompt.Foreground(colorAccent)
|
||||
l.Styles.Filter = filterStyles
|
||||
|
||||
h := help.New()
|
||||
h.Styles = help.DefaultStyles(true)
|
||||
|
||||
return Model{
|
||||
state: stateList,
|
||||
list: l,
|
||||
actionList: makeActionList(),
|
||||
help: help.New(),
|
||||
help: h,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +94,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width = msg.Width
|
||||
m.height = msg.Height
|
||||
m.help.Width = msg.Width
|
||||
m.help.SetWidth(msg.Width)
|
||||
m.list.SetSize(msg.Width, m.listHeight())
|
||||
m.updateActionListSize()
|
||||
return m, nil
|
||||
@@ -124,11 +131,25 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
return m, fetchDevices
|
||||
|
||||
case tea.KeyMsg:
|
||||
case tea.KeyPressMsg:
|
||||
if m.state == statePopup {
|
||||
return m.updatePopup(msg)
|
||||
}
|
||||
return m.updateList(msg)
|
||||
|
||||
case tea.MouseClickMsg:
|
||||
if m.state == statePopup {
|
||||
var cmd tea.Cmd
|
||||
m.actionList, cmd = m.actionList.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
case tea.MouseWheelMsg:
|
||||
if m.state == statePopup {
|
||||
var cmd tea.Cmd
|
||||
m.actionList, cmd = m.actionList.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
}
|
||||
|
||||
if m.state == stateList {
|
||||
@@ -140,7 +161,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m Model) updateList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
||||
if msg.String() == "ctrl+c" {
|
||||
return m, tea.Quit
|
||||
}
|
||||
@@ -184,7 +205,7 @@ func (m Model) updateList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m Model) updatePopup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
func (m Model) updatePopup(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
||||
switch {
|
||||
case key.Matches(msg, cancelKey):
|
||||
m.state = stateList
|
||||
@@ -201,7 +222,16 @@ func (m Model) updatePopup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
func (m Model) View() tea.View {
|
||||
return tea.View{
|
||||
Content: m.renderContent(),
|
||||
AltScreen: true,
|
||||
WindowTitle: "USBGuard TUI",
|
||||
MouseMode: tea.MouseModeCellMotion,
|
||||
}
|
||||
}
|
||||
|
||||
func (m Model) renderContent() string {
|
||||
header := m.renderHeader()
|
||||
notice := m.renderNotice()
|
||||
listView := strings.TrimRight(m.list.View(), "\n")
|
||||
@@ -238,7 +268,7 @@ func (m Model) renderActionSelect() string {
|
||||
color := statusColors[dev.Status]
|
||||
innerW := m.actionListInnerWidth()
|
||||
|
||||
title := popupTitleStyle.Copy().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")
|
||||
|
||||
content := strings.Join([]string{title, m.actionList.View(), "", hint}, "\n")
|
||||
|
||||
@@ -3,7 +3,7 @@ package ui
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
|
||||
+13
-11
@@ -1,28 +1,30 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var (
|
||||
colorAllowed = lipgloss.Color("28")
|
||||
colorAllowedSelected = lipgloss.Color("42")
|
||||
colorBlocked = lipgloss.Color("124")
|
||||
colorBlockedSelected = lipgloss.Color("196")
|
||||
colorRejected = lipgloss.Color("130")
|
||||
colorRejectedSelected = lipgloss.Color("214")
|
||||
colorMuted = lipgloss.Color("240")
|
||||
colorAccent = lipgloss.Color("99")
|
||||
colorAllowed color.Color = lipgloss.Color("28")
|
||||
colorAllowedSelected color.Color = lipgloss.Color("42")
|
||||
colorBlocked color.Color = lipgloss.Color("124")
|
||||
colorBlockedSelected color.Color = lipgloss.Color("196")
|
||||
colorRejected color.Color = lipgloss.Color("130")
|
||||
colorRejectedSelected color.Color = lipgloss.Color("214")
|
||||
colorMuted color.Color = lipgloss.Color("240")
|
||||
colorAccent color.Color = lipgloss.Color("99")
|
||||
)
|
||||
|
||||
var statusColors = map[guard.Status]lipgloss.Color{
|
||||
var statusColors = map[guard.Status]color.Color{
|
||||
guard.Allowed: colorAllowed,
|
||||
guard.Blocked: colorBlocked,
|
||||
guard.Rejected: colorRejected,
|
||||
}
|
||||
|
||||
var statusColorsSelected = map[guard.Status]lipgloss.Color{
|
||||
var statusColorsSelected = map[guard.Status]color.Color{
|
||||
guard.Allowed: colorAllowedSelected,
|
||||
guard.Blocked: colorBlockedSelected,
|
||||
guard.Rejected: colorRejectedSelected,
|
||||
|
||||
Reference in New Issue
Block a user