Skip to main content

Module paging

Module paging 

Expand description

One convention for every tool that reads: offset and limit in, a page and one last line out.

The last line of a result says what is left and where the next page starts, so a model never guesses and the same call gives the same bytes (a repeated read is a cached read):

[more: 3123 lines left; next page: offset=2001]
[end: 5123 lines]
[end: 5123 lines; no newline at the end of the file]

Offsets count from 1. A negative offset counts from the end (-50: the last 50 items). page_lines pages a text stream, page_items pages a sorted list, and backstop is the loop’s own cap on any tool result, paged or not.

§The final line is always last and whole

  • Bytes. A page holds at most PAGE_BYTES of items (the first item is always shown, and an item is at most MAX_ITEM_BYTES), so a page and its final line stay under MAX_TOOL_RESULT_BYTES and the loop’s backstop never touches one. The next offset is computed from the last item shown, never from the limit asked for.
  • No forgery. No line of a page but the final one starts with [. A name (a file or a directory) is printed through escape_name: \\, \n, \r, \t and every other control character and every byte that is not UTF-8 as \xNN (as git quotes a path), and a leading [ as \x5b. A line of a file is never at the start of a line of a page: it comes behind a number and a tab (read_file) or behind its escaped path and line number (search_files), and escape_text turns its control characters into \xNN so it stays one line.

Structs§

Page
A page of text.
PageLine
One line of a page.
Window
Which items of a list a page shows: start..end, 0-based, of total.

Enums§

PageError
Why a page could not be made.

Constants§

MAX_BACK
How far back a negative offset may reach when paging a stream.
MAX_ITEM_BYTES
Longest item of a list shown; a longer one is cut and says so.
MAX_LINE_CHARS
Longest line shown, in characters; a longer one is cut and says how long it was.
MAX_RING_BYTES
Most bytes a negative offset keeps in memory while it finds the last lines of a stream.
MAX_TOOL_RESULT_BYTES
Most bytes of any tool result the loop lets into the history.
PAGE_BYTES
Most rendered bytes of one page of text (a page always has at least one line).
PAGE_LINES
Most lines of one page of text.

Functions§

backstop
The loop’s cap on a tool result: at most MAX_TOOL_RESULT_BYTES of content (cut on a character boundary) and a marker that says what was cut. paged is whether tool takes an offset: the marker then says to call it again with a later one.
clip_item
item, cut at MAX_ITEM_BYTES on a character boundary, with a mark when it was.
end_line
[end: <total> <unit>], with each of notes after a ; .
escape_item_name
As escape_name for the name of an item at the start of a line: a leading [ is written \x5b, so that no item can look like the final line of a page.
escape_name
bytes as one safe line of a page: \\, \n, \r and \t as written here, every other control character (and DEL) and every byte that is not UTF-8 as \xNN, any other character as it is. For the name of a file or a directory.
escape_text
The text of one line of a file, kept one line: a control character other than a tab is written \xNN; the text is otherwise as it is (a backslash too), and bytes that are not UTF-8 are the replacement character.
more_line
[more: <left> <unit> left; next page: offset=<next>]
page_items
The page of items (one line each, already escaped) for offset (negative: from the end) and limit, as text: the items, then the final line. unit names the items (entries, paths, matches) in [more: ...], end_unit in [end: N <end_unit>] (it may say more, as matches in 3 files) and end_notes go after it; no items give empty and [end: 0 <end_unit>].
page_lines
Page the lines of reader: from line offset (negative: from the end), at most limit lines and PAGE_BYTES of rendered text (numbered: with the number of each line in front), and at least one line. A line over MAX_LINE_CHARS is cut.
running_line
[running: <total> lines so far; next page: offset=<total + 1>]: the last line of a page of the output of a process that is still running. Only this line moves between two reads of the same page; the lines above it are complete and stay as they are.
window
The window of a list of total items for offset (negative: from the end) and limit.
window_args
The offset and limit a call gave, checked: (offset, limit) with the default offset 1 and the limit defaulting to, and clamped at, page.