osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
Classes | Typedefs | Enumerations | Functions | Variables
osm Namespace Reference

Classes

class  Canvas
 Instances of this class are used to draw in a limited 2D space. All the functions that modify the canvas do it internally; to make the changes effective in the console, you must use the refresh method. More...
 
class  Plot2DCanvas
 This class is used to plot mathematical functions R -> R. In addition to all the properties of a basic Canvas, this one also has an offset and a scale for the plot. The offset is the first value in x and y to be represented in the canvas and the scale is the difference in x or y that each character represents relative to the previous one. For example: a canvas of size = (15, 10) with offset = (3,2) and scale = (7, 5) will represent the functions you draw from x=3 to x=3+15*7=108 and from y=2 to y=2+10*5=52. More...
 
class  Decorator
 Class used to decorate an output stream. Each setting is set permanently on the chosen output stream until the destructor is met or one of the reset functions is called. More...
 
struct  indices
 Type to generate indices for parameter packs. More...
 
struct  gen_indices
 Type to generate indices for parameter packs. More...
 
struct  gen_indices< 0, Is... >
 Type to generate indices for parameter packs. More...
 
class  make_MultiProgressBar
 Template class used to create multi progress bars. More...
 
struct  type_identity
 Struct used to to typedef the functor. More...
 
struct  updater
 Functor used to call the ProgressBar class update method. More...
 
class  ProgressBar
 Template class used to create customized progress bars. More...
 
class  OutputRedirector
 This class is used to redirected output to a file. More...
 
class  Stringbuf
 This class inherits std::stringbuf and adds additional functionality and thread safety. More...
 
class  Ostreambuf
 This class inherits Stringbuf and adds the ability to send output to a specific std::ostream buffer as well as redirect output to osm::redirout. More...
 

Typedefs

using string_pair_map = std::unordered_map< std::string, std::pair< std::string, std::string > >
 
using string_set_map = std::unordered_map< std::string, std::unordered_set< std::string > >
 
using steady_clock = std::chrono::steady_clock
 
using duration = std::chrono::duration< float, steady_clock::period >
 

Enumerations

enum  FrameStyle { EMPTY = 0 , ASCII = 1 , BOX = 2 }
 Enum class used to define the frame style. More...
 
enum class  ANSI_SEARCH { first , generic }
 
enum class  CURSOR { ON , OFF }
 It is used to store the OPTION function options for the cursor view. Current options are: ON to enable cursor view and OFF to disable it. More...
 
enum class  ANSI { ON , OFF }
 It is used to store the OPTION function options for the ANSI escape sequences enabling. Current options are: ON to enable and OFF to disable. More...
 
enum class  UNICODECH { ON , OFF }
 It is used to store the OPTION function options for the UNICODE characters enabling. Current options are: ON to enable and OFF to disable. More...
 

Functions

