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_BYTESof items (the first item is always shown, and an item is at mostMAX_ITEM_BYTES), so a page and its final line stay underMAX_TOOL_RESULT_BYTESand the loop’sbackstopnever 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 throughescape_name:\\,\n,\r,\tand 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), andescape_textturns its control characters into\xNNso it stays one line.
Structs§
- Page
- A page of text.
- Page
Line - One line of a page.
- Window
- Which items of a list a page shows:
start..end, 0-based, oftotal.
Enums§
- Page
Error - 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_BYTESofcontent(cut on a character boundary) and a marker that says what was cut.pagedis whethertooltakes anoffset: the marker then says to call it again with a later one. - clip_
item item, cut atMAX_ITEM_BYTESon a character boundary, with a mark when it was.- end_
line [end: <total> <unit>], with each ofnotesafter a;.- escape_
item_ name - As
escape_namefor 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 bytesas one safe line of a page:\\,\n,\rand\tas 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) foroffset(negative: from the end) andlimit, as text: the items, then the final line.unitnames the items (entries,paths,matches) in[more: ...],end_unitin[end: N <end_unit>](it may say more, asmatches in 3 files) andend_notesgo after it; no items giveemptyand[end: 0 <end_unit>]. - page_
lines - Page the lines of
reader: from lineoffset(negative: from the end), at mostlimitlines andPAGE_BYTESof rendered text (numbered: with the number of each line in front), and at least one line. A line overMAX_LINE_CHARSis 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
totalitems foroffset(negative: from the end) andlimit. - window_
args - The
offsetandlimita call gave, checked:(offset, limit)with the default offset 1 and the limit defaulting to, and clamped at,page.