pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
pflib::menu::BaseMenu Class Reference

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

#include <Menu.h>

Inheritance diagram for pflib::menu::BaseMenu:
[legend]
Collaboration diagram for pflib::menu::BaseMenu:
[legend]

Static Public Member Functions

static void set_history_filepath (std::string fp)
 Decide where the filepath for reading/writing the history should be.
 
static void open_history ()
 open history and read from file (if it exists)
 
static void close_history ()
 close up history
 
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.
 
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.
 
static std::string readline (const std::string &prompt)
 Read a string parameter without a default.
 
static std::string readline (const std::string &prompt, const std::vector< std::string > &opts, const std::string &def="")
 Read a string parameter without a default but with the input list of options for tab-completion.
 
static std::string default_path (const std::string &name, const std::string &extension="")
 Create a default path from the output directory and timestamp format parameters.
 
static std::string readline_path (const std::string &name, const std::string &extension="")
 Read a path from the user using default_path to generate a default value.
 
static int readline_int (const std::string &prompt)
 Read an integer parameter without a default.
 
static int readline_int (const std::string &prompt, int aval, bool ashex=false)
 Read an integer parameter with a default.
 
static double readline_float (const std::string &prompt)
 Read a float parameter with a default.
 
static bool readline_bool (const std::string &prompt, bool aval)
 Read a bool parameter with a default.
 
static std::string readline_cmd ()
 Read a command from the menu.
 
static void add_to_command_queue (const std::string &str)
 Add to the queue of commands to execute automatically.
 

Static Public Attributes

static std::string output_directory = ""
 output directory to include in default path
 
static std::string timestamp_format = "_%Y%m%d_%H%M%S"
 format of timestamp to append to default path
 

Protected Member Functions

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

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_
 a pointer to the list of options when attempting readline completion
 
::pflib::logging::logger the_log_ = ::pflib::logging::get("menu")
 

Static Private Member Functions

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

Static Private Attributes

static std::string history_filepath_ = ".pflib-menu-history"
 

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 pflib::menu::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 pflib::menu::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

◆ default_path()

std::string pflib::menu::BaseMenu::default_path ( const std::string & name,
const std::string & extension = "" )
static

Create a default path from the output directory and timestamp format parameters.

Parameters
[in]namename to be included in output filename before timestamp
[in]extensionextension of file including .
Returns
filepath string that looks like [BaseMenu::output_directory/]{name}{timestamp}[extension] where {timestamp} is created from the current time and BaseMenu::timestamp_format

◆ matcher()

char * pflib::menu::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

◆ open_history()

void pflib::menu::BaseMenu::open_history ( )
static

open history and read from file (if it exists)

The history is stored at a file defined by set_history_filepath which is updated when the menu exits (either normally or with Ctrl+C).

◆ readline() [1/3]

std::string pflib::menu::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 pflib::menu::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 pflib::menu::BaseMenu::readline ( const std::string & prompt,
const std::vector< std::string > & opts,
const std::string & def = "" )
static

Read a string parameter without a default but with the input list of options for tab-completion.

Parameters
[in]promptPrompt string to the user
[in]optslist of options for tab completion of user entry
[in]defdefault value if user provides nothing
Returns
value chosen by user

◆ readline_bool()

bool pflib::menu::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 pflib::menu::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 pflib::menu::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 pflib::menu::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 pflib::menu::BaseMenu::readline_int ( const std::string & prompt,
int aval,
bool ashex = false )
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 pflib::menu::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

◆ readline_path()

std::string pflib::menu::BaseMenu::readline_path ( const std::string & name,
const std::string & extension = "" )
static

Read a path from the user using default_path to generate a default value.

The prompt is "Filename: " if an extension is given or "Filename (no extension):" if not. The arguments are then passed on to BaseMenu::default_path to create a default path for the user to confirm or rewrite if they desire.

Member Data Documentation

◆ rl_comp_opts_

const std::vector< std::string > * pflib::menu::BaseMenu::rl_comp_opts_
staticprotected
Initial value:
=
static std::vector< std::string > cmd_options_
the current command options (for interfacing with readline's tab completion)
Definition Menu.h:224

a pointer to the list of options when attempting readline completion

◆ timestamp_format

std::string pflib::menu::BaseMenu::timestamp_format = "_%Y%m%d_%H%M%S"
static

format of timestamp to append to default path

The maximum size of the resulting timestamp is 64 characters, which should be more than enough. A complete specification of time down to the second is 14 characters, so this gives you the ability to add 50 characters of separators and other suffix-related things.


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