CPPConsole
Loading...
Searching...
No Matches
cursor.hpp
Go to the documentation of this file.
1#ifndef __CPP_CONSOLE_CURSOR_HPP__
2#define __CPP_CONSOLE_CURSOR_HPP__
3
4#include <iostream>
5
6#ifdef __WIN32
7#include <Windows.h>
8#else
9#include <stdio.h>
10#include <sys/ioctl.h>
11#include <termios.h>
12#include <unistd.h>
13#endif
14
15#include "ansi.hpp"
16#include "console.hpp"
17#include "data.hpp"
18
22namespace console {
29#ifdef __WIN32
30 CONSOLE_SCREEN_BUFFER_INFO cbsi;
31 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cbsi);
32 return {cbsi.dwCursorPosition.X, cbsi.dwCursorPosition.Y};
33#else
34 ConsoleCoord coord;
35 struct termios term, term_orig;
36 tcgetattr(STDIN_FILENO, &term);
37 term_orig = term;
38
39 term.c_lflag &= ~(ICANON | ECHO);
40 tcsetattr(STDIN_FILENO, TCSANOW, &term);
41 std::cout << "\033[6n";
42 std::cin.ignore();
43
44 char ignore;
45 std::cin >> ignore >> coord.X;
46 std::cin.ignore();
47 std::cin >> coord.Y;
48
49 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
50 return coord;
51#endif
52}
53
58 if (isANSIEnabled()) {
59 std::cout << "\033[H";
60 return;
61 }
62#ifdef __WIN32
63 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
64 if (hConsole == INVALID_HANDLE_VALUE) {
65 std::cerr << "Error: Unable to get console handle." << std::endl;
66 return;
67 }
68 COORD homeCoords = {0, 0};
69 SetConsoleCursorPosition(hConsole, homeCoords);
70#endif
71}
72
79void moveCursorToPosition(int row, int col) {
80 if (isANSIEnabled()) {
81 std::cout << "\033[" << std::to_string(row) << ";"
82 << std::to_string(col) << "H";
83 return;
84 }
85#ifdef __WIN32
86 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
87 if (hConsole == INVALID_HANDLE_VALUE) {
88 std::cerr << "Error: Unable to get console handle." << std::endl;
89 return;
90 }
91 COORD coord;
92 coord.X = row;
93 coord.Y = col;
94 SetConsoleCursorPosition(hConsole, coord);
95#endif
96}
97
103void moveCursorUp(int n) {
104 if (isANSIEnabled()) {
105 std::cout << "\033[" << std::to_string(n) << "A";
106 return;
107 }
108#ifdef __WIN32
110 moveCursorToPosition(coord.X, coord.Y - n);
111#endif
112}
113
119void moveCursorDown(int n) {
120 if (isANSIEnabled()) {
121 std::cout << "\033[" << std::to_string(n) << "B";
122 return;
123 }
124#ifdef __WIN32
126 moveCursorToPosition(coord.X, coord.Y + n);
127#endif
128}
129
135void moveCursorRight(int n) {
136 if (isANSIEnabled()) {
137 std::cout << "\033[" << std::to_string(n) << "C";
138 return;
139 }
140#ifdef __WIN32
142 moveCursorToPosition(coord.X + n, coord.Y);
143#endif
144}
145
151void moveCursorLeft(int n) {
152 if (isANSIEnabled()) {
153 std::cout << "\033[" << std::to_string(n) << "D";
154 return;
155 }
156#ifdef __WIN32
158 moveCursorToPosition(coord.X - n, coord.Y);
159#endif
160}
161
168 if (isANSIEnabled()) {
169 std::cout << "\033[" << std::to_string(n) << "E";
170 return;
171 }
172#ifdef __WIN32
174 moveCursorToPosition(0, coord.Y + n);
175#endif
176}
177
184 if (isANSIEnabled()) {
185 std::cout << "\033[" << std::to_string(n) << "F";
186 return;
187 }
188#ifdef __WIN32
190 moveCursorToPosition(0, coord.Y - n);
191#endif
192}
193
200 if (isANSIEnabled()) {
201 std::cout << "\033[" << std::to_string(n) << "G";
202 return;
203 }
204#ifdef __WIN32
206 moveCursorToPosition(coord.X, n);
207#endif
208}
209} // namespace console
210
211#endif
Namespace Containing all console commands.
Definition ansi.hpp:16
void moveCursorLeft(int n)
Move the cursor left by n lines.
Definition cursor.hpp:151
ConsoleCoord getCursorPosition()
Gets the position of the cursor within the console.
Definition cursor.hpp:28
void moveCursorRight(int n)
Move the cursor right by n lines.
Definition cursor.hpp:135
void moveCursorToColumn(int n)
Moves cursor to column n.
Definition cursor.hpp:199
void moveCursorDown(int n)
Move the cursor down by n lines.
Definition cursor.hpp:119
void moveCursorDownAndToStart(int n)
Moves cursor to beginning of next line, n lines down.
Definition cursor.hpp:167
void moveCursorUpAndToStart(int n)
Moves cursor to beginning of previous line, n lines up.
Definition cursor.hpp:183
void moveCursorToPosition(int row, int col)
Move the cursor to the specified position in the console.
Definition cursor.hpp:79
void moveCursorUp(int n)
Move the cursor up by n lines.
Definition cursor.hpp:103
bool isANSIEnabled()
Checks if ANSI Codes are enable in windows.
Definition ansi.hpp:22
void moveCursorHome()
Moves cursor to home position (0, 0)
Definition cursor.hpp:57
A position on the console (row and column)
Definition data.hpp:13
int X
Definition data.hpp:14
int Y
Definition data.hpp:15