Skip to content

Commit 395b36e

Browse files
committed
added horizontal split view for default noncurses output #575
1 parent f0039da commit 395b36e

File tree

6 files changed

+118
-46
lines changed

6 files changed

+118
-46
lines changed

cava.c

+27-5
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
330330
if (p.disable_blanking)
331331
system("setterm -blank 0");
332332
#endif
333+
if (p.orientation != ORIENT_BOTTOM) {
334+
cleanup();
335+
fprintf(stderr, "only default bottom orientation is supported in tty\n");
336+
exit(EXIT_FAILURE);
337+
}
333338
}
334339

335340
// We use unicode block characters to draw the bars and
@@ -581,7 +586,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
581586

582587
init_terminal_noncurses(inAtty, p.color, p.bcolor, p.col, p.bgcol, p.gradient,
583588
p.gradient_count, p.gradient_colors, width, lines,
584-
p.bar_width);
589+
p.bar_width, p.orientation);
585590
height = lines * 8;
586591
break;
587592
#ifndef _MSC_VER
@@ -949,7 +954,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
949954
p.sens *= 0.999;
950955
else
951956
p.sens *= 1.00001;
952-
cava_out[n] = (cava_out[n] + 1.0) / 2.0;
957+
958+
if (p.orientation != ORIENT_SPLIT_H)
959+
cava_out[n] = (cava_out[n] + 1.0) / 2.0;
953960
}
954961

955962
if (output_mode == OUTPUT_SDL_GLSL) {
@@ -959,6 +966,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
959966
cava_out[n] = 0.0;
960967
} else {
961968
cava_out[n] *= *dimension_value;
969+
if (p.orientation == ORIENT_SPLIT_H || p.orientation == ORIENT_SPLIT_V) {
970+
cava_out[n] /= 2;
971+
}
962972
}
963973
if (p.waveform) {
964974
bars_raw[n] = cava_out[n];
@@ -1122,9 +1132,21 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
11221132
break;
11231133
#endif
11241134
case OUTPUT_NONCURSES:
1125-
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars, p.bar_width,
1126-
p.bar_spacing, remainder, bars, previous_frame,
1127-
p.gradient, x_axis_info, p.orientation);
1135+
if (p.orientation == ORIENT_SPLIT_H) {
1136+
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
1137+
p.bar_width, p.bar_spacing, remainder, bars,
1138+
previous_frame, p.gradient, x_axis_info,
1139+
ORIENT_BOTTOM, 1);
1140+
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
1141+
p.bar_width, p.bar_spacing, remainder, bars,
1142+
previous_frame, p.gradient, x_axis_info,
1143+
ORIENT_TOP, 1);
1144+
} else {
1145+
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
1146+
p.bar_width, p.bar_spacing, remainder, bars,
1147+
previous_frame, p.gradient, x_axis_info,
1148+
p.orientation, 0);
1149+
}
11281150
break;
11291151
case OUTPUT_NCURSES:
11301152
#ifdef NCURSES

config.c

+30-9
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,39 @@ bool validate_config(struct config_params *p, struct error_s *error) {
285285
}
286286

287287
p->orientation = ORIENT_BOTTOM;
288-
if (p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES || p->output == OUTPUT_NONCURSES) {
289-
if (strcmp(orientation, "top") == 0) {
290-
p->orientation = ORIENT_TOP;
291-
}
292-
if (strcmp(orientation, "left") == 0) {
293-
p->orientation = ORIENT_LEFT;
294-
}
295-
if (strcmp(orientation, "right") == 0) {
296-
p->orientation = ORIENT_RIGHT;
288+
if (strcmp(orientation, "top") == 0) {
289+
p->orientation = ORIENT_TOP;
290+
}
291+
if (strcmp(orientation, "left") == 0) {
292+
p->orientation = ORIENT_LEFT;
293+
}
294+
if (strcmp(orientation, "right") == 0) {
295+
p->orientation = ORIENT_RIGHT;
296+
}
297+
if (strcmp(orientation, "horizontal") == 0) {
298+
if (p->output != OUTPUT_NONCURSES) {
299+
write_errorf(error, "only noncurses output suports horizontal orientation\n");
300+
return false;
297301
}
302+
p->orientation = ORIENT_SPLIT_H;
303+
}
304+
if ((p->orientation == ORIENT_LEFT || p->orientation == ORIENT_RIGHT) &&
305+
!(p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES)) {
306+
write_errorf(error, "only ncurses and sdl supports left/right orientation\n");
307+
return false;
308+
}
309+
if ((p->orientation == ORIENT_TOP) &&
310+
!(p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES ||
311+
p->output == OUTPUT_NONCURSES)) {
312+
write_errorf(error, "only noncurses, ncurses and sdl supports top orientation\n");
313+
return false;
298314
}
299315

316+
if ((p->orientation != ORIENT_BOTTOM && p->output == OUTPUT_SDL && p->gradient != 0)) {
317+
write_errorf(error,
318+
"gradient in sdl is not supported with top, left or right orientation\n");
319+
return false;
320+
}
300321
p->xaxis = NONE;
301322
if (strcmp(xaxisScale, "none") == 0) {
302323
p->xaxis = NONE;

config.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ enum data_format { FORMAT_ASCII = 0, FORMAT_BINARY = 1, FORMAT_NTK3000 = 2 };
9393

9494
enum xaxis_scale { NONE, FREQUENCY, NOTE };
9595

96-
enum orientation { ORIENT_BOTTOM, ORIENT_TOP, ORIENT_LEFT, ORIENT_RIGHT };
96+
enum orientation {
97+
ORIENT_BOTTOM,
98+
ORIENT_TOP,
99+
ORIENT_LEFT,
100+
ORIENT_RIGHT,
101+
ORIENT_SPLIT_H,
102+
ORIENT_SPLIT_V
103+
};
97104

98105
struct config_params {
99106
char *color, *bcolor, *raw_target, *audio_source,

example_files/config

+6-4
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@
144144
# use one of the predefined ones.
145145
; method = noncurses
146146

147-
# Orientation of the visualization. Can be 'bottom', 'top', 'left' or 'right'.
148-
# Default is 'bottom'. Other orientations are only supported on sdl and ncruses
149-
# output. Note: many fonts have weird glyphs for 'top' and 'right' characters,
150-
# which can make ncurses not look right.
147+
# Orientation of the visualization. Can be 'bottom', 'top', 'left', 'right' or
148+
# 'horizontal'. Default is 'bottom'. 'left and 'right' are only supported on sdl
149+
# and ncruses output. 'horizontal' (bars go up and down from center) is only supported
150+
# on noncurses output.
151+
# Note: many fonts have weird or missing glyphs for characters used in orientations
152+
# other than 'bottom', which can make output not look right.
151153
; orientation = bottom
152154

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

output/terminal_noncurses.c

+43-25
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ void free_terminal_noncurses(void) {
8989

9090
int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg_color_string,
9191
int col, int bgcol, int gradient, int gradient_count,
92-
char **gradient_color_strings, int width, int lines, int bar_width) {
92+
char **gradient_color_strings, int width, int lines, int bar_width,
93+
enum orientation orientation) {
9394

9495
free_terminal_noncurses();
9596

@@ -188,7 +189,33 @@ int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg
188189
printf("\033[%dm", col); // setting color
189190
}
190191

192+
if (bgcol != 0) {
193+
194+
bgcol += 40;
195+
196+
if (bgcol == 48) {
197+
struct colors bg_color = parse_color(bg_color_string);
198+
printf("\033[48;2;%d;%d;%dm", bg_color.rgb[0], bg_color.rgb[1], bg_color.rgb[2]);
199+
} else {
200+
printf("\033[%dm", bgcol);
201+
}
202+
203+
for (int n = lines; n >= 0; n--) {
204+
for (int i = 0; i < width; i++) {
205+
printf(" "); // setting backround color
206+
}
207+
if (n != 0)
208+
printf("\n");
209+
else
210+
printf("\r");
211+
}
212+
printf("\033[%dA", lines); // moving cursor back up
213+
}
214+
191215
if (gradient) {
216+
if (orientation == ORIENT_SPLIT_H || orientation == ORIENT_SPLIT_V) {
217+
lines = lines / 2;
218+
}
192219
struct colors gradient_color_defs[MAX_GRADIENT_COLOR_DEFS];
193220
for (int i = 0; i < gradient_count; i++) {
194221
gradient_color_defs[i] = parse_color(gradient_color_strings[i]);
@@ -224,28 +251,6 @@ int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg
224251
gradient_colors[lines - 1] = gradient_color_defs[gradient_count - 1];
225252
}
226253

227-
if (bgcol != 0) {
228-
229-
bgcol += 40;
230-
231-
if (bgcol == 48) {
232-
struct colors bg_color = parse_color(bg_color_string);
233-
printf("\033[48;2;%d;%d;%dm", bg_color.rgb[0], bg_color.rgb[1], bg_color.rgb[2]);
234-
} else {
235-
printf("\033[%dm", bgcol);
236-
}
237-
238-
for (int n = lines; n >= 0; n--) {
239-
for (int i = 0; i < width; i++) {
240-
printf(" "); // setting backround color
241-
}
242-
if (n != 0)
243-
printf("\n");
244-
else
245-
printf("\r");
246-
}
247-
printf("\033[%dA", lines); // moving cursor back up
248-
}
249254
#ifdef _MSC_VER
250255
setecho(1, 0);
251256
#else
@@ -274,7 +279,8 @@ void get_terminal_dim_noncurses(int *width, int *lines) {
274279

275280
int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, int bar_width,
276281
int bar_spacing, int rest, int bars[], int previous_frame[],
277-
int gradient, int x_axis_info, enum orientation orientation) {
282+
int gradient, int x_axis_info, enum orientation orientation,
283+
int offset) {
278284

279285
int current_cell, prev_cell, same_line, new_line, cx;
280286

@@ -291,7 +297,7 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
291297
int new_width;
292298
get_terminal_dim_noncurses(&new_width, &new_lines);
293299

294-
if (new_lines != (lines) || new_width != width)
300+
if (new_lines != lines || new_width != width)
295301
return -1;
296302

297303
if (x_axis_info)
@@ -303,6 +309,14 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
303309
else if (!tty)
304310
frame_buffer[0] = '\0';
305311

312+
if (offset)
313+
lines /= 2;
314+
315+
if (orientation == ORIENT_TOP && offset) {
316+
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[%dB",
317+
lines); // move down
318+
}
319+
306320
for (int current_line = lines - 1; current_line >= 0; current_line--) {
307321

308322
if (orientation == ORIENT_BOTTOM) {
@@ -440,6 +454,10 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
440454
same_line++;
441455
}
442456
}
457+
if (orientation == ORIENT_TOP && offset) {
458+
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[%dA",
459+
lines); // move up
460+
}
443461
if (same_line != lines) {
444462
if (tty)
445463
printf("%s\r\033[%dA", ttyframe_buffer, new_line);

output/terminal_noncurses.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
int init_terminal_noncurses(int inAtty, char *const fg_color_string, char *const bg_color_string,
44
int col, int bgcol, int gradient, int gradient_count,
5-
char **gradient_colors, int w, int h, int bar_width);
5+
char **gradient_colors, int w, int h, int bar_width,
6+
enum orientation orientation);
67
void get_terminal_dim_noncurses(int *w, int *h);
78
int draw_terminal_noncurses(int inAtty, int lines, int width, int number_of_bars, int bar_width,
89
int bar_spacing, int rest, int bars[], int previous_frame[],
9-
int gradient, int x_axis_info, enum orientation orientation);
10+
int gradient, int x_axis_info, enum orientation orientation,
11+
int offset);
1012
void cleanup_terminal_noncurses(void);

0 commit comments

Comments
 (0)