Merge pull request #48 from motis-project/win-cursor-mv

Abstraction for Cursor Movements (Issue #46)
This commit is contained in:
Pranav
2020-04-26 10:28:57 -07:00
committed by GitHub
9 changed files with 119 additions and 20 deletions

View File

@@ -110,14 +110,16 @@ If you'd rather control progress of the bar in discrete steps, consider using `b
```cpp ```cpp
#include <chrono> #include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
int main() { int main() {
using namespace indicators;
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; show_console_cursor(false);
using namespace indicators;
ProgressBar bar{ ProgressBar bar{
option::BarWidth{50}, option::BarWidth{50},
option::Start{"["}, option::Start{"["},
@@ -157,7 +159,7 @@ int main() {
bar.mark_as_completed(); bar.mark_as_completed();
// Show cursor // Show cursor
std::cout << "\e[?25h"; show_console_cursor(true);
return 0; return 0;
} }
@@ -173,11 +175,16 @@ All progress bars and spinners in `indicators` support showing time elapsed and
```cpp ```cpp
#include <chrono> #include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
int main() { int main() {
using namespace indicators; using namespace indicators;
// Hide cursor
show_console_cursor(false);
indicators::ProgressBar bar{ indicators::ProgressBar bar{
option::BarWidth{50}, option::BarWidth{50},
option::Start{" ["}, option::Start{" ["},
@@ -201,7 +208,7 @@ int main() {
} }
// Show cursor // Show cursor
std::cout << "\e[?25h"; show_console_cursor(true);
return 0; return 0;
} }
@@ -222,10 +229,11 @@ Are you in need of a smooth block progress bar using [unicode block elements](ht
int main() { int main() {
// Hide cursor
std::cout << "\e[?25l";
using namespace indicators; using namespace indicators;
// Hide cursor
show_console_cursor(false);
BlockProgressBar bar{ BlockProgressBar bar{
option::BarWidth{80}, option::BarWidth{80},
option::Start{"["}, option::Start{"["},
@@ -245,7 +253,7 @@ int main() {
} }
// Show cursor // Show cursor
std::cout << "\e[?25h"; show_console_cursor(true);
return 0; return 0;
} }

View File

@@ -1,12 +1,14 @@
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <indicators/progress_spinner.hpp> #include <indicators/progress_spinner.hpp>
#include <indicators/cursor_control.hpp>
#include <vector> #include <vector>
int main() { int main() {
using namespace indicators;
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; show_console_cursor(false);
using namespace indicators;
{ {
// //
// PROGRESS BAR 1 // PROGRESS BAR 1
@@ -312,7 +314,7 @@ int main() {
} }
// Show cursor // Show cursor
std::cout << "\e[?25h"; show_console_cursor(true);
return 0; return 0;
} }

View File

@@ -0,0 +1,35 @@
#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 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 show_console_cursor(bool const show) {
std::fputs(show ? "\e[?25h" : "\e[?25l", stdout);
}
#endif
} // namespace indicators

View 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

View File

@@ -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";

View File

@@ -1,11 +1,12 @@
#include <chrono> #include <chrono>
#include <indicators/block_progress_bar.hpp> #include <indicators/block_progress_bar.hpp>
#include <indicators/cursor_control.hpp>
#include <thread> #include <thread>
int main() { int main() {
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; indicators::show_console_cursor(false);
indicators::BlockProgressBar bar{ indicators::BlockProgressBar bar{
indicators::option::BarWidth{80}, indicators::option::BarWidth{80},
@@ -24,7 +25,7 @@ int main() {
} }
// Show cursor // Show cursor
std::cout << "\e[?25h"; indicators::show_console_cursor(true);
return 0; return 0;
} }

View File

@@ -1,11 +1,12 @@
#include <chrono> #include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
int main() { int main() {
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; indicators::show_console_cursor(false);
indicators::ProgressBar bar{ indicators::ProgressBar bar{
indicators::option::BarWidth{50}, indicators::option::BarWidth{50},
@@ -47,7 +48,7 @@ int main() {
bar.mark_as_completed(); bar.mark_as_completed();
// Show cursor // Show cursor
std::cout << "\e[?25h"; indicators::show_console_cursor(true);
return 0; return 0;
} }

View File

@@ -1,9 +1,10 @@
#include <indicators/cursor_control.hpp>
#include <indicators/progress_spinner.hpp> #include <indicators/progress_spinner.hpp>
int main() { int main() {
// Hide cursor // Hide cursor
std::cout << "\e[?25l"; indicators::show_console_cursor(false);
indicators::ProgressSpinner spinner{ indicators::ProgressSpinner spinner{
indicators::option::PostfixText{"Checking credentials"}, indicators::option::PostfixText{"Checking credentials"},
@@ -34,7 +35,7 @@ int main() {
thread.join(); thread.join();
// Show cursor // Show cursor
std::cout << "\e[?25h"; indicators::show_console_cursor(true);
return 0; return 0;
} }

View File

@@ -1,8 +1,12 @@
#include <chrono> #include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/progress_bar.hpp> #include <indicators/progress_bar.hpp>
#include <thread> #include <thread>
int main() { int main() {
// Hide cursor
indicators::show_console_cursor(false);
indicators::ProgressBar bar{ indicators::ProgressBar bar{
indicators::option::BarWidth{50}, indicators::option::BarWidth{50},
indicators::option::Start{" ["}, indicators::option::Start{" ["},
@@ -27,7 +31,7 @@ int main() {
} }
// Show cursor // Show cursor
std::cout << "\e[?25h"; indicators::show_console_cursor(true);
return 0; return 0;
} }