-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Help the user upgrade to a more colorful TERM setting
If the user runs in a terminal emulator with TERM=xterm or TERM=screen, then termshark will only use 8-colors. This is fine but the experience is better with 256 colors available (or more) - for example, for packet coloring. If the user knows to change TERM to xterm-256color, that's also fine, but not all termshark users will know to do this. This change adds a workflow to help the user run in a higher-color mode, if possible. On startup, termshark will check to see if (a) only 8 colors are in use and (b) if a 256-color version of the TERM variable is available. If so, termshark opens a dialog to suggest switching. If the user hits "No, don't ask", then termshark sets main.disable-term-helper in termshark.toml and won't ask again. If the user hits yes, termshark will restart using the 256-color TERM. After 3 seconds, termshark will open a dialog to ask the user to confirm that everything looks ok - the default is no, and if the user hits that, or if 10 seconds elapse without input, termshark reverts back to the original setting. If the user hits yes, termshark will ask if the user wants to save that TERM as the default to use; if yes, main.term will be set in termshark.toml.
- Loading branch information
Showing
6 changed files
with
265 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright 2019-2021 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" | ||
) | ||
|
||
//====================================================================== | ||
|
||
// 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) { | ||
termshark.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. Your 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) { | ||
termshark.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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters