pflib v2.7.0-1-gd371ab6a
Polarfire Interaction Library
BaseMenu Class Reference

Type-less menu base for common tasks. More...

#include <Menu.h>

Inheritance diagram for BaseMenu:
[legend]

Static Public Member Functions

static std::string readline (const std::string &prompt, const std::string &defval, bool preserve_last_blank=false)
 Read in a parameter using the default value if nothing provided. More...
 
static std::string readline_nosplit (const std::string &prompt, const std::string &defval)
 Get a raw input value without the additional splitting and modifications done in base readline. More...
 
static std::string readline (const std::string &prompt)
 Read a string parameter without a default. More...
 
static std::string readline (const std::string &prompt, const std::vector< std::string > &opts)
 Read a string parameter without a default but with the input list of options for tab-completion (and validation) More...
 
static int readline_int (const std::string &prompt)
 Read an integer parameter without a default. More...
 
static int readline_int (const std::string &prompt, int aval)
 Read an integer parameter with a default. More...
 
static double readline_float (const std::string &prompt)
 Read a float parameter with a default. More...
 
static bool readline_bool (const std::string &prompt, bool aval)
 Read a bool parameter with a default. More...
 
static std::string readline_cmd ()
 Read a command from the menu. More...
 
static void add_to_command_queue (const std::string &str)
 Add to the queue of commands to execute automatically. More...
 

Protected Member Functions

void add_to_history (const std::string &cmd) const
 Add a command to the history of commands that have been executed. More...
 

Static Protected Attributes

static std::list< std::stringcmdTextQueue_
 the ordered list of commands to be executed from a script file
 
static std::vector< std::stringcmd_options_
 the current command options (for interfacing with readline's tab completion)
 
static const std::vector< std::string > * rl_comp_opts_ = &BaseMenu::cmd_options_
 a pointer to the list of options when attempting readline completion
 

Static Private Member Functions

static char * matcher (const char *text, int state)
 matcher function following readline's function signature More...
 

Detailed Description

Type-less menu base for common tasks.

This base menu class handles most of the interaction with the GNU readline library. This includes some helper commands for obtaining parameters via readline and keeping command history.

Member Function Documentation

◆ add_to_command_queue()

void BaseMenu::add_to_command_queue ( const std::string str)
static

Add to the queue of commands to execute automatically.

Parameters
[in]strcommand to add into the queue

◆ add_to_history()

void BaseMenu::add_to_history ( const std::string cmd) const
protected

Add a command to the history of commands that have been executed.

Uses readline's add_to_history.

Parameters
[in]cmdCommand string that was executed

◆ matcher()

char * BaseMenu::matcher ( const char *  text,
int  state 
)
staticprivate

matcher function following readline's function signature

We get the command options from the BaseMenu::rl_comp_opts_ which is determined by the Menu::command_options function at the beginning of Menu::steer or after leaving a sub-menu.

We check for matching with strncasecmp so that the tab completion is also case-insensitive (same as the menu selection itself).

Parameters
[in]textthe text to potentionally match
[in]state0 if first call, incrementing otherwise
Returns
matching string until all out, NULL at end

◆ readline() [1/3]

std::string BaseMenu::readline ( const std::string prompt)
static

Read a string parameter without a default.

See also
readline with an empty default value
Parameters
[in]promptThe informing the user what the parameter is
Returns
value input by user

◆ readline() [2/3]

std::string BaseMenu::readline ( const std::string prompt,
const std::string defval,
bool  preserve_last_blank = false 
)
static

Read in a parameter using the default value if nothing provided.

Expands '~' into $HOME within strings.

If the cmdTextQueue is not empty, then it uses the next command in that list rather than keyboard input. This leads to the effect that executables using this menu can provide an "initialization script" of commands by registering them in order with the BaseMenu::add_to_command_queue before launching the menu.

Parameters
[in]promptThe informing the user what the parameter is
[in]defvaldefault value if user does not provide anything
[in]preserve_last_blankkeep whitespace at end of input
Returns
value input by user or default value

◆ readline() [3/3]

std::string BaseMenu::readline ( const std::string prompt,
const std::vector< std::string > &  opts 
)
static

Read a string parameter without a default but with the input list of options for tab-completion (and validation)

Parameters
[in]promptThe informing the user what the parameter is
Returns
value input by user

◆ readline_bool()

bool BaseMenu::readline_bool ( const std::string prompt,
bool  aval 
)
static

Read a bool parameter with a default.

The characters y, Y, 1, t, and T are accepted as equivalent to "true" while all others are equivalent to "false". The prompt should be in the form of a yes/no question since the string ' [Y/N]' is appended to it before passing along.

Parameters
[in]promptThe informing the user what the parameter is
[in]defvaldefault value if user does not provide anything
Returns
value input by user

◆ readline_cmd()

std::string BaseMenu::readline_cmd ( )
static

Read a command from the menu.

We use the prompt ' > ' before the command and we wrap the underlying readline call with the setup/teardown of the TAB-completion function that readline calls if the user presses TAB.

Returns
command entered by user

◆ readline_float()

double BaseMenu::readline_float ( const std::string prompt)
static

Read a float parameter with a default.

Uses string readline and then converts the output string into a float using atof.

Parameters
[in]promptThe informing the user what the parameter is
[in]defvaldefault value if user does not provide anything
Returns
value input by user

◆ readline_int() [1/2]

int BaseMenu::readline_int ( const std::string prompt)
static

Read an integer parameter without a default.

Uses string readline and then converts the output string into a integer using stol.

Parameters
[in]promptThe informing the user what the parameter is
Returns
value input by user

◆ readline_int() [2/2]

int BaseMenu::readline_int ( const std::string prompt,
int  aval 
)
static

Read an integer parameter with a default.

Uses string readline and then converts the output string into a integer using stol.

Parameters
[in]promptThe informing the user what the parameter is
[in]defvaldefault value if user does not provide anything
Returns
value input by user

◆ readline_nosplit()

std::string BaseMenu::readline_nosplit ( const std::string prompt,
const std::string defval 
)
static

Get a raw input value without the additional splitting and modifications done in base readline.

Parameters
[in]promptThe informing the user what the parameter is
[in]defvaldefault value if user does not provide anything
Returns
value input by user or default value

The documentation for this class was generated from the following files: