summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthieu <matthieu.cneude@gmail.com>2019-08-25 20:24:19 +0200
committerMatthieu <matthieu.cneude@gmail.com>2019-08-25 20:25:05 +0200
commit50e0b1667ba34fe7f5bc1409dc6e62470f0050a2 (patch)
tree445c2a16badfd9ac37ce9e25204e6bdfa7484721
parent2eaed96ff24e700bb7a097e6469a5586734170f6 (diff)
[github-widgets] Add hot reloading
-rw-r--r--cmd/devdash/config.go1
-rw-r--r--cmd/devdash/devdash.go33
-rw-r--r--internal/display_widget.go4
-rw-r--r--internal/plateform/termui.go12
-rw-r--r--internal/tui.go10
5 files changed, 41 insertions, 19 deletions
diff --git a/cmd/devdash/config.go b/cmd/devdash/config.go
index 08c45d6..e0ae5fc 100644
--- a/cmd/devdash/config.go
+++ b/cmd/devdash/config.go
@@ -21,6 +21,7 @@ type config struct {
type General struct {
Keys map[string]string `mapstructure:"keys"`
Refresh int64 `mapstructure:"refresh"`
+ Reload bool `mapstructure:"reload"`
}
// RefreshTime return the duration before refreshing the data of all widgets, in seconds.
diff --git a/cmd/devdash/devdash.go b/cmd/devdash/devdash.go
index fc43116..6f9b900 100644
--- a/cmd/devdash/devdash.go
+++ b/cmd/devdash/devdash.go
@@ -16,6 +16,7 @@ import (
var debug *bool
func main() {
+ // TODO add mor commands + better management of them (cobra?)
file := flag.String("config", ".devdash.yml", "The config file")
debug = flag.Bool("debug", false, "Debug mode")
term := flag.Bool("term", false, "Display terminal dimensions")
@@ -27,14 +28,15 @@ func main() {
return
}
- cfg, tui, err := loadFile(*file)
+ termui, err := plateform.NewTermUI(*debug)
if err != nil {
- internal.DisplayError(tui, err)
+ fmt.Println(err)
}
+ tui := internal.NewTUI(termui)
defer tui.Close()
- run(cfg.Projects, tui)
+ cfg := loadFile(*file)
if _, err := os.Stat(*file); os.IsNotExist(err) {
internal.DisplayNoFile(tui)
@@ -46,10 +48,17 @@ func main() {
tui.AddRow()
tui.Render()
} else {
+ tui.AddKQuit(cfg.KQuit())
ticker := time.NewTicker(time.Duration(cfg.RefreshTime()) * time.Second)
go func() {
for range ticker.C {
- tui.Clean()
+ if cfg.General.Reload {
+ // TODO add possibility to press key to reload
+ cfg = hotReload(tui, file)
+ } else {
+ tui.Clean()
+ }
+
run(cfg.Projects, tui)
}
}()
@@ -58,18 +67,18 @@ func main() {
tui.Loop()
}
-func loadFile(file string) (config, *internal.Tui, error) {
- termui, err := plateform.NewTermUI(*debug)
- if err != nil {
- return config{}, nil, err
- }
+func hotReload(tui *internal.Tui, file *string) config {
+ cfg := loadFile(*file)
+ tui.HotReload()
- tui := internal.NewTUI(termui)
+ return cfg
+}
+
+func loadFile(file string) config {
data, _ := ioutil.ReadFile(file)
cfg := mapConfig(data)
- tui.AddKQuit(cfg.KQuit())
- return cfg, tui, nil
+ return cfg
}
func run(projects []Project, tui *internal.Tui) {
diff --git a/internal/display_widget.go b/internal/display_widget.go
index 4cd95c8..a58831a 100644
--- a/internal/display_widget.go
+++ b/internal/display_widget.go
@@ -16,8 +16,8 @@ func DisplayNoFile(tui *Tui) {
You can name the configuration file [my-config.yml](fg-blue,fg-bold), and then run [devdash -config my-config.yml](fg-green,fg-bold).
- There is an example of configuration here:
- [https://github.com/Phantas0s/devdash#user-content-getting-started](fg-blue,fg-bold).
+ There are multiple example of configurations there:
+ [https://thedevdash.com/getting-started/](fg-blue,fg-bold).
More complex configuration examples are available here:
[https://github.com/Phantas0s/devdash#configuration-examples](fg-blue,fg-bold).
diff --git a/internal/plateform/termui.go b/internal/plateform/termui.go
index 0dd9f50..37b9f9d 100644
--- a/internal/plateform/termui.go
+++ b/internal/plateform/termui.go
@@ -6,8 +6,6 @@ import (
"github.com/Phantas0s/termui"
)
-var debug bool = false
-
type termUI struct {
body *termui.Grid
widgets []termui.GridBufferer
@@ -17,8 +15,6 @@ type termUI struct {
// NewTermUI returns a new Terminal Interface object with a given output mode.
func NewTermUI(d bool) (*termUI, error) {
- debug = d
-
if err := termui.Init(); err != nil {
return nil, err
}
@@ -178,7 +174,7 @@ func (t *termUI) Table(
}
// KQuit set a key to quit the application.
-func (termUI) KQuit(key string) {
+func (*termUI) KQuit(key string) {
termui.Handle(fmt.Sprintf("/sys/kbd/%s", key), func(termui.Event) {
termui.StopLoop()
})
@@ -209,3 +205,9 @@ func (t *termUI) Clean() {
t.body.BgColor = termui.ThemeAttr("bg")
t.body.Width = termui.TermWidth()
}
+
+func (t *termUI) HotReload() {
+ termui.Close()
+ _ = termui.Init()
+ t.Clean()
+}
diff --git a/internal/tui.go b/internal/tui.go
index 8c9b4d9..08fa131 100644
--- a/internal/tui.go
+++ b/internal/tui.go
@@ -150,11 +150,16 @@ type looper interface {
Loop()
}
+type reloader interface {
+ HotReload()
+}
+
type manager interface {
keyManager
renderer
drawer
looper
+ reloader
}
type coloredElements struct {
@@ -447,3 +452,8 @@ func (t *Tui) Loop() {
func (t *Tui) Clean() {
t.instance.Clean()
}
+
+// Hot reload the whole TUI
+func (t *Tui) HotReload() {
+ t.instance.HotReload()
+}