mirror of
https://github.com/shokinn/hosts-go.git
synced 2025-08-23 08:33:02 +00:00
feat: save hosts file
This commit is contained in:
parent
e59af1cb02
commit
e813704f81
6 changed files with 60 additions and 23 deletions
|
@ -40,17 +40,18 @@ const (
|
|||
)
|
||||
|
||||
type Model struct {
|
||||
list list.Model
|
||||
detail viewport.Model
|
||||
hosts *core.HostsFile
|
||||
width int
|
||||
height int
|
||||
mode Mode
|
||||
focus pane
|
||||
list list.Model
|
||||
detail viewport.Model
|
||||
hosts *core.HostsFile
|
||||
hostsPath string
|
||||
width int
|
||||
height int
|
||||
mode Mode
|
||||
focus pane
|
||||
}
|
||||
|
||||
// NewModel constructs the TUI model from a parsed HostsFile.
|
||||
func NewModel(hf *core.HostsFile) Model {
|
||||
// NewModel constructs the TUI model from a parsed HostsFile and its path.
|
||||
func NewModel(hf *core.HostsFile, path string) Model {
|
||||
items := make([]list.Item, len(hf.Entries))
|
||||
for i, e := range hf.Entries {
|
||||
items[i] = entryItem{entry: e}
|
||||
|
@ -63,11 +64,12 @@ func NewModel(hf *core.HostsFile) Model {
|
|||
l.SetShowPagination(false)
|
||||
|
||||
m := Model{
|
||||
list: l,
|
||||
detail: viewport.New(0, 0),
|
||||
hosts: hf,
|
||||
mode: ViewMode,
|
||||
focus: listPane,
|
||||
list: l,
|
||||
detail: viewport.New(0, 0),
|
||||
hosts: hf,
|
||||
hostsPath: path,
|
||||
mode: ViewMode,
|
||||
focus: listPane,
|
||||
}
|
||||
m.refreshDetail()
|
||||
return m
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"hosts-go/internal/core"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
|
@ -24,6 +28,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
} 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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue