summaryrefslogtreecommitdiff
path: root/toolkit/gocui/gocui.go
blob: 0d7c5a54a0b35e6f5fec5fa274c29b89b1db7267 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"errors"
	"fmt"
	"log"
	"os"

	"git.wit.org/wit/gui/toolkit"

	"github.com/awesome-gocui/gocui"
)

const delta = 1

var (
	views   = []string{}
	curView = -1
	idxView = 0
	currentX = 5
	currentY = 2
	groupSize = 0
	baseGui *gocui.Gui
	helpLabel *gocui.View
	err error
	ch chan(func ())
	outf *os.File
)

func Init() {
	baseGui, err = gocui.NewGui(gocui.OutputNormal, true)
	if err != nil {
		log.Panicln(err)
	}

	baseGui.Highlight = true
	baseGui.SelFgColor = gocui.ColorRed
	baseGui.SelFrameColor = gocui.ColorRed

	baseGui.SetManagerFunc(layout)

	if err := initKeybindings(baseGui); err != nil {
		log.Panicln(err)
	}

	viewWidget = make(map[*gocui.View]*toolkit.Widget)
	stringWidget = make(map[string]*toolkit.Widget)

	ch = make(chan func())

	outf, err = os.OpenFile("/tmp/witgui.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	// hmm. where to put this?
	// defer outf.Close()

	log.SetOutput(outf)
	log.Println("This is a test log entry")
}

func Queue(f func()) {
	log.Println("QUEUEEEEE")
	f()
}

func Main(f func()) {
	// close the STDOUT log file
	defer outf.Close()
	if (baseGui == nil) {
		panic("WTF Main()")
	}
	defer baseGui.Close()
	// log.Println("ADDDDDDDD BUTTTTTTTTTON")
	// addButton("test 3")
	f()
	if err := baseGui.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
		log.Panicln(err)
	}
	baseGui.Close()
	os.Exit(0)
}

func layout(g *gocui.Gui) error {
	var err error
	maxX, _ := g.Size()

	helpLabel, err = g.SetView("help", maxX-32, 0, maxX-1, 12, 0)
	if err != nil {
		if !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}
		fmt.Fprintln(helpLabel, "KEYBINDINGS")
		fmt.Fprintln(helpLabel, "Enter: Click Button")
		fmt.Fprintln(helpLabel, "Tab/Space: Switch Buttons")
		fmt.Fprintln(helpLabel, "")
		fmt.Fprintln(helpLabel, "h: Help")
		fmt.Fprintln(helpLabel, "Backspace: Delete Button")
		fmt.Fprintln(helpLabel, "Arrow keys: Move Button")
		fmt.Fprintln(helpLabel, "t: Move Button to the top")
		fmt.Fprintln(helpLabel, "b: Move Button to the button")
		fmt.Fprintln(helpLabel, "STDOUT: /tmp/witgui.log")
		fmt.Fprintln(helpLabel, "Ctrl-C or Q: Exit")
	}
	return nil
}