summaryrefslogtreecommitdiff
path: root/help.go
blob: 067d841fac6afa995b9d615e80d51ba979fe26b9 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2014 The gocui Authors. All rights reserved.
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

// 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"
	"strings"
	"time"

	"github.com/awesome-gocui/gocui"
	log "go.wit.com/log"
)

/*
	This in helpText doesn't print

	"\x1b[0;32m  \x1b[0m", // this was a test to see what might be
	// possible with gocui. it doesn't seem to work for me
*/

var helpText []string = []string{"Help Menu",
	"",
	"H: toggle z(H)elp",
	"D: toggle light/dark mode",
	"Tab: toggle through windows",
	"q: quit()",
	"",
	"Debugging:",
	"O: toggle (O)output (os.STDOUT)",
	"S: super mouse",
	"M: list all widgets positions",
	"L: list all widgets in tree",
}

func hideHelp() {
	if me.showHelp {
		log.Info("help is already down")
		me.showHelp = true
		return
	}
	me.showHelp = true
	me.baseGui.DeleteView("help")
}

func showHelp() error {
	if !me.showHelp {
		log.Info("help is already up")
		me.showHelp = false
		return nil
	}
	me.showHelp = false
	g := me.baseGui
	var err error
	maxX, _ := g.Size()

	var newW int = 8
	for _, s := range helpText {
		if newW < len(s) {
			newW = len(s)
		}
	}

	help, err := g.SetView("help", maxX-(newW+me.FramePadW), 0, maxX-1, len(helpText)+me.FramePadH, 0)
	if err != nil {
		if !errors.Is(err, gocui.ErrUnknownView) {
			return err
		}
		help.SelBgColor = gocui.ColorGreen
		help.SelFgColor = gocui.ColorBlack
		// fmt.Fprintln(help, "Enter: Click Button")
		// fmt.Fprintln(help, "Tab/Space: Switch Buttons")
		// fmt.Fprintln(help, "Backspace: Delete Button")
		// fmt.Fprintln(help, "Arrow keys: Move Button")

		fmt.Fprintln(help, strings.Join(helpText, "\n"))

		if _, err := g.SetCurrentView("help"); err != nil {
			return err
		}
	}
	g.SetViewOnTop("help")
	me.helpLabel = help
	if me.clock.tk == nil {
		makeClock()
		me.clock.tk.MoveToOffset(maxX-10, 1)
		me.clock.tk.Hide()
		me.clock.tk.Show()
	}
	if me.clock.tk != nil {
		me.clock.tk.MoveToOffset(maxX-10, 1)
		me.clock.tk.Hide()
		me.clock.tk.Show()
	}
	if me.stdout.tk == nil {
		makeOutputWidget(me.baseGui, "made this in showHelp()")
		msg := fmt.Sprintf("test to stdout from in showHelp() %d\n", me.ecount)
		me.stdout.Write([]byte(msg))
		log.Log(NOW, "log.log(NOW) test")
	}
	return nil
}

func makeClock() {
	me.clock.tk = makeNewFlagWidget(me.clock.wId)
	me.clock.tk.dumpWidget("init() clock")
	w, h := me.baseGui.MousePosition()
	me.clock.tk.MoveToOffset(w, h)
	me.clock.tk.labelN = time.Now().Format("15:04:05")
	me.clock.tk.frame = false
	me.clock.tk.setColorLabel()
	me.clock.tk.Show()
	me.clock.active = true
	me.clock.tk.dumpWidget("showClock()")
}

// in the very end of redrawing things, this will place the help and stdout on the top or botton
// depending on the state the user has chosen
func setThingsOnTop() {
	if me.showHelp { // terrible variable name. FIXME
		// log.Info("help does not exist")
	} else {
		me.baseGui.SetViewOnTop("help")
	}
	if me.clock.tk != nil {
		me.baseGui.SetViewOnTop(me.clock.tk.v.Name())
	}

	if me.dark {
		me.stdout.tk.v.FgColor = gocui.ColorWhite
		me.stdout.tk.v.BgColor = gocui.ColorBlack
	} else {
		me.stdout.tk.v.FgColor = gocui.ColorBlack
		me.stdout.tk.v.BgColor = gocui.AttrNone
	}

	if me.stdout.outputOnTop {
		me.baseGui.SetViewOnTop("msg")
	} else {
		me.baseGui.SetViewOnBottom("msg")
	}
	if me.stdout.startOnscreen {
		log.Info("attempting to locate stdout on screen for the first time")
		me.stdout.tk.relocateStdout(me.stdout.lastW, me.stdout.lastH)
		me.stdout.startOnscreen = false
	}
	setBottomBG()
}

func setBottomBG() {
	// this attempts to find the "BG" widget and set it to the background on the very very bottom
	rootTK := me.treeRoot.TK.(*guiWidget)
	if tk := rootTK.findBG(); tk != nil {
		// log.Info("found BG. setting to bottom", tk.cuiName)
		if me.dark {
			tk.v.BgColor = gocui.ColorBlack
		} else {
			tk.v.BgColor = gocui.ColorWhite
		}
		tk.v.Clear()
		me.baseGui.SetViewOnBottom(tk.cuiName)
		w, h := me.baseGui.Size()
		me.baseGui.SetView(tk.cuiName, -1, -1, w+1, h+1, 0)
	}
}