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

Commit

Permalink
detect text editor saved
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 6, 2022
1 parent 3eabbbd commit df91262
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 14 deletions.
1 change: 1 addition & 0 deletions gohost/host.go
Expand Up @@ -13,6 +13,7 @@ type Host interface {
SetContent([]byte)
GetDesc() string
GetGroupID() string
IsEnabled() bool
}

func (s *Service) SaveHost(host Host) error {
Expand Down
2 changes: 1 addition & 1 deletion gohost/host_test.go
Expand Up @@ -30,6 +30,6 @@ func TestService_SaveHost(t *testing.T) {
func TestService_LoadHost(t *testing.T) {
hosts := GetService().loadLocalHosts("3")
for _, host := range hosts {
fmt.Println(host)
fmt.Printf("%+v\n", host)
}
}
6 changes: 4 additions & 2 deletions gohost/local_host.go
Expand Up @@ -6,6 +6,7 @@ type LocalHost struct {
Content []byte
Desc string
GroupID string
Enabled bool
}

// Implement of Host
Expand Down Expand Up @@ -35,8 +36,9 @@ func (h *LocalHost) GetGroupID() string {
return h.GroupID
}

// Implement of TreeNode
var _ TreeNode = (*LocalHost)(nil)
func (h *LocalHost) IsEnabled() bool {
return h.Enabled
}

func (h *LocalHost) FilterValue() string {
return h.Name
Expand Down
70 changes: 68 additions & 2 deletions gohost/sys_host.go
@@ -1,5 +1,71 @@
package gohost

type SysHost struct {
HostsEnable map[int]struct{}
import (
"gohost/config"
"os"
"sync"
)

type sysHost struct {
name string
desc string
}

var (
sysHostOnce sync.Once
sysHostInstance *sysHost
)

func SysHost() Host {
sysHostOnce.Do(func() {
sysHostInstance = &sysHost{
name: "System Host",
desc: config.Instance().SysHostFile,
}
})
return sysHostInstance
}

// Implement of Host
var _ Host = (*sysHost)(nil)

func (s *sysHost) FilterValue() string {
return s.name
}

func (s *sysHost) GetParentID() string {
panic("implement me")
}

func (s *sysHost) GetID() string {
panic("implement me")
}

func (s *sysHost) GetName() string {
//TODO implement me
return s.name
}

func (s *sysHost) GetContent() []byte {
hosts, err := os.ReadFile(config.Instance().SysHostFile)
if err != nil {
return []byte("Can not open system hosts file: \n" + err.Error())
}
return hosts
}

func (s *sysHost) SetContent(content []byte) {
panic("implement me")
}

func (s *sysHost) GetDesc() string {
return s.desc
}

func (s *sysHost) GetGroupID() string {
panic("implement me")
}

func (s *sysHost) IsEnabled() bool {
return true
}
39 changes: 30 additions & 9 deletions tui/editor_view.go
@@ -1,20 +1,21 @@
package tui

import (
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/config"
"gohost/gohost"
"os"
)

type EditorView struct {
model *Model
hostEditor textarea.Model
host gohost.Host
statusLine string
saved bool
prevLen int
}

func NewTextView(model *Model) *EditorView {
Expand All @@ -23,18 +24,17 @@ func NewTextView(model *Model) *EditorView {
return &EditorView{
model: model,
hostEditor: hostEditor,
statusLine: "initializing",
host: nil,
statusLine: "",
saved: true,
prevLen: 0,
}
}

func (v *EditorView) Init() tea.Cmd {
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))
// Display system host on start up
v.SetHost(gohost.SysHost())
return nil
}
}
Expand All @@ -54,13 +54,16 @@ func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
err := gohost.GetService().UpdateHost(v.host)
if err != nil {
v.model.Log(err.Error())
} else {
v.SetSaved()
}
}
} else {
// Disable key
msg = nil
}
}
v.RefreshStatusLine()
v.hostEditor, cmd = v.hostEditor.Update(msg)
return append(cmds, cmd)
}
Expand All @@ -80,4 +83,22 @@ func (v *EditorView) Blur() {
func (v *EditorView) SetHost(host gohost.Host) {
v.host = host
v.hostEditor.SetValue(string(host.GetContent()))
v.prevLen = v.hostEditor.Length()
}

func (v *EditorView) RefreshStatusLine() {
v.statusLine = fmt.Sprintf("file: %s, saved: %t\n", v.host.GetName(), v.IsSaved())
}

func (v *EditorView) IsSaved() bool {
saved := v.prevLen == v.hostEditor.Length()
if !saved {
v.saved = false
}
return v.saved && saved
}

func (v *EditorView) SetSaved() {
v.prevLen = v.hostEditor.Length()
v.saved = true
}

0 comments on commit df91262

Please sign in to comment.