Open fenollp opened 1 year ago
Bat already supports line ranges, but as a flag instead of a file name :)
From the manpage:
-r, --line-range <N:M>...
Only print the specified range of lines for each file. For
example:
--line-range 30:40
prints lines 30 to 40
--line-range :40
prints lines 1 to 40
--line-range 40:
prints lines 40 to the end of the file
--line-range 30:+10
prints lines 30 to 40
We could look into supporting filename:range
as an option, but I'm not too sure that it would be feasible. Windows and NTFS use a colon to specify alternate data streams on files.
I'd love for this to be supported at least for non windows platforms. Want me to open a PR?
--line-range
physically cuts the file, though - no way to "scroll" up, right?
--line-range
physically cuts the file, though - no way to "scroll" up, right?
That is correct, yeah.
Looking back at this issue with a clear head, it might be possible to support printing everything while opening the pager to start a specific line. I'm not sure for other pagers, but less +5G
would seek down to the 5th line when the pager is started:
1 This is a text file.
2 Foo
3 Bar
4 Baz
|---------------------------------- <-- viewport of less
| 5 Cat |
| 6 Dog |
| 7 Frog |
| 8 Bird |
| 9 Rabbit |
|----------------------------------
10 Hydrogen
11 Helium
12 Lithium
...
For that to work with bat
's output, to compensate for the file header, it would need an additional 3 lines. If bat
detects less
as the pager, we could add an extra argument of +123G
when starting the process. It's not quite seamless, though:
When bat
does soft line wrapping, it creates a line break with \n
. less
would see that as two (or more) lines, meaning we won't have a 1:1 correspondence of file lines to less
less.
Since bat
doesn't buffer output, we won't be able to know what line 53
of the file is line 53 in less
's buffer until we print it. By that point, we've already started less
(and have no way to control it anymore).
If you use less +100G
with only 50 lines of output, less
starts up with an error message that needs to be acknowledged before anything is rendered to the terminal: "Cannot seek to line number 300 (press RETURN)"
How would this work with multiple input files?
n
-th line of the first file?n
-th printed line? (but we can't know that until we start printing the files)And if the user doesn't use a pager at all, it would be downright impossible. AFAIK, neither ANSI nor VT___ have escape sequences that can directly influence the terminal's viewport over its scrollback buffer. The next best thing would be to buffer it ourselves and use the alternate screen mode to display the current viewport... but that's just a minimally-useful pager.
In an ideal world, we would have some other pager that works like less
, but accepts control sequences over an alternate file descriptor (e.g. fd3). Then we could just send something like "annotate line 58 as 'real_line_50'" followed by "scroll to 'real_line_50'".
Honestly, I've wanted to make a standalone pager that has bidirectional communication with some host program (to facilitate use cases like this) for years, but I just haven't had the time between work and classes. It's a massive undertaking made even more complicated by (1) needing to support ANSI/VT escape sequences in the input; and (2) rendering and handling terminal inputs without using curses or other TUI libraries, which either lack support for rendering cells with certain attributes (e.g. hyperlinks, 24-bit color), or have a ton of abstraction that would overcomplicate a pager.
I'd prefer that
bat
opensmigrations/2022-04-26-081536_create_ips/up.sql
with line60
in the middle of the viewport.Thanks!