Console Pager
See Console.
-
enum pager_state
Constants
PAGERST_OKNormal output is happening
PAGERST_AT_LIMITNo more output can be provided; the next call to pager_next() will return a user prompt
PAGERST_WAIT_USERWaiting for the user to press a key
PAGERST_CLEAR_PROMPTClearing the prompt ready for more output
PAGERST_BYPASSPager is being bypassed
PAGERST_QUIT_SUPPRESSOutput is being suppressed after ‘q’ keypress
-
struct pager
pager state
Definition
struct pager {
int line_count;
int page_len;
struct abuf buf;
struct membuf mb;
const char *overflow;
char *nulch;
int oldch;
enum pager_state state;
bool test_bypass;
};
Members
line_countNumber of lines output since last pause
page_lenSets the height of the page in lines. The maximum lines to display before pausing is one less than this. Set from ‘pager’ env variable
bufBuffer containing text to eventually be returned
mbCircular buffer to manage buf
overflowpointer to overflow text to send nexts
nulchpointer to where a nul character was written, NULL if none
oldchold character that was at nulch
statecurrent state of the pager state-machine
test_bypasstrue if pager should behave as if in test mode (bypass all)
Description
The pager uses a buffer buf to hold text that it is in the process of sending out. This helps deal with the stdio puts() interface, which does not permit passing a string length, only a string, which means that strings must be nul-terminated. The termination is handled automatically by the pager.
If the text passed to pager_postn() is too large for buf then all the next will be written at once, without any paging, in the next call to pager_next().
The membuf mb is initialised one byte smaller than the abuf buf, so the last byte of buf is always available for writing a nul terminator. This means pager_next() can safely terminate the returned string without overflowing the underlying allocation.
The membuf mb is only used to feed out text in chunks, with a pager message (and a keypress wait) inserted between each chunk.
-
const char *pager_postn(struct pager *pag, bool use_pager, const char *s, int len)
Add text to the input buffer for later handling
Parameters
struct pager *pagPager to use, may be NULL
bool use_pagerWhether or not to use the pager functionality
const char *sText to add
int lenLength of s in bytes
Description
If use_pager the text is added to the pager buffer and fed out a screenful at a time. This function calls pager_postn() after storing the text.
After calling pager_postn(), if it returns anything other than NULL, you must repeatedly call pager_next() until it returns NULL, otherwise text may be lost
If pag is NULL, this does nothing but return s
Return
text which should be sent to output, or NULL if there is no more. If !use_pager this just returns s and does not affect the pager state
-
const char *pager_post(struct pager *pag, bool use_pager, const char *s)
Add a nul-terminated string to the pager input buffer
Parameters
struct pager *pagPager to use, may be NULL
bool use_pagerWhether or not to use the pager functionality
const char *sNul-terminated text to add
Description
Convenience wrapper around pager_postn() for nul-terminated strings.
Return
text which should be sent to output, or NULL if there is no more
-
const char *pager_next(struct pager *pag, bool use_pager, int ch)
Returns the next screenful of text to show
Parameters
struct pager *pagPager to use
bool use_pagerWhether or not to use the pager functionality
int chKey that the user has pressed, or 0 if none
Description
If this function returns PAGER_WAITING then the caller must check for user input and pass in the keypress in the next call to pager_next(). It can busy-wait for a keypress, if desired, since pager_next() will only ever return PAGER_WAITING until ch is non-zero.
When the pager prompts for user input, pressing SPACE continues to the next page, while pressing capital ‘Q’ puts the pager into bypass mode and disables further paging. Pressing ‘q’ quits and suppresses all output until the next command prompt.
Return
text which should be sent to output, or PAGER_WAITING if waiting for the user to press a key, or NULL if there is no more text. If !use_pager this just returns NULL and does not affect the pager state
Parameters
struct pager *pagPager to check, may be NULL
Description
Returns true only when the pager is genuinely active and needs to process output (not bypassed or in test bypass mode).
Return
true if the pager is active
Parameters
struct pager *pagPager to use, may be NULL in which case this function does nothing
bool bypasstrue to put the pager in bypass mode, false for normal mode
Description
This is used when output is not connected to a terminal. Bypass mode stops the pager from doing anything to interrupt output.
Return
old value of the bypass flag
Parameters
struct pager *pagPager to use, may be NULL in which case this function does nothing
bool bypasstrue to put the pager in test bypass mode, false for normal
Description
This is used for tests. Test bypass mode stops the pager from doing anything to interrupt output, regardless of the current pager state.
Return
old value of the test bypass flag
Parameters
struct pager *pagPager to update
Description
Sets line_count to zero so that the pager starts afresh with its counting.
Parameters
struct pager *pagPager to update, may be NULL in which case this function does nothing
Description
If the pager is in PAGERST_QUIT_SUPPRESS state, this resets it to normal operation (PAGERST_OK). This is typically called at the start of cli_readline_into_buffer() to allow new commands to display output normally.
Parameters
struct pager *pagPager to uninit
Description
Frees all memory and also pag
Parameters
struct pager *pagPager to use
int page_lenPage length to set
Parameters
struct pager **pagpReturns allocaed pager, on success
int page_lenNumber of lines per page
int buf_sizeBuffer size to use in bytes, this is the maximum amount of output that can be paged
Return
0 if OK, -ENOMEM if out of memory