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

Commit

Permalink
use cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 7, 2022
1 parent eec5d5d commit 9f0a4cf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var (
service *Service
serviceOnce sync.Once
cfg = config.Instance()
)

func GetService() *Service {
Expand Down Expand Up @@ -84,7 +85,7 @@ func (s *Service) ChildNodes(nodeID string) []*TreeNode {
// ApplyHost TODO apply host to system
func (s *Service) ApplyHost(hosts []byte) {
// open system host file
sysHostFile, err := os.Create(config.Instance().SysHostFile)
sysHostFile, err := os.Create(cfg.SysHostFile)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions gohost/sys_host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gohost

import (
"gohost/config"
"os"
"sync"
)
Expand All @@ -16,7 +15,7 @@ func SysHost() Host {
sysHostOnce.Do(func() {
sysHostInstance = &sysHost{
name: "System Host",
desc: config.Instance().SysHostFile,
desc: cfg.SysHostFile,
}
})
return sysHostInstance
Expand Down Expand Up @@ -56,7 +55,7 @@ func (s *sysHost) GetName() string {
}

func (s *sysHost) GetContent() []byte {
hosts, err := os.ReadFile(config.Instance().SysHostFile)
hosts, err := os.ReadFile(cfg.SysHostFile)
if err != nil {
return []byte("Can not open system hosts file: \n" + err.Error())
}
Expand Down
11 changes: 7 additions & 4 deletions tui/help_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"strings"
)

type HelpView struct {
Expand Down Expand Up @@ -37,12 +38,14 @@ func (h *HelpView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (h *HelpView) View() string {
var helper string
helper += h.view.View(keys)
var b strings.Builder
b.WriteString(h.view.View(keys))
if h.enableDebug {
helper += "\nDebug: " + h.debug
b.WriteString(cfg.LineBreak)
b.WriteString("Debug: ")
b.WriteString(h.debug)
}
return helper
return b.String()
}

func (h *HelpView) Width() int {
Expand Down
24 changes: 14 additions & 10 deletions tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/config"
"gohost/tui/styles"
"strconv"
"strings"
)

type sessionState int
Expand All @@ -17,6 +19,8 @@ const (
lastState
)

var cfg = config.Instance()

type Model struct {
state sessionState
helpView *HelpView
Expand Down Expand Up @@ -66,24 +70,24 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *Model) View() string {
var str string
var b strings.Builder
switch m.state {
case treeViewState:
str = lipgloss.JoinHorizontal(lipgloss.Top,
b.WriteString(lipgloss.JoinHorizontal(lipgloss.Top,
styles.FocusedView.Render(m.groupView.View()),
styles.DefaultView.Render(m.editorView.View()))
styles.DefaultView.Render(m.editorView.View())))
case editorViewState:
str = lipgloss.JoinHorizontal(lipgloss.Top,
b.WriteString(lipgloss.JoinHorizontal(lipgloss.Top,
styles.DefaultView.Render(m.groupView.View()),
styles.FocusedView.Render(m.editorView.View()))
styles.FocusedView.Render(m.editorView.View())))
case nodeViewState:
str = lipgloss.JoinHorizontal(lipgloss.Top,
b.WriteString(lipgloss.JoinHorizontal(lipgloss.Top,
styles.DefaultView.Render(m.groupView.View()),
styles.FocusedView.Render(m.nodeView.View()),
)
styles.FocusedView.Render(m.nodeView.View())))
}
str = lipgloss.JoinVertical(lipgloss.Left, str, m.helpView.View())
return str

v := lipgloss.JoinVertical(lipgloss.Left, b.String(), m.helpView.View())
return v
}

func (m *Model) updateView(msg tea.Msg, cmds *[]tea.Cmd, view tea.Model) {
Expand Down
3 changes: 1 addition & 2 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tui
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"gohost/config"
"gohost/tui/styles"
"strings"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (v *NodeView) View() string {
for i := range v.inputs {
b.WriteString(v.inputs[i].View())
if i < len(v.inputs)-1 {
b.WriteString(config.Instance().LineBreak)
b.WriteString(cfg.LineBreak)
}
}
return b.String()
Expand Down

0 comments on commit 9f0a4cf

Please sign in to comment.