From 726969833bc95fc97f5ee38b435d4ba23b5613d4 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Sun, 20 Feb 2022 10:32:38 +0100 Subject: [PATCH] main: use terminfo to set window title Parse the terminal capabilities from the TERM environment variable instead of using a hard coded list of terminals. tcell does not expose the status line capabilities. Use another library for this: github.com/xo/terminfo Signed-off-by: Robin Jarry --- aerc.go | 26 +++++++++++++++----------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/aerc.go b/aerc.go index 555b3a3..65c31ec 100644 --- a/aerc.go +++ b/aerc.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "io" "io/ioutil" @@ -8,11 +9,11 @@ import ( "os" "runtime/debug" "sort" - "strings" "time" "git.sr.ht/~sircmpwn/getopt" "github.com/mattn/go-isatty" + "github.com/xo/terminfo" "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/commands/account" @@ -93,18 +94,21 @@ func usage() { log.Fatal("Usage: aerc [-v] [mailto:...]") } -var termsWithStatusLine = []string{"xterm", "tmux", "screen"} - func setWindowTitle() { - term := strings.ToLower(os.Getenv("TERM")) - for _, t := range termsWithStatusLine { - if strings.Contains(term, t) { - // TODO: avoid hard coding the list of terminals that - // have status line support. - os.Stderr.Write([]byte("\x1b]0;aerc\a")) - return - } + ti, err := terminfo.LoadFromEnv() + if err != nil { + return } + + if !ti.Has(terminfo.HasStatusLine) { + return + } + + buf := new(bytes.Buffer) + ti.Fprintf(buf, terminfo.ToStatusLine) + fmt.Fprint(buf, "aerc") + ti.Fprintf(buf, terminfo.FromStatusLine) + os.Stderr.Write(buf.Bytes()) } func main() { diff --git a/go.mod b/go.mod index 3a18379..954e784 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab github.com/stretchr/testify v1.4.0 + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect github.com/zenhack/go.notmuch v0.0.0-20211022191430-4d57e8ad2a8b golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect diff --git a/go.sum b/go.sum index 00e4036..c1f3d6e 100644 --- a/go.sum +++ b/go.sum @@ -191,6 +191,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=