Code Monkey home page Code Monkey logo

csi's Introduction

Why is this library useful?

csi provides easy to use methods that return special strings for controlling the terminal. For example, enable(style::bold) and disable(style::bold) return "\033[1m" and "\033[21m" which the terminal interprets as commands to enable and disable bold.

How to print text with style:

The enable and disable methods take style values and return strings that enable or disable a style. style is an enum class of 5 styles: bold, italics, underline, strikethrough, and inverse. inverse inverts the text color and the background color as-if the text is highlighted.

#include <iostream>
#include "csi.hpp"

int main(){
	/* Enable bold, print "Hello World!",
	   disable bold, then print a newline. */
	std::cout << csi::enable(csi::style::bold)
	          << "Hello World!"
	          << csi::disable(csi::style::bold)
	          << std::endl;
}

How to print text with color:

The foreground and background methods take color values and return strings that set the text color or the background color. color is an enum class of 9 colors: none, black, red, green, yellow, blue, magenta, cyan, and white. none is the terminal's default color.

#include <iostream>
#include "csi.hpp"

int main(){
	/* Set the text color to yellow, print "Hello World!",
	   set the text color to default, then print a new line */
	std::cout << csi::foreground(csi::color::yellow)
	          << "Hello World!"
	          << csi::foreground()
	          << std::endl;
		  
	/* Set the background color to blue, print "Hello World!", 
	 * set the background color to default, then print a new line */
	std::cout << csi::background(csi::color::blue)
	          << "Hello World!"
	          << csi::background()
	          << std::endl;
}

How to move the cursor:

There are 7 methods for moving the cursor: cursor_up, cursor_down, cursor_forward, cursor_back, cursor_next_line, cursor_previous_line, and cursor_position. cursor_position accepts two parameters: row and column, both 1 by default (top-left corner) while the other methods accept a single parameter, also 1 by default.

#include <iostream>
#include "csi.hpp"

int main(){
	/* Set the cursor position to the second column of
	   the second row, print "Hello World!", then print
	   a new line. */
	std:: cout << csi::cursor_position(2, 2)
	           << "Hello World!"
	           << std::endl;
}

How to erase the display:

The erase_display and erase_line methods accept erase_mode values as parameters and return strings which erase lines or even the whole display. erase_mode is an enum class which defines three modes: to_end, to_beginning, and all. to_end erases from the cursor to the end of the line/display. to_beginning erases from the cursor to the beginning of the line/display. all erases the entire line/display. For both methods, erase_mode is all by default.

#include <iostream>
#include "csi.hpp"

int main(){
	/* Erase the display, set the cursor position to
	   the top-left, print "Hello World!", then print
	   a new line */
	std:: cout << csi::erase_display()
	           << csi::cursor_position()
	           << "Hello World!"
	           << std::endl;
}

csi's People

Contributors

livibetter avatar wesofx avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.