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 171 172 173 174 175 176 177 178
|
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
// Package ui contains user-interface functions and helpers for termshark.
package ui
import (
"fmt"
"os"
"time"
"github.com/gcla/gowid"
"github.com/gcla/gowid/widgets/dialog"
"github.com/gcla/gowid/widgets/framed"
"github.com/gcla/gowid/widgets/holder"
"github.com/gcla/gowid/widgets/paragraph"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/configs/profiles"
)
//======================================================================
// SuggestSwitchingTerm will open a dialog asking the user if they would like to try
// a more colorful TERM setting.
func SuggestSwitchingTerm(app gowid.IApp) {
var switchTerm *dialog.Widget
Yes := dialog.Button{
Msg: "Yes",
Action: gowid.MakeWidgetCallback("exec", gowid.WidgetChangedFunction(func(app gowid.IApp, w gowid.IWidget) {
termshark.ShouldSwitchTerminal = true
switchTerm.Close(app)
RequestQuit()
})),
}
No := dialog.Button{
Msg: "No",
}
NoAsk := dialog.Button{
Msg: "No, don't ask",
Action: gowid.MakeWidgetCallback("exec", gowid.WidgetChangedFunction(func(app gowid.IApp, w gowid.IWidget) {
profiles.SetConf("main.disable-term-helper", true)
switchTerm.Close(app)
})),
}
term := os.Getenv("TERM")
term256 := term + "-256color"
switchTerm = dialog.New(
framed.NewSpace(paragraph.New(fmt.Sprintf("Termshark is running with TERM=%s. The terminal database contains %s. Would you like to switch for a more colorful experience? Termshark will need to restart.", term, term256))),
dialog.Options{
Buttons: []dialog.Button{Yes, No, NoAsk},
NoShadow: true,
BackgroundStyle: gowid.MakePaletteRef("dialog"),
BorderStyle: gowid.MakePaletteRef("dialog"),
ButtonStyle: gowid.MakePaletteRef("dialog-button"),
Modal: true,
FocusOnWidget: false,
},
)
switchTerm.Open(appView, gowid.RenderWithRatio{R: 0.5}, app)
}
//======================================================================
// IsTerminalLegible will open up a dialog asking the user to confirm that their
// running termshark is legible, having upgraded the TERM variable to a 256-color
// version and restarted.
func IsTerminalLegible(app gowid.IApp) {
var saveTerm *dialog.Widget
YesSave := dialog.Button{
Msg: "Yes",
Action: gowid.MakeWidgetCallback("exec", gowid.WidgetChangedFunction(func(app gowid.IApp, w gowid.IWidget) {
profiles.SetConf("main.term", os.Getenv("TERM"))
saveTerm.Close(app)
})),
}
NoSave := dialog.Button{
Msg: "No",
}
saveTerm = dialog.New(
framed.NewSpace(paragraph.New(fmt.Sprintf("Do you want to save TERM=%s in termshark's config to use as the default?", os.Getenv("TERM")))),
dialog.Options{
Buttons: []dialog.Button{YesSave, NoSave},
NoShadow: true,
BackgroundStyle: gowid.MakePaletteRef("dialog"),
BorderStyle: gowid.MakePaletteRef("dialog"),
ButtonStyle: gowid.MakePaletteRef("dialog-button"),
Modal: true,
FocusOnWidget: false,
},
)
tick := time.NewTicker(time.Duration(1) * time.Second)
stopC := make(chan struct{})
var legibleTerm *dialog.Widget
No := dialog.Button{
Msg: "No",
Action: gowid.MakeWidgetCallback("exec", gowid.WidgetChangedFunction(func(app gowid.IApp, w gowid.IWidget) {
close(stopC)
termshark.ShouldSwitchBack = true
legibleTerm.Close(app)
RequestQuit()
})),
}
Yes := dialog.Button{
Msg: "Yes",
Action: gowid.MakeWidgetCallback("exec", gowid.WidgetChangedFunction(func(app gowid.IApp, w gowid.IWidget) {
close(stopC)
legibleTerm.Close(app)
saveTerm.Open(appView, gowid.RenderWithRatio{R: 0.5}, app)
})),
}
secs := 10
tw := func(count int) *paragraph.Widget {
return paragraph.New(fmt.Sprintf("Is the terminal legible? If no selection is made, termshark will revert to its original TERM setting in %d seconds.", secs))
}
message := holder.New(tw(secs))
termshark.TrackedGo(func() {
Loop:
for {
select {
case <-tick.C:
secs--
switch {
case secs >= 0:
app.Run(gowid.RunFunction(func(app gowid.IApp) {
message.SetSubWidget(tw(secs), app)
}))
case secs < 0:
tick.Stop()
close(stopC)
app.Run(gowid.RunFunction(func(app gowid.IApp) {
termshark.ShouldSwitchBack = true
legibleTerm.Close(app)
RequestQuit()
}))
}
case <-stopC:
break Loop
}
}
}, Goroutinewg)
legibleTerm = dialog.New(
framed.NewSpace(message),
dialog.Options{
Buttons: []dialog.Button{Yes, No},
NoShadow: true,
BackgroundStyle: gowid.MakePaletteRef("dialog"),
BorderStyle: gowid.MakePaletteRef("dialog"),
ButtonStyle: gowid.MakePaletteRef("dialog-button"),
Modal: true,
FocusOnWidget: false,
StartIdx: 1,
},
)
legibleTerm.Open(appView, gowid.RenderWithRatio{R: 0.5}, app)
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|