mirror of
https://github.com/p-ranav/indicators.git
synced 2025-12-16 04:18:51 +08:00
cursor movement and cursor hiding support for windows
This commit is contained in:
43
include/indicators/cursor_control.hpp
Executable file
43
include/indicators/cursor_control.hpp
Executable file
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#if !defined(NOMINMAX)
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <cstdio>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace indicators {
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
|
||||||
|
void enable_cursor_movement() {
|
||||||
|
auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
SetConsoleMode(hStdout,
|
||||||
|
ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||||
|
}
|
||||||
|
|
||||||
|
void show_console_cursor(bool const show) {
|
||||||
|
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
|
CONSOLE_CURSOR_INFO cursorInfo;
|
||||||
|
|
||||||
|
GetConsoleCursorInfo(out, &cursorInfo);
|
||||||
|
cursorInfo.bVisible = show; // set the cursor visibility
|
||||||
|
SetConsoleCursorInfo(out, &cursorInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void enable_cursor_movement() {}
|
||||||
|
|
||||||
|
void show_console_cursor(bool const show) {
|
||||||
|
std::fputs(show ? "\e[?25h" : "\e[?25l", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace indicators
|
||||||
46
include/indicators/cursor_movement.hpp
Executable file
46
include/indicators/cursor_movement.hpp
Executable file
@@ -0,0 +1,46 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
#if !defined(NOMINMAX)
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <iostream>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace indicators {
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
void move(int x, int y) {
|
||||||
|
auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
if (!hStdout)
|
||||||
|
return;
|
||||||
|
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
||||||
|
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
|
||||||
|
|
||||||
|
COORD cursor;
|
||||||
|
|
||||||
|
cursor.X = csbiInfo.dwCursorPosition.X + x;
|
||||||
|
cursor.Y = csbiInfo.dwCursorPosition.Y + y;
|
||||||
|
SetConsoleCursorPosition(hStdout, cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
void move_up(int lines) { move(0, -lines); }
|
||||||
|
void move_down(int lines) { move(0, -lines); }
|
||||||
|
void move_right(int cols) { move(cols, 0); }
|
||||||
|
void move_left(int cols) { move(-cols, 0); }
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void move_up(int lines) { std::cout << "x1b[" << lines << "A"; }
|
||||||
|
void move_down(int lines) { std::cout << "x1b[" << lines << "B"; }
|
||||||
|
void move_right(int cols) { std::cout << "x1b[" << cols << "C"; }
|
||||||
|
void move_left(int cols) { std::cout << "x1b[" << cols << "D"; }
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace indicators
|
||||||
@@ -27,11 +27,13 @@ SOFTWARE.
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <indicators/color.hpp>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <indicators/cursor_movement.hpp>
|
||||||
|
#include <indicators/color.hpp>
|
||||||
|
|
||||||
namespace indicators {
|
namespace indicators {
|
||||||
|
|
||||||
template <typename Indicator, size_t count> class MultiProgress {
|
template <typename Indicator, size_t count> class MultiProgress {
|
||||||
@@ -86,8 +88,7 @@ private:
|
|||||||
void print_progress() {
|
void print_progress() {
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
if (started_)
|
if (started_)
|
||||||
for (size_t i = 0; i < count; ++i)
|
move_up(count);
|
||||||
std::cout << "\x1b[A";
|
|
||||||
for (auto &bar : bars_) {
|
for (auto &bar : bars_) {
|
||||||
bar.get().print_progress(true);
|
bar.get().print_progress(true);
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
#include <indicators/multi_progress.hpp>
|
#include <indicators/multi_progress.hpp>
|
||||||
#include <indicators/progress_bar.hpp>
|
#include <indicators/progress_bar.hpp>
|
||||||
|
#include <indicators/cursor_control.hpp>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
indicators::enable_cursor_movement();
|
||||||
|
indicators::show_console_cursor(false);
|
||||||
|
|
||||||
indicators::ProgressBar bar1{indicators::option::BarWidth{50},
|
indicators::ProgressBar bar1{indicators::option::BarWidth{50},
|
||||||
indicators::option::Start{"["},
|
indicators::option::Start{"["},
|
||||||
@@ -81,5 +84,7 @@ int main() {
|
|||||||
second_job.join();
|
second_job.join();
|
||||||
third_job.join();
|
third_job.join();
|
||||||
|
|
||||||
|
indicators::show_console_cursor(true);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user