From b8774327b6e19b31221f0a5ff16b627e2638a0f8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Feb 2024 23:06:10 +0530 Subject: [PATCH] icat kitten: Add a command line argument to override terminal window size detection Fixes #7165 I had five minutes, so why not. --- docs/changelog.rst | 2 ++ kittens/icat/main.go | 48 +++++++++++++++++++++++++++++++++++--------- kittens/icat/main.py | 5 +++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index f94a14bef..235c5d54d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kittens/icat/main.go b/kittens/icat/main.go index 8eb55babe..c73daacc6 100644 --- a/kittens/icat/main.go +++ b/kittens/icat/main.go @@ -146,17 +146,45 @@ func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) { if err != nil { return 1, err } - if tty.IsTerminal(os.Stdout.Fd()) { - screen_size, err = tty.GetSize(int(os.Stdout.Fd())) - } else { - t, oerr := tty.OpenControllingTerm() - if oerr != nil { - return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", oerr) + if opts.UseWindowSize == "" { + if tty.IsTerminal(os.Stdout.Fd()) { + screen_size, err = tty.GetSize(int(os.Stdout.Fd())) + } else { + t, oerr := tty.OpenControllingTerm() + 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 { diff --git a/kittens/icat/main.py b/kittens/icat/main.py index 2f9a01df3..c8c8f2e0a 100644 --- a/kittens/icat/main.py +++ b/kittens/icat/main.py @@ -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