Skip to content

Commit

Permalink
A minibuffer option to set the preferred terminal type
Browse files Browse the repository at this point in the history
Sometimes the terminal variable TERM does not correctly capture the
capabilities of the running terminal - for example, TERM might report 8
colors whereas the terminal can actually support 256. Termshark has a
config variable, main.term, that can be used to set this variable so
that termshark will use the correct terminal settings. This change
exposes that setting to the minibuffer e.g.

set term screen-256color    # saves as main.term in termshark.toml
set noterm                  # clears the setting
  • Loading branch information
gcla committed Jul 19, 2020
1 parent c33f006 commit b9d4e1e
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions ui/lastline.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import (
"github.com/gcla/gowid/gwutil"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/widgets/minibuffer"
"github.com/gdamore/tcell/terminfo"
"github.com/gdamore/tcell/terminfo/dynamic"
)

//======================================================================

var notEnoughArgumentsErr = fmt.Errorf("Not enough arguments provided")
var invalidSetCommandErr = fmt.Errorf("Invalid set command")

type minibufferFn func(gowid.IApp, ...string) error

Expand Down Expand Up @@ -100,9 +103,7 @@ func (s unhelpfulArg) Completions() []string {

//======================================================================

type setArg struct {
arg string
}
type setArg struct{}

var _ minibuffer.IArg = setArg{}

Expand All @@ -118,6 +119,8 @@ func (s setArg) Completions() []string {
"dark-mode",
"disable-shark-fin",
"packet-colors",
"term",
"noterm",
}
}

Expand All @@ -142,6 +145,15 @@ func parseOnOff(str string) (bool, error) {
return false, strconv.ErrSyntax
}

func validateTerm(term string) error {
var err error
_, err = terminfo.LookupTerminfo(term)
if err != nil {
_, _, err = dynamic.LoadTerminfo(term)
}
return err
}

type setCommand struct{}

var _ minibuffer.IAction = setCommand{}
Expand All @@ -150,7 +162,8 @@ func (d setCommand) Run(app gowid.IApp, args ...string) error {
var err error
var b bool
var i uint64
if len(args) == 3 {
switch len(args) {
case 3:
switch args[1] {
case "auto-scroll":
if b, err = parseOnOff(args[2]); err == nil {
Expand Down Expand Up @@ -179,9 +192,26 @@ func (d setCommand) Run(app gowid.IApp, args ...string) error {
termshark.SetConf("main.packet-colors", PacketColors)
OpenMessage(fmt.Sprintf("Packet colors are now %s", gwutil.If(b, "on", "off").(string)), appView, app)
}
case "term":
if err = validateTerm(args[2]); err == nil {
termshark.SetConf("main.term", args[2])
app.Run(gowid.RunFunction(func(app gowid.IApp) {
OpenMessage(fmt.Sprintf("Terminal type is now %s\n(Requires restart)", args[2]), appView, app)
}))
}
default:
err = invalidSetCommandErr
}
case 2:
switch args[1] {
case "noterm":
termshark.DeleteConf("main.term")
app.Run(gowid.RunFunction(func(app gowid.IApp) {
OpenMessage("Terminal type is now unset\n(Requires restart)", appView, app)
}))
default:
err = invalidSetCommandErr
}
} else {
err = notEnoughArgumentsErr
}

if err != nil {
Expand Down

0 comments on commit b9d4e1e

Please sign in to comment.