Fix help screen documentation - incorrect key binding for viewing details
This commit is contained in:
@@ -3,6 +3,7 @@ package screens
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/calmcacil/wg-admin/internal/tui/components"
|
||||
"github.com/calmcacil/wg-admin/internal/wireguard"
|
||||
@@ -11,16 +12,17 @@ import (
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
const statusRefreshInterval = 10 // seconds
|
||||
const statusRefreshInterval = 3 // seconds
|
||||
|
||||
// ListScreen displays a table of WireGuard clients
|
||||
type ListScreen struct {
|
||||
table table.Model
|
||||
search *components.SearchModel
|
||||
clients []ClientWithStatus
|
||||
filtered []ClientWithStatus
|
||||
sortedBy string // Column name being sorted by
|
||||
ascending bool // Sort direction
|
||||
table table.Model
|
||||
search *components.SearchModel
|
||||
clients []ClientWithStatus
|
||||
filtered []ClientWithStatus
|
||||
sortedBy string // Column name being sorted by
|
||||
ascending bool // Sort direction
|
||||
lastUpdated time.Time
|
||||
}
|
||||
|
||||
// ClientWithStatus wraps a client with its connection status
|
||||
@@ -43,9 +45,20 @@ func (s *ListScreen) Init() tea.Cmd {
|
||||
return tea.Batch(
|
||||
s.loadClients,
|
||||
wireguard.Tick(statusRefreshInterval),
|
||||
ticker(),
|
||||
)
|
||||
}
|
||||
|
||||
// ticker sends a message every second to update the time display
|
||||
func ticker() tea.Cmd {
|
||||
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
||||
return timeTickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
// timeTickMsg is sent every second to update the time display
|
||||
type timeTickMsg time.Time
|
||||
|
||||
// Update handles messages for the list screen
|
||||
func (s *ListScreen) Update(msg tea.Msg) (Screen, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
@@ -104,8 +117,11 @@ func (s *ListScreen) Update(msg tea.Msg) (Screen, tea.Cmd) {
|
||||
}
|
||||
case clientsLoadedMsg:
|
||||
s.clients = msg.clients
|
||||
s.lastUpdated = time.Now()
|
||||
s.search.SetTotalCount(len(s.clients))
|
||||
s.applyFilter()
|
||||
case timeTickMsg:
|
||||
// Trigger a re-render to update "Last updated" display
|
||||
case wireguard.StatusTickMsg:
|
||||
// Refresh status on periodic tick
|
||||
return s, s.loadClients
|
||||
@@ -132,7 +148,29 @@ func (s *ListScreen) View() string {
|
||||
Render("No matching clients found. Try a different search term.")
|
||||
}
|
||||
|
||||
return s.search.View() + "\n" + s.table.View()
|
||||
// Calculate time since last update
|
||||
timeAgo := "never"
|
||||
if !s.lastUpdated.IsZero() {
|
||||
duration := time.Since(s.lastUpdated)
|
||||
timeAgo = formatDuration(duration)
|
||||
}
|
||||
|
||||
lastUpdatedText := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("241")).
|
||||
Render("Last updated: " + timeAgo)
|
||||
|
||||
return s.search.View() + "\n" + s.table.View() + "\n" + lastUpdatedText
|
||||
}
|
||||
|
||||
// formatDuration returns a human-readable string for the duration
|
||||
func formatDuration(d time.Duration) string {
|
||||
if d < time.Minute {
|
||||
return "just now"
|
||||
}
|
||||
if d < time.Hour {
|
||||
return fmt.Sprintf("%d min ago", int(d.Minutes()))
|
||||
}
|
||||
return fmt.Sprintf("%d hr ago", int(d.Hours()))
|
||||
}
|
||||
|
||||
// loadClients loads clients from wireguard config
|
||||
|
||||
Reference in New Issue
Block a user