gopls/internal/telemetry/cmd/stacks: display closed issues white

The isTerminal implementation works for our dev machines,
{darwin,linux} x {amd64,arm}, and avoids a dependency
on x/term, which gopls so far doesn't need... though I
am now tempted to find a pretext for the dependency.

Change-Id: If96b5cf737c9ea998d0612dec294295a5528b22a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/613575
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Alan Donovan 2024-09-16 14:22:53 -04:00 коммит произвёл Gopher Robot
Родитель 5aac53c5ff
Коммит a319a85de8
1 изменённых файлов: 28 добавлений и 0 удалений

Просмотреть файл

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:build linux || darwin
// The stacks command finds all gopls stack traces reported by // The stacks command finds all gopls stack traces reported by
// telemetry in the past 7 days, and reports their associated GitHub // telemetry in the past 7 days, and reports their associated GitHub
// issue, creating new issues as needed. // issue, creating new issues as needed.
@ -73,12 +75,14 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode" "unicode"
"golang.org/x/sys/unix"
"golang.org/x/telemetry" "golang.org/x/telemetry"
"golang.org/x/tools/gopls/internal/util/browser" "golang.org/x/tools/gopls/internal/util/browser"
"golang.org/x/tools/gopls/internal/util/moremaps" "golang.org/x/tools/gopls/internal/util/moremaps"
@ -410,6 +414,12 @@ func main() {
fmt.Printf("%s issues:\n", caption) fmt.Printf("%s issues:\n", caption)
for _, summary := range keys { for _, summary := range keys {
count := issues[summary] count := issues[summary]
// Show closed issues in "white".
if isTerminal(os.Stdout) && strings.Contains(summary, "[closed]") {
// ESC + "[" + n + "m" => change color to n
// (37 = white, 0 = default)
summary = "\x1B[37m" + summary + "\x1B[0m"
}
fmt.Printf("%s (n=%d)\n", summary, count) fmt.Printf("%s (n=%d)\n", summary, count)
} }
} }
@ -940,3 +950,21 @@ func findPredicateBlock(body string) string {
} }
return rest return rest
} }
// isTerminal reports whether file is a terminal,
// avoiding a dependency on golang.org/x/term.
func isTerminal(file *os.File) bool {
// Hardwire the constants to avoid the need for build tags.
// The values here are good for our dev machines.
switch runtime.GOOS {
case "darwin":
const TIOCGETA = 0x40487413 // from unix.TIOCGETA
_, err := unix.IoctlGetTermios(int(file.Fd()), TIOCGETA)
return err == nil
case "linux":
const TCGETS = 0x5401 // from unix.TCGETS
_, err := unix.IoctlGetTermios(int(file.Fd()), TCGETS)
return err == nil
}
panic("unreachable")
}