TUI IDE multiplexer?!
golang
tui
ide
1package main
2
3import (
4 "encoding/json"
5 "errors"
6 "os"
7 "path/filepath"
8 "time"
9)
10
11// EmphemeralConfig reprsents values that are not crucially defined by the user, such as current panel sizings and positions.
12type EphemeralConfig struct {
13 PanelSizes map[string]map[int]int
14 saving bool
15 path string
16 cancel chan bool
17}
18
19func (c *EphemeralConfig) Save() error {
20 if c.path == "" {
21 return errors.New("ephemeral config save path not defined")
22 }
23
24 // TODO: Make this... "debounce"
25 if c.saving {
26 c.cancel <- true
27 }
28
29 c.saving = true
30 go func() {
31 select {
32 case <-c.cancel:
33 case <-time.After(time.Second * 1):
34 b, err := json.Marshal(c)
35 if err != nil {
36 panic(err)
37 }
38
39 if err := os.WriteFile(c.path, b, 0644); err != nil {
40 panic(err)
41 }
42 c.saving = false
43 }
44 }()
45
46 return nil
47}
48
49func (c *EphemeralConfig) Load() error {
50 if c.path == "" {
51 return errors.New("ephemeral config load path not defined")
52 }
53
54 b, err := os.ReadFile(c.path)
55 if err != nil {
56 return err
57 }
58
59 if err := json.Unmarshal(b, c); err != nil {
60 return err
61 }
62 return nil
63}
64
65var econfig EphemeralConfig
66
67func init() {
68 user, err := os.UserConfigDir()
69 if err != nil {
70 panic(err)
71 }
72
73 dirpath := filepath.Join(user, "aight")
74
75 if err := os.MkdirAll(dirpath, 0755); err != nil {
76 panic(err)
77 }
78
79 econfig.path = filepath.Join(dirpath, "ephemeral.json")
80 econfig.cancel = make(chan bool)
81 econfig.PanelSizes = make(map[string]map[int]int)
82
83 // TODO: Move to an app init func.
84 if err := econfig.Load(); err != nil {
85 panic(err)
86 }
87}