icat kitten: Add a command line argument to override terminal window size detection

Fixes #7165

I had five minutes, so why not.
This commit is contained in:
Kovid Goyal
2024-02-27 23:06:10 +05:30
parent e9c4e73103
commit b8774327b6
3 changed files with 45 additions and 10 deletions

View File

@@ -70,6 +70,8 @@ Detailed list of changes
- hints kitten: The option to set the text color for hints now allows arbitrary
colors (:pull:`7150`)
- icat kitten: Add a command line argument to override terminal window size detection (:iss:`7165`)
0.32.2 [2024-02-12]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -146,6 +146,7 @@ func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) {
if err != nil {
return 1, err
}
if opts.UseWindowSize == "" {
if tty.IsTerminal(os.Stdout.Fd()) {
screen_size, err = tty.GetSize(int(os.Stdout.Fd()))
} else {
@@ -158,6 +159,33 @@ func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) {
if err != nil {
return 1, fmt.Errorf("Failed to query terminal using TIOCGWINSZ with error: %w", err)
}
} else {
parts := strings.SplitN(opts.UseWindowSize, ",", 4)
if len(parts) != 4 {
return 1, fmt.Errorf("Invalid size specification: " + opts.UseWindowSize)
}
screen_size = &unix.Winsize{}
t := 0
if t, err = strconv.Atoi(parts[0]); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Col = uint16(t)
if t, err = strconv.Atoi(parts[1]); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Row = uint16(t)
if t, err = strconv.Atoi(parts[2]); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Xpixel = uint16(t)
if t, err = strconv.Atoi(parts[3]); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Ypixel = uint16(t)
if screen_size.Xpixel == 0 || screen_size.Ypixel == 0 {
return 1, fmt.Errorf("zero is not allowed for screen pixel size")
}
}
if opts.PrintWindowSize {
fmt.Printf("%dx%d", screen_size.Xpixel, screen_size.Ypixel)

View File

@@ -72,6 +72,11 @@ The amount of time (in seconds) to wait for a response from the terminal, when
detecting image display support.
--use-window-size
Instead of querying the terminal for the window size, use the specified size, which must
be of the format: width_in_cells,height_in_cells,width_in_pixels,height_in_pixels
--print-window-size
type=bool-set
Print out the window size as <:italic:`width`>x<:italic:`height`> (in pixels) and quit. This is a