mirror of
https://github.com/shokinn/hosts-go.git
synced 2025-08-23 08:33:02 +00:00
100 lines
2 KiB
Go
100 lines
2 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"hosts-go/internal/core"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
// Update handles all messages for the TUI.
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
var cmd tea.Cmd
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "q", "ctrl+c":
|
|
return m, tea.Quit
|
|
case "tab":
|
|
if m.focus == listPane {
|
|
m.focus = detailPane
|
|
} else {
|
|
m.focus = listPane
|
|
}
|
|
case "ctrl+e":
|
|
if m.mode == ViewMode {
|
|
m.mode = EditMode
|
|
} else {
|
|
m.mode = ViewMode
|
|
}
|
|
case "ctrl+s":
|
|
if m.mode == EditMode && m.hostsPath != "" {
|
|
if err := core.WriteHostsFile(m.hostsPath, m.hosts); err != nil {
|
|
fmt.Println("save failed:", err)
|
|
}
|
|
}
|
|
case " ":
|
|
if m.mode == EditMode {
|
|
if entry := m.SelectedEntry(); entry != nil {
|
|
entry.Active = !entry.Active
|
|
m.list.SetItem(m.list.Index(), entryItem{entry})
|
|
m.refreshDetail()
|
|
}
|
|
}
|
|
case "up", "k":
|
|
if m.focus == detailPane {
|
|
m.detail.ScrollUp(1)
|
|
} else {
|
|
m.list, cmd = m.list.Update(msg)
|
|
m.refreshDetail()
|
|
}
|
|
return m, cmd
|
|
case "down", "j":
|
|
if m.focus == detailPane {
|
|
m.detail.ScrollDown(1)
|
|
} else {
|
|
m.list, cmd = m.list.Update(msg)
|
|
m.refreshDetail()
|
|
}
|
|
return m, cmd
|
|
}
|
|
if m.focus == listPane {
|
|
m.list, cmd = m.list.Update(msg)
|
|
m.refreshDetail()
|
|
}
|
|
case tea.WindowSizeMsg:
|
|
m.width = msg.Width
|
|
m.height = msg.Height - 1
|
|
|
|
leftWidth := msg.Width / 2
|
|
rightWidth := msg.Width - leftWidth
|
|
leftHeight := m.height
|
|
rightHeight := m.height
|
|
|
|
if m.focus == listPane {
|
|
leftWidth -= 2
|
|
leftHeight -= 2
|
|
} else {
|
|
rightWidth -= 2
|
|
rightHeight -= 2
|
|
}
|
|
|
|
leftWidth -= 2
|
|
rightWidth -= 2
|
|
|
|
m.list.SetSize(leftWidth, leftHeight)
|
|
m.detail.Width = rightWidth
|
|
m.detail.Height = rightHeight
|
|
|
|
if m.focus == listPane {
|
|
m.list, cmd = m.list.Update(msg)
|
|
}
|
|
default:
|
|
if m.focus == listPane {
|
|
m.list, cmd = m.list.Update(msg)
|
|
m.refreshDetail()
|
|
}
|
|
}
|
|
return m, cmd
|
|
}
|