Skip to content

Commit

Permalink
added horizontal split view for default noncurses output #575
Browse files Browse the repository at this point in the history
  • Loading branch information
karlstav committed Aug 6, 2024
1 parent f0039da commit 395b36e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 46 deletions.
32 changes: 27 additions & 5 deletions cava.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
if (p.disable_blanking)
system("setterm -blank 0");
#endif
if (p.orientation != ORIENT_BOTTOM) {
cleanup();
fprintf(stderr, "only default bottom orientation is supported in tty\n");
exit(EXIT_FAILURE);
}
}

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

init_terminal_noncurses(inAtty, p.color, p.bcolor, p.col, p.bgcol, p.gradient,
p.gradient_count, p.gradient_colors, width, lines,
p.bar_width);
p.bar_width, p.orientation);
height = lines * 8;
break;
#ifndef _MSC_VER
Expand Down Expand Up @@ -949,7 +954,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
p.sens *= 0.999;
else
p.sens *= 1.00001;
cava_out[n] = (cava_out[n] + 1.0) / 2.0;

if (p.orientation != ORIENT_SPLIT_H)
cava_out[n] = (cava_out[n] + 1.0) / 2.0;
}

if (output_mode == OUTPUT_SDL_GLSL) {
Expand All @@ -959,6 +966,9 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
cava_out[n] = 0.0;
} else {
cava_out[n] *= *dimension_value;
if (p.orientation == ORIENT_SPLIT_H || p.orientation == ORIENT_SPLIT_V) {
cava_out[n] /= 2;
}
}
if (p.waveform) {
bars_raw[n] = cava_out[n];
Expand Down Expand Up @@ -1122,9 +1132,21 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
break;
#endif
case OUTPUT_NONCURSES:
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars, p.bar_width,
p.bar_spacing, remainder, bars, previous_frame,
p.gradient, x_axis_info, p.orientation);
if (p.orientation == ORIENT_SPLIT_H) {
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
p.bar_width, p.bar_spacing, remainder, bars,
previous_frame, p.gradient, x_axis_info,
ORIENT_BOTTOM, 1);
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
p.bar_width, p.bar_spacing, remainder, bars,
previous_frame, p.gradient, x_axis_info,
ORIENT_TOP, 1);
} else {
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars,
p.bar_width, p.bar_spacing, remainder, bars,
previous_frame, p.gradient, x_axis_info,
p.orientation, 0);
}
break;
case OUTPUT_NCURSES:
#ifdef NCURSES
Expand Down
39 changes: 30 additions & 9 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,39 @@ bool validate_config(struct config_params *p, struct error_s *error) {
}

