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 - hints kitten: The option to set the text color for hints now allows arbitrary
colors (:pull:`7150`) colors (:pull:`7150`)
- icat kitten: Add a command line argument to override terminal window size detection (:iss:`7165`)
0.32.2 [2024-02-12] 0.32.2 [2024-02-12]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -146,17 +146,45 @@ func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) {
if err != nil { if err != nil {
return 1, err return 1, err
} }
if tty.IsTerminal(os.Stdout.Fd()) { if opts.UseWindowSize == "" {
screen_size, err = tty.GetSize(int(os.Stdout.Fd())) if tty.IsTerminal(os.Stdout.Fd()) {
} else { screen_size, err = tty.GetSize(int(os.Stdout.Fd()))
t, oerr := tty.OpenControllingTerm() } else {
if oerr != nil { t, oerr := tty.OpenControllingTerm()
return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", oerr) if oerr != nil {
return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", oerr)
}
screen_size, err = t.GetSize()
}
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")
} }
screen_size, err = t.GetSize()
}
if err != nil {
return 1, fmt.Errorf("Failed to query terminal using TIOCGWINSZ with error: %w", err)
} }
if opts.PrintWindowSize { if opts.PrintWindowSize {

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. 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 --print-window-size
type=bool-set type=bool-set
Print out the window size as <:italic:`width`>x<:italic:`height`> (in pixels) and quit. This is a Print out the window size as <:italic:`width`>x<:italic:`height`> (in pixels) and quit. This is a