Use the term package to get tty size since it has a function for it

This commit is contained in:
Kovid Goyal 2022-08-21 11:31:09 +05:30
parent 6a9f3feba2
commit 82d0bd9364
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -14,7 +14,7 @@ import (
"github.com/mattn/go-runewidth"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sys/unix"
"golang.org/x/term"
"kitty"
"kitty/tools/utils"
@ -22,13 +22,6 @@ import (
var RootCmd *cobra.Command
func GetTTYSize() (*unix.Winsize, error) {
if stdout_is_terminal {
return unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
}
return nil, fmt.Errorf("STDOUT is not a TTY")
}
func key_in_slice(vals []string, key string) bool {
for _, q := range vals {
if q == key {
@ -333,12 +326,14 @@ func full_command_name(cmd *cobra.Command) string {
}
func show_usage(cmd *cobra.Command, use_pager bool) error {
ws, tty_size_err := GetTTYSize()
var output strings.Builder
screen_width := 80
if tty_size_err == nil && ws.Col < 80 {
screen_width = int(ws.Col)
if stdout_is_terminal {
screen_width, _, tty_size_err := term.GetSize(int(os.Stdout.Fd()))
if tty_size_err != nil || screen_width > 80 {
screen_width = 80
}
}
var output strings.Builder
use := cmd.Use
idx := strings.Index(use, " ")
if idx > -1 {