Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
load sys host on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 6, 2022
1 parent c932b6b commit 9a5eec2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
)

type config struct {
BaseDir string
DBFile string
BaseDir string
DBFile string
SysHostFile string
}

func New() *config {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
baseDir := filepath.Join(homeDir, ".local", "share", "gohost")
baseDir := filepath.Join(homeDir, ".gohost")
_, err = os.Stat(baseDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -28,12 +29,12 @@ func New() *config {
panic(err)
}
}

dbFile := filepath.Join(baseDir, "gohost.db")

sysHostFile := "/etc/hosts"
return &config{
BaseDir: baseDir,
DBFile: dbFile,
BaseDir: baseDir,
DBFile: dbFile,
SysHostFile: sysHostFile,
}
}

Expand All @@ -42,7 +43,7 @@ var (
once sync.Once
)

func Config() *config {
func Instance() *config {
once.Do(func() {
cfg = New()
})
Expand Down
2 changes: 1 addition & 1 deletion db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (

func Instance() *Store {
once.Do(func() {
cfg := config.Config()
cfg := config.Instance()
instance = New(&options{
File: cfg.DBFile,
Options: &bolthold.Options{
Expand Down
46 changes: 30 additions & 16 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,54 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"gohost/config"
"gohost/gohost"
"os"
)

type EditorView struct {
model *Model
HostTextarea textarea.Model
host gohost.Host
model *Model
hostEditor textarea.Model
host gohost.Host
}

func NewTextView(model *Model) *EditorView {
t := textarea.New()
t.ShowLineNumbers = true
t.Placeholder = "Host items here"
hostEditor := textarea.New()
hostEditor.ShowLineNumbers = true
return &EditorView{
model: model,
HostTextarea: textarea.New(),
model: model,
hostEditor: hostEditor,
}
}

func (v *EditorView) Init() tea.Cmd {
return nil
return func() tea.Msg {
sysHost, err := os.ReadFile(config.Instance().SysHostFile)
if err != nil {
v.hostEditor.SetValue("Can not open system hosts file")
return nil
}
v.hostEditor.SetValue(string(sysHost))
return nil
}
}

func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
var cmd tea.Cmd
var cmds []tea.Cmd
v.hostEditor, cmd = v.hostEditor.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.HostTextarea.SetHeight(msg.Height - v.model.reservedHeight)
v.HostTextarea.SetWidth(msg.Width - v.model.groupView.groupList.Width())
v.hostEditor.SetHeight(msg.Height - v.model.reservedHeight)
v.hostEditor.SetWidth(msg.Width - v.model.groupView.groupList.Width())
}
if v.model.state == editorViewState {
v.HostTextarea, cmd = v.HostTextarea.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Save):
v.host.SetContent([]byte(v.HostTextarea.Value()))
v.host.SetContent([]byte(v.hostEditor.Value()))
err := gohost.GetService().UpdateHost(v.host)
if err != nil {
v.model.Log(err.Error())
Expand All @@ -53,14 +63,18 @@ func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
}

func (v *EditorView) View() string {
return v.HostTextarea.View()
return v.hostEditor.View()
}

func (v *EditorView) Focus() {
v.HostTextarea.Focus()
v.hostEditor.Focus()
}

func (v *EditorView) Blur() {
v.hostEditor.Blur()
}

func (v *EditorView) SetHost(host gohost.Host) {
v.host = host
v.HostTextarea.SetValue(string(host.GetContent()))
v.hostEditor.SetValue(string(host.GetContent()))
}
5 changes: 2 additions & 3 deletions tui/group_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func (v *GroupView) Init() tea.Cmd {
}

func (v *GroupView) Update(msg tea.Msg) []tea.Cmd {

var cmd tea.Cmd
var cmds []tea.Cmd
v.groupList, cmd = v.groupList.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.groupList.SetHeight(msg.Height - v.model.reservedHeight)
// FIXME not work
v.groupList.SetWidth(msg.Width / 3)
v.model.helpView.debug = fmt.Sprintf("w %d h %d, w %d h %d", msg.Width, msg.Height, v.groupList.Width(), v.groupList.Height())
case tea.KeyMsg:
Expand All @@ -110,7 +110,6 @@ func (v *GroupView) Update(msg tea.Msg) []tea.Cmd {
}
}
}
v.groupList, cmd = v.groupList.Update(msg)
}
}

Expand Down
8 changes: 4 additions & 4 deletions tui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func newKeys() keyMaps {
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
key.WithKeys( "ctrl+c"),
key.WithHelp("ctrl+c", "quit"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select or confirm"),
),
Switch: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "switch view"),
key.WithKeys("ctrl+e"),
key.WithHelp("ctrl+e", "switch view"),
),
Save: key.NewBinding(
key.WithKeys("ctrl+s"),
Expand Down
10 changes: 7 additions & 3 deletions tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func NewModel() (*Model, error) {
}

func (m *Model) Init() tea.Cmd {
return nil
return tea.Batch(m.groupView.Init(),
m.editorView.Init(),
m.helpView.Init())
}

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -77,8 +79,8 @@ func (m *Model) View() string {
modelStyle.Render(m.groupView.View()),
focusedModelStyle.Render(m.editorView.View()))
}
//str = lipgloss.JoinVertical(lipgloss.Left, str, m.helpView.View())
str += "\n" + m.helpView.View()
str = lipgloss.JoinVertical(lipgloss.Left, str, m.helpView.View())
//str += "\n" + m.helpView.View()
return str
}

Expand All @@ -95,6 +97,8 @@ func (m *Model) switchNextState() sessionState {
func (m *Model) SwitchState(state sessionState) {
if state == editorViewState {
m.editorView.Focus()
} else {
m.editorView.Blur()
}
m.state = state
}

0 comments on commit 9a5eec2

Please sign in to comment.