aboutsummaryrefslogtreecommitdiffstats
path: root/gosrc/main.go
blob: 8e6417a46e7baea60b5e9913c9e11521f0472ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//   SMITH            (  //       /
//   main              ( )/       /
//   by salade         )(/        /
//  ________________  ( /)        /
// ()__)____________)))))   :^}   /

package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"time"

	ui "github.com/gizak/termui/v3"
)

type nodeValue string

func (nv nodeValue) String() string {
	return string(nv)
}

func main() {
	// get user config from json file
	config_path := "./config.json"
	for i, a := range os.Args[1:] {
		if a == "-c" || a == "--config" {
			config_path = os.Args[i + 2]
		}
	}
	config_content, err := ioutil.ReadFile(config_path)
	if err != nil {
		log.Fatal("error: config file not found", err)
	}
	var config Config
	err = json.Unmarshal(config_content, &config)
	if err != nil {
		log.Fatal("error: marshall() ", err)
	}

	ui_loop(config)
	defer ui.Close()
	return
}

// draw stuff
func ui_loop(config Config) error {
	if err := ui.Init(); err != nil {
		log.Fatal("error: failed to initialize termui", err)
	}

	// create default grid
	active_grid := createMainWorkspace(config)
	// ui update loop
	ui.Render(active_grid.grid)
	ui_events := ui.PollEvents()
	ticker := time.NewTicker(time.Second).C
	ticker_count := 0
	for {
		select {
		case e := <-ui_events:
			switch e.ID {
				case "<C-c>":
					return nil
				case "<Resize>":
					payload := e.Payload.(ui.Resize)
					active_grid.grid.SetRect(0, 0, payload.Width, payload.Height)
					ui.Clear()
				case "j", "<Down>":
					active_grid.tree.ScrollDown()
				case "k", "<Up>":
					active_grid.tree.ScrollUp()
				case "e":
					active_grid.tree.ToggleExpand()
				case "<Home>":
					active_grid.tree.ScrollTop()
				case "G", "<End>":
					active_grid.tree.ScrollBottom()
				case "E":
					active_grid.tree.ExpandAll()
				case "C":
					active_grid.tree.CollapseAll()
				case "<Enter>":
					active_grid.handleEnter(config)
			}
			case <-ticker:
				ticker_count++
			default:
				ui.Render(active_grid.grid)
		}
	}
}