mirror of
https://github.com/shokinn/hosts-go.git
synced 2025-08-23 16:43:02 +00:00
Implement TUI pane navigation
This commit is contained in:
parent
3561d15858
commit
a24041d664
6 changed files with 137 additions and 73 deletions
|
@ -3,8 +3,10 @@ package tui
|
|||
import (
|
||||
"fmt"
|
||||
"hosts-go/internal/core"
|
||||
"strings"
|
||||
|
||||
list "github.com/charmbracelet/bubbles/list"
|
||||
viewport "github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
|
@ -30,12 +32,21 @@ const (
|
|||
EditMode
|
||||
)
|
||||
|
||||
type pane int
|
||||
|
||||
const (
|
||||
listPane pane = iota
|
||||
detailPane
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
list list.Model
|
||||
detail viewport.Model
|
||||
hosts *core.HostsFile
|
||||
width int
|
||||
height int
|
||||
mode Mode
|
||||
focus pane
|
||||
}
|
||||
|
||||
// NewModel constructs the TUI model from a parsed HostsFile.
|
||||
|
@ -51,11 +62,39 @@ func NewModel(hf *core.HostsFile) Model {
|
|||
l.SetShowHelp(false)
|
||||
l.SetShowPagination(false)
|
||||
|
||||
return Model{
|
||||
list: l,
|
||||
hosts: hf,
|
||||
mode: ViewMode,
|
||||
m := Model{
|
||||
list: l,
|
||||
detail: viewport.New(0, 0),
|
||||
hosts: hf,
|
||||
mode: ViewMode,
|
||||
focus: listPane,
|
||||
}
|
||||
m.refreshDetail()
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Model) refreshDetail() {
|
||||
if len(m.hosts.Entries) == 0 {
|
||||
m.detail.SetContent("")
|
||||
return
|
||||
}
|
||||
entry := m.hosts.Entries[m.list.Index()]
|
||||
var b strings.Builder
|
||||
status := "active"
|
||||
if !entry.Active {
|
||||
status = "inactive"
|
||||
}
|
||||
fmt.Fprintf(&b, "IP: %s\n", entry.IP)
|
||||
fmt.Fprintf(&b, "Host: %s\n", entry.Hostname)
|
||||
if len(entry.Aliases) > 0 {
|
||||
fmt.Fprintf(&b, "Aliases: %s\n", strings.Join(entry.Aliases, ", "))
|
||||
}
|
||||
if entry.Comment != "" {
|
||||
fmt.Fprintf(&b, "Comment: %s\n", entry.Comment)
|
||||
}
|
||||
fmt.Fprintf(&b, "Status: %s", status)
|
||||
m.detail.SetContent(b.String())
|
||||
m.detail.YOffset = 0
|
||||
}
|
||||
|
||||
// Init satisfies tea.Model.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue