How do I address the screen on a linux terminal? - c++

I am teaching myself C++ and currently run the latest Fedora.
On a Windows command prompt you can address the screen location.
I am led to the believe that a Linux command terminal works, effectively, like characters printed to a piece of paper. i.e. You can't go "up" from where you are.
However, when you install in Linux, there is a progress bar (in text) which doesn't appear to print a new line each time with the increase of progress.
Also, take for example raspbi-config in which you can tab/arrow up and down a "menu" and select "OK" and "Cancel" etc.
How does one achieve this?
Can this then be used to do simple text graphics applications in Linux like one can do under Windows?
Any help would be appreciated.
Uberlinc.
Coding in C++.
General non-gui apps which simply "cout" to the stdout.
Only found one or two examples which show a progress bar that prints out to the same line, but are not clear to me.

On a basic level, there are two things that enable these kinds of things on a Linux (or other POSIX) terminal: ASCII control characters and ANSI escape codes.
For a simple progress bar, it's enough to know how wide the screen is and to have a way to get back to the beginning of the current terminal line. This can be one by reading the environment variable $COLUMNS with getenv and by printing the ASCII control character \r. A very bare-bones and unbeautified example is
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
int main() {
// Default screen width
int width = 80;
// Get screenwidth from environment
char const *envwidth = getenv("COLUMNS");
if(envwidth != nullptr) {
std::istringstream(envwidth) >> width;
}
// save some space for numeric display
width -= 10;
for(int i = 1; i < width; ++i) {
std::cout
<< '\r' // go back to start of line
<< std::string(i, '.') // progress bar
<< std::string(width - i, ' ') // padding
<< "| " << i << '/' << width // numeric display
<< std::flush; // make the progress bar instantly visible
// pretend to work
usleep(100000);
}
}
For more involved terminal "graphics,", there are ANSI escape codes. These allow you to set foreground and background color, move around on the screen at will, and a few other things, by printing special character sequences to stdout. For example,
std::cout << "\x1b[10;20HHello, world." << std::flush;
will print Hello, world. at position 10, 20 on the screen.
There is also the ncurses library that provides more high-level terminal UI functionality (windows, dialogs, that sort of thing). They use ANSI escape codes under the hood, but it's normally nicer to use that than to roll your own UI framework.

Related

Is there a way to change color without outputting the ANSI escape sequence in C++?

I am on android, using termux and clang. I am trying to do something like the echo command but with colored output (for fun.)
I have this code, and the normal outputting works, but I'm confused on how to change color without the ANSI escape sequence being outputted with the text?
This is my code:
#define RESET "\033[0m"
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
for (int i = 2; i < argc; ++i) {
cout << argv[1] << argv[i] << " " << RESET;
}
}
You will need to use a library that understands how to write to the terminal.
You could look at ncurses.
Its been a while since I did any of this stuff but if you want to draw (term used loosely) on the terminal this is a simple library that abstracts away particular terminal devices.
It is used by people writing OS installers who don't know what graphics software is available and so must write user interfaces that work in the terminal.
Here is a getting stared guide:
Hello World
ncurses Colored Text
Getting Started Guid

Let user type in the middle of string (c/c++)?

Normally c/cpp string displayed in console only allows user to type after it.
Is there any simple way to let user to type in the middle of the output string, e.g fill in the blank:
Mr ____ is the teacher.
std::cout can print it easily, but how to let user type directly in the blank with simple code and read it? And e.g. if the name is long the move the printed character to the right?
You're looking for controlling a console/terminal. Neither of the languages you ask about has any notion of that -- they both only know streams of input and output. This is a simple abstraction, input and output are done character by character, in sequence. Input doesn't have to come from a keyboard, output doesn't have to be a screen or terminal ...
Controlling the contents of a screen is very platform-dependent. If you are on windows, the windows API provides a bunch of functions for controlling a console.
If you want to do something cross-platform, have a look at curses. There are implementations for many platforms, like ncurses (often used on *nix systems) and pdcurses (which is quite good for windows) and they all provide the same interface.
To learn about curses programming, the NCURSES Programming HOWTO is a good start. Just replace #include <ncurses.h> with #include <curses.h> so your code isn't tied specifically to ncurses but works with any curses implementation.
Yes, you can absolutely do this (I mean, ever played snake? All games were on terminals back then, and your problem is much simpler than writing a game).
A trick is using \r, which is a carriage return. That character will slide you back to the start of the line, allowing you to overwrite the previous string. This is commonly used for loading animations like
[---]
[=---]
[==-]
[===]
To prevent forcing the user to hit enter before sending data, I'll show a Linux/Mac solution.
system("/bin/stty raw"); // Get keystrokes immediately, #include <stdlib.h>
string s;
char c;
cout << "Mr _ is the teacher." << flush;
while( c = getchar() ) { // #include <stdio.h>
if( c == 3 ) // CTRL+C
exit(1);
if( c == 13 ) { // Newline
cout << endl;
break;
}
if( c == 127 ) { // Backspace
if( s.size() > 0 )
s.pop_back();
} else {
s += c;
}
cout << "\r"; // Reset the cursor
cout << "Mr " << s << "_ is the teacher. " << flush; // Spaces to cover invalid backspace character
cout << "\r"; // Reset the cursor
cout << "Mr " << s << "_ is the teacher." << flush;
}
system("/bin/stty cooked"); // Go back to buffered input
This can be done in Windows by importing #include<conio.h>, and then using getch() instead of getchar(). (You don't need any stty system commands)
Make sure to use your platform-specific #ifdef's to make your code portable!

How would I go about replacing text already on the command prompt? C++

I was wondering if it was possible to replace the text "[1] example1 [2] example2" after keyboard input with "example1extended" that's already on the screen.
Something like system("CLS"); but for only a certain line of text.
int main() //just an example
{
int ans;
std::cout << "[1] example1 [2] example2" << std::endl;
std::cout << "enter a choice: ";
std::cin >> ans;
if (ans == 1)
{
std::cout << "example1extended" << std::endl;
}
}
At first: There is no "screen" for c++. There is only input and output to "something" which is a typically a terminal. But how this terminal behaves is not part of c++ standard and as this not portable. So the results with different terminals and especially on different OS are different.
If you are working with a terminal which have e.g. VT100 support, you can use the special characters to control the cursor and erase chars on the terminal screen. https://en.wikipedia.org/wiki/VT100 https://www.csie.ntu.edu.tw/~r92094/c++/VT100.html
There are hundreds of libraries arround which are dealing with such kind of terminal ( emulators ).
There's no platform independent way of doing that but since you mentioned system('cls') I assume you are on Windows.
Windows has the Console Functions API which is a set of utility functions used for manipulating the console. You have 2 options here:
Set the cursor's position and overwrite it with spaces:
#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordsToDelete = { row, col }; // the coordinates of what you want to delete
// Move the current cursor position back. Writing with std::cout will
// now print on those coordinates
::SetConsoleCursorPosition(consoleHandle, position);
// This will replace the character at (row, col) with space.
// Repeat as many times as you need to clear the line
std::cout << " ";
Alternatively, you can get the entire console buffer and directly modify it:
#include <windows.h>
..
auto consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// Get a handle to the console buffer
GetConsoleScreenBufferInfo(consoleHandle, &csbi));
DWORD count;
COORD coords = { row, 0 };
DWORD cellCount = /* the length of your row */;
// Write the whitespace character to the coordinates in cellCount number of cells
// starting from coords. This effectively erases whatever has been written in those cells
FillConsoleOutputCharacter(
consoleHandle,
(TCHAR) ' ',
cellCount,
coords,
&count);

How do I move the console cursor to (x, y) on unix?

I have used <windows.h> and <conio.h> on windows for this kind of thing, but on unix the only thing I can find is <ncurses.h>, which uses a lot of C and doesn't support a lot of C++ functions. How can I move the console cursor to (x, y), while also being able to do object-oriented programming?
Edit: I'm trying to make simple games in C++ using the console as a display. I know it's not ideal to do so, but this is for a project that can't use Visual C++ or any other graphics. Think something like snake or minesweeper. I need to be able to cout in different locations, without updating the entire screen in the process. It needs to be compatible with unix systems.
One very simple way is through ANSI escape codes:
#include <iostream>
void moveCursor(std::ostream& os, int col, int row)
{
os << "\033[" << col << ";" << row << "H";
}
int main()
{
moveCursor(std::cout, 1,1);
std::cout << "X (1,1)";
moveCursor(std::cout, 13,8);
std::cout << "X (13,8)" << std::endl;
return 0;
}
The sequence <ESC>[ row , col H (the escape character is ASCII 27 or octal '\033') performs absolut cursor positioning. On most common terminals this should place one "X" in the top-left corner, and the second in column 13, row 8 (counts are 1-based).
Edit: hvd’s comment is of course spot-on: This is very simple, but ncurses is complex for a reason. It is guaranteed to work more reliably and in a much wider variety of settings than a plain escape code. Depending on what you actually want to achieve, I agree with hvd that you should be very careful before picking this simple hack as the solution to your problem.

In C/C++, how do you edit a certain 'coordinate' in stdout?

I've been using Vim a lot lately, and I was wondering how the program manages to change the characters at certain positions in the terminal. For example, when using :rc, it replaces the character under the cursor with c.
I have also seen similar things done with Homebrew, which prints a progress bar to the screen and updates it when necessary.
How is this done in C/C++?
There is no standard way of doing this in C++.
It is done with OS dependent lbiraries, such as curses and similar libraries (ncurses) in the Unix/Linux world. Some of these libraries have been ported on across platforms (example: PDCurses)
For very simple things such as a progress bar or a counter, and as long as you remain on a single line there is the trick of using "\r" (carriage return) in the output, to place the cursor back at the begin of the current line. Example:
for (int i = 0; i < 100; i++) {
cout << "\rProgress: " << setw(3) << i;
this_thread::sleep_for(chrono::milliseconds(100));
}
Certainly, using ncurses or similar library is a good answer. An alternative may be to use ANSI Escape Codes to control the cursor in some terminal emulators (but not Windows command shell). For example, this code prints a line in multiple colors and then moves the cursor to 2,2 (coordinates are 1-based with 1,1 being the upper left corner) and prints the word "red" in the color red.
#include <iostream>
#include <string>
const std::string CSI{"\x1b["};
const std::string BLUE{CSI + "34m"};
const std::string RED{CSI + "31m"};
const std::string RESET{CSI + "0m"};
std::ostream &curpos(int row, int col)
{
return std::cout << CSI << row << ';' << col << 'H';
}
int main()
{
std::cout << "This is " << BLUE << "blue" << RESET << " and white.\n";
curpos(2,2);
std::cout << RED << "red" << RESET << '\n';
}
As mentioned that's not a matter of any C/C++ standard operations provided with stdout or cout (besides writing the necessary control characters to the screen).
Controlling the screen cursor of an ASCII terminal totally depends on implementation of the particular terminal program used, and besides a very narrow set of control characters, there's no standard established.
There are libraries like ncurses for a broader variety of linux terminal implementations, or PDcurses for a windows CMD shell.
I'm not sure to understand you completely but with creating an array of 100 elements of type char you can modify any position of the array and loop it with a std:cout to mostrate it on the console.
Perhaps could be better define the array of 50 chars to resuce the size of the printed result.
For example, if you have to print a progessbar in the 1% process, you should print:
Char progressbar[100] = {'X','','','','','','','','',........}