const std::string RGB (int32_t r, int32_t g, int32_t b)
 It takes three integers as arguments which are the corresponding rgb triplets of a color (see here for the full list of available colors http://www.aksiom.net/rgb.html). It can be used to output a wider variety of colors with respect to the standard available from the col map. More...
 
const std::string & feat (const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
 It takes an std::map object as the first argument and an std::string object (map key) as the second argument and returns the interested color / style feature by returning the map value from the corresponding key. More...
 
const std::string feat (const string_pair_map &generic_map, const std::string &feat_string, int32_t feat_int)
 This overload, with respect to the standard function definition, takes an std::map object as the first argument, but with an std::pair as second type and additionally takes an extra integer argument to correctly set the parameter of the crs map. More...
 
const std::string go_to (int32_t x, int32_t y)
 It takes two integers as arguments which are the x and y position of the cursor in the screen and returns the interested position you want to reach. More...
 
std::vector< std::string > split_string (const std::string &input, const std::string &regex)
 Function used to split a string based on a certain reges. More...
 
std::string getCommandOut (const char *command)
 Function used to get the output of a shell command. More...
 
std::string trim_string (const std::string &str)
 Removes all leading and trailing spaces from a string. More...
 
size_t find_first_alpha (std::string_view str, size_t pos)
 Finds the index of the first alphabetic character in the str, starting from the given position. More...
 
std::string get_formatted_from_ansi (const std::string &str, int32_t *last_pos, int32_t *last_size)
 Formats a string according to the ANSI (Control Sequence Introducer) escape sequences present within the string. Can be used in a loop with appending data by using optional arguments. More...
 
std::string get_ansi_csi_string (const std::string &str, size_t esc_pos)
 
int32_t get_ansi_csi_number (const std::string &csi)
 
char get_ansi_csi_code (const std::string &csi)
 
void enableANSI ()
 Function used to enable ANSI escape sequences on Windows. More...
 
void disableANSI ()
 Function used to disable ANSI escape sequences on Windows. More...
 
void enableUNICODE ()
 Function used to enable UNICODE characters (used for Windows only). More...
 
void disableUNICODE ()
 Function used to disable UNICODE characters (used for Windows only). More...
 
template<typename T >
std::ostream & operator<< (Decorator my_shell, const T &elem)
 Operator overload to output a modified ostream object which properties are set thanks to the Decorator class. More...
 
template<class... Indicators>
make_MultiProgressBar< typename std::remove_reference< Indicators >::type... > MultiProgressBar (Indicators &&...inds)
 Helper function used for deduction guides. More...
 
template<typename bar_type >
std::ostream & operator<< (std::ostream &os, const ProgressBar< bar_type > &pb)
 Operator << used to print the progress bar properties. More...
 
template<typename T_err = std::runtime_error>
T_err except_error_func (const std::string &beg="", std::string var=nullptr, const std::string &end="")
 Function used to throw customized stdexception error. This function is extremely specific to my purposes and you can find examples usages in other my projects lik "osmanip" or "SAFD-algorithm". More...
 
template<typename T >
bool is_escape (const T &str, const ANSI_SEARCH &flag)
 This method is used to check if an input variable is an ANSI escape sequency or not. More...
 
template<typename T >
roundoff (T value, unsigned char prec)
 Function to round a floating point to n-th decimal place after comma. More...
 
template<typename T >
bool isFloatingPoint (const T &)
 Function to check if an expression (not a type) is a floating point or not. I know this function is almost useless, but it has been created for lazy purposes and since it is used in other projects it cannot be eliminated now in order to not break backward compatibility. More...
 
template<typename T >
one (const T &iterating_var)
 Function to find the incremented unit of a loop. Not easy to understand its purpose without context, but it is used to get the loop incremented unit in case of loops with floating-point indexes. See example usage in progress bars "update" method. More...
 
template<typename T >
void OPTION (T opt)
 It is used to set a specific option. More...
 
std::string trim_string (std::string &str)
 
void handle_csi (const std::string &csi_str, std::string &dst_str, int32_t *dst_crsr_pos)
 

Variables

const std::unordered_map< std::string, std::string > col
 It is used to store the colors. Note: "bg" is the prefix of the background color features and "bd" is the one of the bold color features. More...
 
const std::unordered_map< std::string, std::string > sty
 It is used to store the styles. More...
 
const std::unordered_map< std::string, std::string > rst
 It is used to store the reset features commands. More...
 
const string_pair_map crs
 It is used to store the cursor commands. More...
 
const std::unordered_map< std::string, std::string > tcs
 It is used to store the terminal control sequences. More...
 
const string_pair_map tcsc
 It is used to store the terminal control sequences for clear line / screen. More...
 
Ostreambuf cout_buf {&std::cout}
 
OutputRedirector redirout {}
 Link to osm::cout. More...
 
std::string old_chcp
 
template<typename T >
const T null_str = ""
 
template<typename T >
const T empty_space = " "
 
std::ostream cout
 

Typedef Documentation

◆ duration

using osm::duration = typedef std::chrono::duration<float, steady_clock::period>

◆ steady_clock

using osm::steady_clock = typedef std::chrono::steady_clock

◆ string_pair_map

using osm::string_pair_map = typedef std::unordered_map<std::string, std::pair<std::string, std::string> >

◆ string_set_map

using osm::string_set_map = typedef std::unordered_map<std::string, std::unordered_set<std::string> >

Enumeration Type Documentation

◆ ANSI

enum osm::ANSI
strong

It is used to store the OPTION function options for the ANSI escape sequences enabling. Current options are: ON to enable and OFF to disable.

Enumerator
ON 
OFF 

◆ ANSI_SEARCH

enum osm::ANSI_SEARCH
strong
Enumerator
first 
generic 

◆ CURSOR

enum osm::CURSOR
strong

It is used to store the OPTION function options for the cursor view. Current options are: ON to enable cursor view and OFF to disable it.

Enumerator
ON 
OFF 

◆ FrameStyle

Enum class used to define the frame style.

Enumerator
EMPTY 
ASCII 
BOX 

◆ UNICODECH

enum osm::UNICODECH
strong

It is used to store the OPTION function options for the UNICODE characters enabling. Current options are: ON to enable and OFF to disable.

Enumerator
ON 
OFF 

Function Documentation

◆ disableANSI()

void osm::disableANSI ( )

Function used to disable ANSI escape sequences on Windows.

◆ disableUNICODE()

void osm::disableUNICODE ( )

Function used to disable UNICODE characters (used for Windows only).

◆ enableANSI()

void osm::enableANSI ( )

Function used to enable ANSI escape sequences on Windows.

◆ enableUNICODE()

void osm::enableUNICODE ( )

Function used to enable UNICODE characters (used for Windows only).

◆ except_error_func()

template<typename T_err = std::runtime_error>
T_err osm::except_error_func ( const std::string &  beg = "",
std::string  var = nullptr,
const std::string &  end = "" 
)
inline

Function used to throw customized stdexception error. This function is extremely specific to my purposes and you can find examples usages in other my projects lik "osmanip" or "SAFD-algorithm".

Template Parameters
T_errThe type of the exception error.
Parameters
begThe first part of the error message.
varThe variable to be inserted in the error message.
endThe last part of the error message.
Returns
T_err The modified exception error.

◆ feat() [1/2]

const std::string & osm::feat ( const std::unordered_map< std::string, std::string > &  generic_map,
const std::string &  feat_string 
)

It takes an std::map object as the first argument and an std::string object (map key) as the second argument and returns the interested color / style feature by returning the map value from the corresponding key.

Parameters
generic_mapThe feature map.
feat_stringThe feature name.
Returns
const std::string& The output feature.

◆ feat() [2/2]

const std::string osm::feat ( const string_pair_map generic_map,
const std::string &  feat_string,
int32_t  feat_int 
)

This overload, with respect to the standard function definition, takes an std::map object as the first argument, but with an std::pair as second type and additionally takes an extra integer argument to correctly set the parameter of the crs map.

Parameters
generic_mapThe feature map.
feat_stringThe feature name.
feat_intExtra integer argument to correctly set the parameter of the crs map.
Returns
const std::string The output feature.

◆ find_first_alpha()

size_t osm::find_first_alpha ( std::string_view  str,
size_t  pos 
)

Finds the index of the first alphabetic character in the str, starting from the given position.

Parameters
strthe string be searched.
posthe starting position.
Returns
the index of the alpha char if successful, otherwise std::string::npos.

◆ get_ansi_csi_code()

char osm::get_ansi_csi_code ( const std::string &  csi)

◆ get_ansi_csi_number()

int32_t osm::get_ansi_csi_number ( const std::string &  csi)

◆ get_ansi_csi_string()

std::string osm::get_ansi_csi_string ( const std::string &  str,
size_t  esc_pos 
)

◆ get_formatted_from_ansi()

std::string osm::get_formatted_from_ansi ( const std::string &  str,
int32_t *  last_pos,
int32_t *  last_size 
)

Formats a string according to the ANSI (Control Sequence Introducer) escape sequences present within the string. Can be used in a loop with appending data by using optional arguments.

Parameters
strthe string to be formatted.
last_pos[Optional] the final position (index) of the last string to be formatted.
last_size[Optional] the size of the last string to be formatted.
Returns
a correctly formatted string, free of any ANSI escape sequences.

◆ getCommandOut()

std::string osm::getCommandOut ( const char *  command)

Function used to get the output of a shell command.

Parameters
commandThe command which output is required.
Returns
std::string The output of the command variable.

◆ go_to()

const std::string osm::go_to ( int32_t  x,
int32_t  y 
)

It takes two integers as arguments which are the x and y position of the cursor in the screen and returns the interested position you want to reach.

Parameters
xThe x position of the cursor in the screen.
yThe y position of the cursor in the screen
Returns
const std::string The (x,y) position of the cursor in the screen.

◆ handle_csi()

void osm::handle_csi ( const std::string &  csi_str,
std::string &  dst_str,
int32_t *  dst_crsr_pos 
)

◆ is_escape()

template<typename T >
bool osm::is_escape ( const T &  str,
const ANSI_SEARCH flag 
)

This method is used to check if an input variable is an ANSI escape sequency or not.

Template Parameters
TTemplate type of the input variable.
Parameters
strThe input variable.
flagA flag which let to return different things with respect to its value. If flag = ANSI_SEARCH::first the ANSI is searched as the first substring of the str argument, otherwise, if flag = ANSI_SEARCH::generic the ANSI is searched as a substring inside the str argument.
Returns
true If the input variable is an ANSI escape sequency.
false Otherwise.

◆ isFloatingPoint()

template<typename T >
bool osm::isFloatingPoint ( const T &  )
inline

Function to check if an expression (not a type) is a floating point or not. I know this function is almost useless, but it has been created for lazy purposes and since it is used in other projects it cannot be eliminated now in order to not break backward compatibility.

Template Parameters
TThe type of the expression.
Returns
bool True if the expression is a floating point, false otherwise.

◆ MultiProgressBar()

template<class... Indicators>
make_MultiProgressBar<typename std::remove_reference<Indicators>::type...> osm::MultiProgressBar ( Indicators &&...  inds)

Helper function used for deduction guides.

Template Parameters
IndicatorsThe parameter pack of the various progress bar types.
Parameters
indsThe progress bars.
Returns
make_MultiProgressBar <typename std::remove_reference <Indicators>::type...> The update of the various progress bars.

◆ one()

template<typename T >
T osm::one ( const T &  iterating_var)
inline

Function to find the incremented unit of a loop. Not easy to understand its purpose without context, but it is used to get the loop incremented unit in case of loops with floating-point indexes. See example usage in progress bars "update" method.

Template Parameters
TThe type of the iterating variable of the loop.
Parameters
iterating_varThe iterating variable of the loop.
Returns
T The incremented unit of the loop.

◆ operator<<() [1/2]

template<typename T >
std::ostream& osm::operator<< ( Decorator  my_shell,
const T &  elem 
)

Operator overload to output a modified ostream object which properties are set thanks to the Decorator class.

Template Parameters
TThe template parameter of the object sent into the output stream.
Parameters
my_shellThe Decorator object.
elemThe element sent into the output stream.
Returns
std::ostream& The modified output stream.

◆ operator<<() [2/2]

template<typename bar_type >
std::ostream& osm::operator<< ( std::ostream &  os,
const ProgressBar< bar_type > &  pb 
)

Operator << used to print the progress bar properties.

Template Parameters
bar_typeIt is the type of the progress bar.
Parameters
osIt is the output stream used to print the progress bar.
pbIt is the progress bar object.
Returns
std::ostream& It is the output stream with the given progress bar properties printed out.

◆ OPTION()

template<typename T >
void osm::OPTION ( opt)
inline

It is used to set a specific option.

Template Parameters
TThe type of the given option.
Parameters
optThe option to be set.
Returns
void Set the chosen option.

◆ RGB()

const std::string osm::RGB ( int32_t  r,
int32_t  g,
int32_t  b 
)

It takes three integers as arguments which are the corresponding rgb triplets of a color (see here for the full list of available colors http://www.aksiom.net/rgb.html). It can be used to output a wider variety of colors with respect to the standard available from the col map.

Parameters
rThe r singlet of the tripled.
gThe g singlet of the tripled.
bThe b singlet of the tripled.
Returns
const std::string The rgb triplet of the color.

◆ roundoff()

template<typename T >
T osm::roundoff ( value,
unsigned char  prec 
)
inline

Function to round a floating point to n-th decimal place after comma.

Template Parameters
TThe Type of the variable to be rounded.
Parameters
valueThe variable to be rounded
precPrecision of the rounding.
Returns
T The rounded variable.

◆ split_string()

std::vector< std::string > osm::split_string ( const std::string &  input,
const std::string &  regex 
)

Function used to split a string based on a certain reges.

Parameters
inputThe input string.
regexThe regex.
Returns
std::vector<std::string> The collection of string elements splitted by the regex.

◆ trim_string() [1/2]

std::string osm::trim_string ( const std::string &  str)

Removes all leading and trailing spaces from a string.

Parameters
strthe string to be trimmed.
Returns
string with no leading or tailing spaces.

◆ trim_string() [2/2]

std::string osm::trim_string ( std::string &  str)

Variable Documentation

◆ col

const std::unordered_map< std::string, std::string > osm::col

It is used to store the colors. Note: "bg" is the prefix of the background color features and "bd" is the one of the bold color features.

◆ cout

std::ostream osm::cout
extern

◆ cout_buf

std::ostream cout & osm::cout_buf {&std::cout}

◆ crs

const string_pair_map osm::crs
Initial value:
= {
{"error", std::make_pair("Inserted cursor command", "")},
{"up", std::make_pair("\u001b[", "A")},
{"down", std::make_pair("\u001b[", "B")},
{"right", std::make_pair("\u001b[", "C")},
{"left", std::make_pair("\u001b[", "D")}}

It is used to store the cursor commands.

◆ empty_space

template<typename T >
const T osm::empty_space = " "
inline

◆ null_str

template<typename T >
const T osm::null_str = ""
inline

◆ old_chcp

std::string osm::old_chcp

◆ redirout

OutputRedirector osm::redirout {}

Link to osm::cout.

Linked to standard output.

◆ rst

const std::unordered_map< std::string, std::string > osm::rst
Initial value:
= {
{"error", "Inserted reset command"},
{"all", "\033[0m"},
{"color", "\033[39m"},
{"bg color", "\033[49m"},
{"bd color", "\033[22m \033[39m"},
{"bd/ft", "\033[22m"},
{"italics", "\033[23m"},
{"underlined", "\033[24m"},
{"blink", "\033[25m"},
{"inverse", "\033[27m"},
{"invisible", "\033[28m"},
{"crossed", "\033[29m"},
}

It is used to store the reset features commands.

◆ sty

const std::unordered_map< std::string, std::string > osm::sty
Initial value:
= {
{"error", "Inserted style"},
{"bold", "\033[1m"},
{"faint", "\033[2m"},
{"italics", "\033[3m"},
{"underlined", "\033[4m"},
{"blink", "\033[5m"},
{"inverse", "\033[7m"},
{"invisible", "\033[8m"},
{"crossed", "\033[9m"},
{"d-underlined", "\033[21m"}}

It is used to store the styles.

◆ tcs

const std::unordered_map< std::string, std::string > osm::tcs
Initial value:
= {
{"error",
"Inserted terminal control "
"sequence"},
{"bell", "\x07"},
{"bksp", "\x08"},
{"tab", "\x09"},
{"lfd", "\x0A"},
{"ffd", "\x0C"},
{"crt", "\x0D"},
{"hcrs", "\u001b[?25l"},
{"scrs", "\u001b[?25h"}
}

It is used to store the terminal control sequences.

◆ tcsc

const string_pair_map osm::tcsc
Initial value:
= {
{"error", std::make_pair("Inserted terminal control sequence", "")},
{"csc", std::make_pair("\u001b[", "J")},
{"cln", std::make_pair("\u001b[", "K")}
}

It is used to store the terminal control sequences for clear line / screen.