p->orientation = ORIENT_BOTTOM;
if (p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES || p->output == OUTPUT_NONCURSES) {
if (strcmp(orientation, "top") == 0) {
p->orientation = ORIENT_TOP;
}
if (strcmp(orientation, "left") == 0) {
p->orientation = ORIENT_LEFT;
}
if (strcmp(orientation, "right") == 0) {
p->orientation = ORIENT_RIGHT;
if (strcmp(orientation, "top") == 0) {
p->orientation = ORIENT_TOP;
}
if (strcmp(orientation, "left") == 0) {
p->orientation = ORIENT_LEFT;
}
if (strcmp(orientation, "right") == 0) {
p->orientation = ORIENT_RIGHT;
}
if (strcmp(orientation, "horizontal") == 0) {
if (p->output != OUTPUT_NONCURSES) {
write_errorf(error, "only noncurses output suports horizontal orientation\n");
return false;
}
p->orientation = ORIENT_SPLIT_H;
}
if ((p->orientation == ORIENT_LEFT || p->orientation == ORIENT_RIGHT) &&
!(p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES)) {
write_errorf(error, "only ncurses and sdl supports left/right orientation\n");
return false;
}
if ((p->orientation == ORIENT_TOP) &&
!(p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES ||
p->output == OUTPUT_NONCURSES)) {
write_errorf(error, "only noncurses, ncurses and sdl supports top orientation\n");
return false;
}

if ((p->orientation != ORIENT_BOTTOM && p->output == OUTPUT_SDL && p->gradient != 0)) {
write_errorf(error,
"gradient in sdl is not supported with top, left or right orientation\n");
return false;
}
p->xaxis = NONE;
if (strcmp(xaxisScale, "none") == 0) {
p->xaxis = NONE;
Expand Down
9 changes: 8 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ enum data_format { FORMAT_ASCII = 0, FORMAT_BINARY = 1, FORMAT_NTK3000 = 2 };

enum xaxis_scale { NONE, FREQUENCY, NOTE };

enum orientation { ORIENT_BOTTOM, ORIENT_TOP, ORIENT_LEFT, ORIENT_RIGHT };
enum orientation {
ORIENT_BOTTOM,
ORIENT_TOP,
ORIENT_LEFT,
ORIENT_RIGHT,
ORIENT_SPLIT_H,
ORIENT_SPLIT_V
};

struct config_params {
char *color, *bcolor, *raw_target, *audio_source,
Expand Down
10 changes: 6 additions & 4 deletions example_files/config
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@
# use one of the predefined ones.
; method = noncurses

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

# Visual channels. Can be 'stereo' or 'mono'.
Expand Down
68 changes: 43 additions & 25 deletions output/terminal_noncurses.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void free_terminal_noncurses(void) {

int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg_color_string,
int col, int bgcol, int gradient, int gradient_count,
char **gradient_color_strings, int width, int lines, int bar_width) {
char **gradient_color_strings, int width, int lines, int bar_width,
enum orientation orientation) {

free_terminal_noncurses();

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

if (bgcol != 0) {

bgcol += 40;

if (bgcol == 48) {
struct colors bg_color = parse_color(bg_color_string);
printf("\033[48;2;%d;%d;%dm", bg_color.rgb[0], bg_color.rgb[1], bg_color.rgb[2]);
} else {
printf("\033[%dm", bgcol);
}

for (int n = lines; n >= 0; n--) {
for (int i = 0; i < width; i++) {
printf(" "); // setting backround color
}
if (n != 0)
printf("\n");
else
printf("\r");
}
printf("\033[%dA", lines); // moving cursor back up
}

if (gradient) {
if (orientation == ORIENT_SPLIT_H || orientation == ORIENT_SPLIT_V) {
lines = lines / 2;
}
struct colors gradient_color_defs[MAX_GRADIENT_COLOR_DEFS];
for (int i = 0; i < gradient_count; i++) {
gradient_color_defs[i] = parse_color(gradient_color_strings[i]);
Expand Down Expand Up @@ -224,28 +251,6 @@ int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg
gradient_colors[lines - 1] = gradient_color_defs[gradient_count - 1];
}

if (bgcol != 0) {

bgcol += 40;

if (bgcol == 48) {
struct colors bg_color = parse_color(bg_color_string);
printf("\033[48;2;%d;%d;%dm", bg_color.rgb[0], bg_color.rgb[1], bg_color.rgb[2]);
} else {
printf("\033[%dm", bgcol);
}

for (int n = lines; n >= 0; n--) {
for (int i = 0; i < width; i++) {
printf(" "); // setting backround color
}
if (n != 0)
printf("\n");
else
printf("\r");
}
printf("\033[%dA", lines); // moving cursor back up
}
#ifdef _MSC_VER
setecho(1, 0);
#else
Expand Down Expand Up @@ -274,7 +279,8 @@ void get_terminal_dim_noncurses(int *width, int *lines) {

int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, int bar_width,
int bar_spacing, int rest, int bars[], int previous_frame[],
int gradient, int x_axis_info, enum orientation orientation) {
int gradient, int x_axis_info, enum orientation orientation,
int offset) {

int current_cell, prev_cell, same_line, new_line, cx;

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

if (new_lines != (lines) || new_width != width)
if (new_lines != lines || new_width != width)
return -1;

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

if (offset)
lines /= 2;

if (orientation == ORIENT_TOP && offset) {
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[%dB",
lines); // move down
}

for (int current_line = lines - 1; current_line >= 0; current_line--) {

if (orientation == ORIENT_BOTTOM) {
Expand Down Expand Up @@ -440,6 +454,10 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
same_line++;
}
}
if (orientation == ORIENT_TOP && offset) {
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[%dA",
lines); // move up
}
if (same_line != lines) {
if (tty)
printf("%s\r\033[%dA", ttyframe_buffer, new_line);
Expand Down
6 changes: 4 additions & 2 deletions output/terminal_noncurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

int init_terminal_noncurses(int inAtty, char *const fg_color_string, char *const bg_color_string,
int col, int bgcol, int gradient, int gradient_count,
char **gradient_colors, int w, int h, int bar_width);
char **gradient_colors, int w, int h, int bar_width,
enum orientation orientation);
void get_terminal_dim_noncurses(int *w, int *h);
int draw_terminal_noncurses(int inAtty, int lines, int width, int number_of_bars, int bar_width,
int bar_spacing, int rest, int bars[], int previous_frame[],
int gradient, int x_axis_info, enum orientation orientation);
int gradient, int x_axis_info, enum orientation orientation,
int offset);
void cleanup_terminal_noncurses(void);

0 comments on commit 395b36e

Please sign in to comment.