CursesHelper.hpp
Go to the documentation of this file.
1 #ifndef CURSES_HELPER_HPP
2 #define CURSES_HELPER_HPP
3 
4 #include <ncursesw/ncurses.h>
5 #include <locale>
6 #include <iostream>
7 #include <signal.h>
8 
9 static mmask_t MOUSE_MOVE = 134217728;
10 static mmask_t MOUSE_SCROLL_UP = 524288;
11 
12 static short CURSES_NUM_GRAYS = 200;
13 static short CURSES_CUSTOM_COLORS_SHIFT = 10;
14 
15 
16 static void destroy_curses()
17 {
18  endwin();
19  // disable mouse movements
20  printf("\033[?1003l\n");
21 }
22 
23 static void curses_sigint(int sig)
24 {
25  int W = getmaxx(stdscr);
26  int H = getmaxy(stdscr);
27  mvprintw(H-1, W-1-6, "SIGINT");
28  // destroy_curses();
29  // exit(0);
30 }
31 
32 static void set_num_grays(short num_grays)
33 {
34  CURSES_NUM_GRAYS = num_grays;
35 }
36 
37 static void init_grayscale(short num_grays)
38 {
39  set_num_grays(num_grays);
40  init_color(COLOR_BLACK, 0, 0, 0);
41 
42  short step = static_cast<short>(1000 / num_grays);
43 
44  // init colors
45  for(short i=0; i < num_grays; i++)
46  {
47  short color_id = i + CURSES_CUSTOM_COLORS_SHIFT;
48  short pair_id = i+1 + CURSES_CUSTOM_COLORS_SHIFT;
49 
50  init_color(color_id, i * step, i * step, i * step);
51  init_pair(pair_id, color_id, COLOR_BLACK);
52  }
53 }
54 
56  int y, int x,
57  wchar_t sign,
58  short gray
59 )
60 {
61  short color_pair_id = gray + CURSES_CUSTOM_COLORS_SHIFT;
62 
63  attron(COLOR_PAIR(color_pair_id));
64  mvprintw(y, x, "%lc", sign);
65  attroff(COLOR_PAIR(color_pair_id));
66 }
67 
69  int y, int x,
70  wchar_t sign,
71  float gray)
72 {
73  short gray_short = static_cast<short>(gray * static_cast<float>(CURSES_NUM_GRAYS) );
74  console_print_grayscale(y, x, sign, gray_short);
75 }
76 
77 static void clear_background()
78 {
79  int W = getmaxx(stdscr);
80  int H = getmaxy(stdscr);
81 
82  for(int y=0; y<H; y++)
83  {
84  for(int x=0; x<W; x++)
85  {
86  wchar_t sign = L'\u2800';
87  console_print_grayscale(y, x, sign, 0.0f);
88  }
89  }
90 }
91 
92 static void init_mouse()
93 {
94  mouseinterval(0);
95  mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
96  printf("\033[?1003h\n");
97 }
98 
99 static void init_curses(unsigned int num_grayscale)
100 {
101  signal(SIGINT, curses_sigint);
102 
103  initscr();
104  clear();
105  cbreak();
106  keypad(stdscr, TRUE);
107  timeout(5);
108 
109  // test this
110  nodelay(stdscr, TRUE);
111  // notimeout(stdscr, TRUE);
112  // wtimeout(stdscr, 1);
113  noecho();
114  // mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
115  refresh();
116 
117  // mouse events
118  init_mouse();
119  move(0,0);
120 
121  start_color();
122 
123  init_grayscale(num_grayscale);
124  std::locale::global(std::locale("en_US.utf8"));
125  std::wcout.imbue(std::locale());
127 }
128 
129 static int curses_width()
130 {
131  return getmaxx(stdscr);
132 }
133 
134 static int curses_height()
135 {
136  return getmaxy(stdscr);
137 }
138 
139 
140 #endif // CURSES_HELPER_HPP
MOUSE_MOVE
static mmask_t MOUSE_MOVE
Definition: CursesHelper.hpp:9
init_grayscale
static void init_grayscale(short num_grays)
Definition: CursesHelper.hpp:37
init_curses
static void init_curses(unsigned int num_grayscale)
Definition: CursesHelper.hpp:99
TRUE
#define TRUE
Definition: mydefs.hpp:137
NULL
#define NULL
Definition: mydefs.hpp:141
destroy_curses
static void destroy_curses()
Definition: CursesHelper.hpp:16
CURSES_CUSTOM_COLORS_SHIFT
static short CURSES_CUSTOM_COLORS_SHIFT
Definition: CursesHelper.hpp:13
set_num_grays
static void set_num_grays(short num_grays)
Definition: CursesHelper.hpp:32
MOUSE_SCROLL_UP
static mmask_t MOUSE_SCROLL_UP
Definition: CursesHelper.hpp:10
clear_background
static void clear_background()
Definition: CursesHelper.hpp:77
curses_sigint
static void curses_sigint(int sig)
Definition: CursesHelper.hpp:23
init_mouse
static void init_mouse()
Definition: CursesHelper.hpp:92
CURSES_NUM_GRAYS
static short CURSES_NUM_GRAYS
Definition: CursesHelper.hpp:12
console_print_grayscale
static void console_print_grayscale(int y, int x, wchar_t sign, short gray)
Definition: CursesHelper.hpp:55
curses_height
static int curses_height()
Definition: CursesHelper.hpp:134
curses_width
static int curses_width()
Definition: CursesHelper.hpp:129
scripts.create_png.step
step
Definition: create_png.py:42


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23