Skip to content

Commit 770f010

Browse files
DziubanMaciejkarlstav
authored andcommitted
Implement different orientations of output for ncurses
1 parent f8e8cc7 commit 770f010

File tree

5 files changed

+95
-44
lines changed

5 files changed

+95
-44
lines changed

cava.c

+21-19
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,15 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
412412
double *cava_out;
413413

414414
int height, lines, width, remainder, fp;
415-
int dimension_bar, dimension_value;
415+
int *dimension_bar, *dimension_value;
416+
417+
if (p.orientation == ORIENT_LEFT || p.orientation == ORIENT_RIGHT) {
418+
dimension_bar = &height;
419+
dimension_value = &width;
420+
} else {
421+
dimension_bar = &width;
422+
dimension_value = &height;
423+
}
416424

417425
#ifdef SDL
418426
// output: start sdl mode
@@ -438,8 +446,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
438446
p.gradient_count, p.gradient_colors, &width, &lines);
439447
if (p.xaxis != NONE)
440448
lines--;
441-
// we have 8 times as much height due to using 1/8 block characters
442-
height = lines * 8;
449+
height = lines;
450+
*dimension_value *=
451+
8; // we have 8 times as much height due to using 1/8 block characters
443452
break;
444453
#endif
445454
#ifdef SDL
@@ -516,19 +525,11 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
516525
p.autobars = 1;
517526
}
518527

519-
if (p.orientation == ORIENT_LEFT || p.orientation == ORIENT_RIGHT) {
520-
dimension_bar = height;
521-
dimension_value = width;
522-
} else {
523-
dimension_bar = width;
524-
dimension_value = height;
525-
}
526-
527528
// getting numbers of bars
528529
int number_of_bars = p.fixedbars;
529530

530531
if (p.autobars == 1)
531-
number_of_bars = (dimension_bar + p.bar_spacing) / (p.bar_width + p.bar_spacing);
532+
number_of_bars = (*dimension_bar + p.bar_spacing) / (p.bar_width + p.bar_spacing);
532533

533534
if (number_of_bars <= 1) {
534535
number_of_bars = 1; // must have at least 1 bars
@@ -552,7 +553,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
552553
}
553554

554555
// checks if there is stil extra room, will use this to center
555-
remainder = (dimension_bar - number_of_bars * p.bar_width -
556+
remainder = (*dimension_bar - number_of_bars * p.bar_width -
556557
number_of_bars * p.bar_spacing + p.bar_spacing) /
557558
2;
558559
if (remainder < 0)
@@ -561,7 +562,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
561562
#ifndef NDEBUG
562563
debug("height: %d width: %d dimension_bar: %d dimension_value: %d bars:%d bar width: "
563564
"%d remainder: %d\n",
564-
height, width, dimension_bar, dimension_value, number_of_bars, p.bar_width,
565+
height, width, *dimension_bar, *dimension_value, number_of_bars, p.bar_width,
565566
remainder);
566567
#endif
567568

@@ -782,7 +783,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
782783

783784
for (uint32_t n = 0; n < (number_of_bars / output_channels) * audio.channels; n++) {
784785
if (p.autosens)
785-
cava_out[n] *= dimension_value;
786+
cava_out[n] *= *dimension_value;
786787
else
787788
cava_out[n] *= p.sens;
788789
}
@@ -908,15 +909,16 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
908909
switch (output_mode) {
909910
case OUTPUT_NCURSES:
910911
#ifdef NCURSES
911-
rc = draw_terminal_ncurses(inAtty, lines, width, number_of_bars, p.bar_width,
912-
p.bar_spacing, remainder, bars, previous_frame,
913-
p.gradient, x_axis_info);
912+
rc = draw_terminal_ncurses(inAtty, *dimension_value / 8, *dimension_bar,
913+
number_of_bars, p.bar_width, p.bar_spacing,
914+
remainder, bars, previous_frame, p.gradient,
915+
x_axis_info, p.orientation);
914916
break;
915917
#endif
916918
#ifdef SDL
917919
case OUTPUT_SDL:
918920
rc = draw_sdl(number_of_bars, p.bar_width, p.bar_spacing, remainder,
919-
dimension_value, bars, previous_frame, frame_time_msec,
921+
*dimension_value, bars, previous_frame, frame_time_msec,
920922
p.orientation);
921923
break;
922924
#endif

config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ bool validate_config(struct config_params *p, struct error_s *error) {
239239
}
240240

241241
p->orientation = ORIENT_BOTTOM;
242-
if (p->output == OUTPUT_SDL) {
242+
if (p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES) {
243243
if (strcmp(orientation, "top") == 0) {
244244
p->orientation = ORIENT_TOP;
245245
}

example_files/config

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@
9494
; method = ncurses
9595

9696
# Orientation of the visualization. Can be 'bottom', 'top', 'left' or 'right'.
97-
# Default is 'bottom'. Other orientations are only supported on sdl output.
97+
# Default is 'bottom'. Other orientations are only supported on sdl and ncruses
98+
# output. Note: many fonts have weird glyphs for 'top' and 'right' characters,
99+
# which can make ncurses not look right.
98100
; orientation = bottom
99101

100102
# Visual channels. Can be 'stereo' or 'mono'.

output/terminal_ncurses.c

+66-22
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ struct colors {
2020

2121
#define MAX_COLOR_REDEFINITION 256
2222

23-
const wchar_t *bar_heights[] = {L"\u2581", L"\u2582", L"\u2583", L"\u2584",
24-
L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
25-
int num_bar_heights = (sizeof(bar_heights) / sizeof(bar_heights[0]));
23+
const int num_bar_heights = 8;
24+
const wchar_t *bar_heights_bottom[] = {L"\u2581", L"\u2582", L"\u2583", L"\u2584",
25+
L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
26+
const wchar_t *bar_heights_top[] = {L"\u2594", L"\U0001FB82", L"\U0001FB83", L"\u2580",
27+
L"\U0001FB84", L"\U0001FB85", L"\U0001FB86", L"\u2588"};
28+
const wchar_t *bar_heights_left[] = {L"\u258F", L"\u258E", L"\u258D", L"\u258C",
29+
L"\u258B", L"\u258A", L"\u2589", L"\u2588"};
30+
const wchar_t *bar_heights_right[] = {L"\u2595", L"\U0001FB87", L"\U0001FB88", L"\u2590",
31+
L"\U0001FB89", L"\U0001FB8A", L"\U0001FB8B", L"\u2588"};
32+
const wchar_t **bar_heights[] = {bar_heights_bottom, bar_heights_top, bar_heights_left,
33+
bar_heights_right};
2634

2735
// static struct colors the_color_redefinitions[MAX_COLOR_REDEFINITION];
2836

@@ -69,6 +77,28 @@ static NCURSES_COLOR_T change_color_definition(NCURSES_COLOR_T color_number,
6977
return return_color_number;
7078
}
7179

80+
static void get_screen_coords(int val, int col, int max_value, enum orientation orientation, int *x,
81+
int *y) {
82+
switch (orientation) {
83+
case ORIENT_LEFT:
84+
*x = val;
85+
*y = col;
86+
break;
87+
case ORIENT_RIGHT:
88+
*x = max_value - val;
89+
*y = col;
90+
break;
91+
case ORIENT_TOP:
92+
*x = col;
93+
*y = val;
94+
break;
95+
default:
96+
*x = col;
97+
*y = max_value - val;
98+
break;
99+
}
100+
}
101+
72102
void init_terminal_ncurses(char *const fg_color_string, char *const bg_color_string,
73103
int predef_fg_color, int predef_bg_color, int gradient,
74104
int gradient_count, char **gradient_colors, int *width, int *lines) {
@@ -191,33 +221,42 @@ void get_terminal_dim_ncurses(int *width, int *height) {
191221

192222
#define TERMINAL_RESIZED -1
193223

194-
int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width, int bars_count,
224+
int draw_terminal_ncurses(int is_tty, int dimension_value, int dimension_bar, int bars_count,
195225
int bar_width, int bar_spacing, int rest, const int bars[],
196-
int previous_frame[], int gradient, int x_axis_info) {
197-
const int height = terminal_height - 1;
226+
int previous_frame[], int gradient, int x_axis_info,
227+
enum orientation orientation) {
228+
const int max_value = dimension_value - 1;
198229

199230
// output: check if terminal has been resized
200231
if (!is_tty) {
201232
if (x_axis_info)
202-
terminal_height++;
233+
dimension_value++;
234+
int terminal_width, terminal_height;
235+
if (orientation == ORIENT_LEFT || orientation == ORIENT_RIGHT) {
236+
terminal_width = dimension_value;
237+
terminal_height = dimension_bar;
238+
} else {
239+
terminal_width = dimension_bar;
240+
terminal_height = dimension_value;
241+
}
203242
if (LINES != terminal_height || COLS != terminal_width) {
204243
return TERMINAL_RESIZED;
205244
if (x_axis_info)
206-
terminal_height--;
245+
dimension_value--;
207246
}
208247
}
209248

210249
// Compute how much of the screen we possibly need to update ahead-of-time.
211-
int max_update_y = 0;
250+
int max_update_value = 0;
212251
for (int bar = 0; bar < bars_count; bar++) {
213-
max_update_y = max(max_update_y, max(bars[bar], previous_frame[bar]));
252+
max_update_value = max(max_update_value, max(bars[bar], previous_frame[bar]));
214253
}
215254

216-
max_update_y = (max_update_y + num_bar_heights) / num_bar_heights;
255+
max_update_value = (max_update_value + num_bar_heights) / num_bar_heights;
217256

218-
for (int y = 0; y < max_update_y; y++) {
257+
for (int val = 0; val < max_update_value; val++) {
219258
if (gradient) {
220-
change_colors(y, height);
259+
change_colors(val, max_value);
221260
}
222261

223262
for (int bar = 0; bar < bars_count; bar++) {
@@ -229,32 +268,37 @@ int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width, i
229268
int f_cell = (bars[bar] - 1) / num_bar_heights;
230269
int f_last_cell = (previous_frame[bar] - 1) / num_bar_heights;
231270

232-
if (f_cell >= y) {
271+
if (f_cell >= val) {
233272
int bar_step;
234273

235-
if (f_cell == y) {
236-
// The "cap" of the bar occurs at this [y].
274+
if (f_cell == val) {
275+
// The "cap" of the bar occurs at this [val].
237276
bar_step = (bars[bar] - 1) % num_bar_heights;
238-
} else if (f_last_cell <= y) {
239-
// The bar is full at this [y].
277+
} else if (f_last_cell <= val) {
278+
// The bar is full at this [val].
240279
bar_step = num_bar_heights - 1;
241280
} else {
242281
// No update necessary since last frame.
243282
continue;
244283
}
245284

246285
for (int col = cur_col, i = 0; i < bar_width; i++, col++) {
286+
int x, y;
287+
get_screen_coords(val, col, max_value, orientation, &x, &y);
288+
247289
if (is_tty) {
248-
mvaddch(height - y, col, 0x41 + bar_step);
290+
mvaddch(y, x, 0x41 + bar_step);
249291
} else {
250-
mvaddwstr(height - y, col, bar_heights[bar_step]);
292+
mvaddwstr(y, x, bar_heights[orientation][bar_step]);
251293
}
252294
}
253-
} else if (f_last_cell >= y) {
295+
} else if (f_last_cell >= val) {
254296
// This bar was taller during the last frame than during this frame, so
255297
// clear the excess characters.
256298
for (int col = cur_col, i = 0; i < bar_width; i++, col++) {
257-
mvaddch(height - y, col, ' ');
299+
int x, y;
300+
get_screen_coords(val, col, max_value, orientation, &x, &y);
301+
mvaddch(y, x, ' ');
258302
}
259303
}
260304
}

output/terminal_ncurses.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#include "../config.h"
2+
13
void init_terminal_ncurses(char *const fg_color_string, char *const bg_color_string,
24
int predef_fg_color, int predef_bg_color, int gradient,
35
int gradient_count, char **gradient_colors, int *width, int *height);
46
void get_terminal_dim_ncurses(int *width, int *height);
57
int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width, int bars_count,
68
int bar_width, int bar_spacing, int rest, const int bars[],
7-
int previous_frame[], int gradient, int x_axis_info);
9+
int previous_frame[], int gradient, int x_axis_info,
10+
enum orientation orientation);
811
void cleanup_terminal_ncurses(void);

0 commit comments

Comments
 (0)