Skip to content

Commit f0039da

Browse files
committed
#575: implement top orientation for noncurses
1 parent 3f6e931 commit f0039da

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

cava.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ as of 0.4.0 all options are specified in config file, see in '/home/username/.co
11241124
case OUTPUT_NONCURSES:
11251125
rc = draw_terminal_noncurses(inAtty, lines, width, number_of_bars, p.bar_width,
11261126
p.bar_spacing, remainder, bars, previous_frame,
1127-
p.gradient, x_axis_info);
1127+
p.gradient, x_axis_info, p.orientation);
11281128
break;
11291129
case OUTPUT_NCURSES:
11301130
#ifdef NCURSES

config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ 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) {
288+
if (p->output == OUTPUT_SDL || p->output == OUTPUT_NCURSES || p->output == OUTPUT_NONCURSES) {
289289
if (strcmp(orientation, "top") == 0) {
290290
p->orientation = ORIENT_TOP;
291291
}

output/terminal_noncurses.c

+63-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "output/terminal_noncurses.h"
2+
13
#include <locale.h>
24
#include <math.h>
35
#include <stdio.h>
@@ -18,6 +20,7 @@
1820

1921
wchar_t *frame_buffer;
2022
wchar_t *barstring[8];
23+
wchar_t *top_barstring[8];
2124
wchar_t *spacestring;
2225
int buf_length;
2326
char *ttyframe_buffer;
@@ -78,6 +81,7 @@ void free_terminal_noncurses(void) {
7881
free(ttyspacestring);
7982
for (int i = 0; i < 8; i++) {
8083
free(barstring[i]);
84+
free(top_barstring[i]);
8185
free(ttybarstring[i]);
8286
}
8387
free(gradient_colors);
@@ -125,6 +129,8 @@ int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg
125129
for (int n = 0; n < 8; n++) {
126130
barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * (bar_width + 1));
127131
barstring[n][0] = '\0';
132+
top_barstring[n] = (wchar_t *)malloc(sizeof(wchar_t) * (bar_width + 1));
133+
top_barstring[n][0] = '\0';
128134
}
129135
spacestring[0] = '\0';
130136
frame_buffer[0] = '\0';
@@ -139,6 +145,14 @@ int init_terminal_noncurses(int tty, char *const fg_color_string, char *const bg
139145
wcscat(barstring[5], L"\u2585");
140146
wcscat(barstring[6], L"\u2586");
141147
wcscat(barstring[7], L"\u2587");
148+
wcscat(top_barstring[0], L"\u2588");
149+
wcscat(top_barstring[1], L"\u2594");
150+
wcscat(top_barstring[2], L"\U0001FB82");
151+
wcscat(top_barstring[3], L"\U0001FB83");
152+
wcscat(top_barstring[4], L"\u2580");
153+
wcscat(top_barstring[5], L"\U0001FB84");
154+
wcscat(top_barstring[6], L"\U0001FB85");
155+
wcscat(top_barstring[7], L"\U0001FB86");
142156
wcscat(spacestring, L" ");
143157
}
144158
}
@@ -260,7 +274,7 @@ void get_terminal_dim_noncurses(int *width, int *lines) {
260274

261275
int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, int bar_width,
262276
int bar_spacing, int rest, int bars[], int previous_frame[],
263-
int gradient, int x_axis_info) {
277+
int gradient, int x_axis_info, enum orientation orientation) {
264278

265279
int current_cell, prev_cell, same_line, new_line, cx;
266280

@@ -291,17 +305,33 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
291305

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

294-
if (gradient) {
295-
if (tty) {
296-
cx += snprintf(ttyframe_buffer + cx, ttybuf_length - cx, "\033[38;2;%d;%d;%dm",
297-
gradient_colors[current_line].rgb[0],
298-
gradient_colors[current_line].rgb[1],
299-
gradient_colors[current_line].rgb[2]);
300-
} else if (!tty) {
301-
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[38;2;%d;%d;%dm",
302-
gradient_colors[current_line].rgb[0],
303-
gradient_colors[current_line].rgb[1],
304-
gradient_colors[current_line].rgb[2]);
308+
if (orientation == ORIENT_BOTTOM) {
309+
if (gradient) {
310+
if (tty) {
311+
cx += snprintf(ttyframe_buffer + cx, ttybuf_length - cx, "\033[38;2;%d;%d;%dm",
312+
gradient_colors[current_line].rgb[0],
313+
gradient_colors[current_line].rgb[1],
314+
gradient_colors[current_line].rgb[2]);
315+
} else if (!tty) {
316+
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[38;2;%d;%d;%dm",
317+
gradient_colors[current_line].rgb[0],
318+
gradient_colors[current_line].rgb[1],
319+
gradient_colors[current_line].rgb[2]);
320+
}
321+
}
322+
} else if (orientation == ORIENT_TOP) {
323+
if (gradient) {
324+
if (tty) {
325+
cx += snprintf(ttyframe_buffer + cx, ttybuf_length - cx, "\033[38;2;%d;%d;%dm",
326+
gradient_colors[lines - current_line - 1].rgb[0],
327+
gradient_colors[lines - current_line - 1].rgb[1],
328+
gradient_colors[lines - current_line - 1].rgb[2]);
329+
} else if (!tty) {
330+
cx += swprintf(frame_buffer + cx, buf_length - cx, L"\033[38;2;%d;%d;%dm",
331+
gradient_colors[lines - current_line - 1].rgb[0],
332+
gradient_colors[lines - current_line - 1].rgb[1],
333+
gradient_colors[lines - current_line - 1].rgb[2]);
334+
}
305335
}
306336
}
307337

@@ -310,8 +340,13 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
310340

311341
for (int i = 0; i < number_of_bars; i++) {
312342

313-
current_cell = bars[i] - current_line * 8;
314-
prev_cell = previous_frame[i] - current_line * 8;
343+
if (orientation == ORIENT_BOTTOM) {
344+
current_cell = bars[i] - current_line * 8;
345+
prev_cell = previous_frame[i] - current_line * 8;
346+
} else if (orientation == ORIENT_TOP) {
347+
current_cell = bars[i] - (lines - current_line - 1) * 8;
348+
prev_cell = previous_frame[i] - (lines - current_line - 1) * 8;
349+
}
315350

316351
// same as last frame
317352
if ((current_cell < 1 && prev_cell < 1) || (current_cell > 7 && prev_cell > 7) ||
@@ -369,12 +404,21 @@ int draw_terminal_noncurses(int tty, int lines, int width, int number_of_bars, i
369404
center_adjusted = 1;
370405
}
371406

372-
if (current_cell < 1)
407+
if (current_cell < 1) {
373408
cx += swprintf(frame_buffer + cx, buf_length - cx, spacestring);
374-
else if (current_cell > 7)
375-
cx += swprintf(frame_buffer + cx, buf_length - cx, barstring[0]);
376-
else
377-
cx += swprintf(frame_buffer + cx, buf_length - cx, barstring[current_cell]);
409+
} else if (current_cell > 7) {
410+
if (orientation == ORIENT_BOTTOM)
411+
cx += swprintf(frame_buffer + cx, buf_length - cx, barstring[0]);
412+
else if (orientation == ORIENT_TOP)
413+
cx += swprintf(frame_buffer + cx, buf_length - cx, top_barstring[0]);
414+
} else {
415+
if (orientation == ORIENT_BOTTOM)
416+
cx += swprintf(frame_buffer + cx, buf_length - cx,
417+
barstring[current_cell]);
418+
else if (orientation == ORIENT_TOP)
419+
cx += swprintf(frame_buffer + cx, buf_length - cx,
420+
top_barstring[current_cell]);
421+
}
378422

379423
if (bar_spacing)
380424
cx +=

output/terminal_noncurses.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#include "../config.h"
2+
13
int init_terminal_noncurses(int inAtty, char *const fg_color_string, char *const bg_color_string,
24
int col, int bgcol, int gradient, int gradient_count,
35
char **gradient_colors, int w, int h, int bar_width);
46
void get_terminal_dim_noncurses(int *w, int *h);
57
int draw_terminal_noncurses(int inAtty, int lines, int width, int number_of_bars, int bar_width,
68
int bar_spacing, int rest, int bars[], int previous_frame[],
7-
int gradient, int x_axis_info);
9+
int gradient, int x_axis_info, enum orientation orientation);
810
void cleanup_terminal_noncurses(void);

0 commit comments

Comments
 (0)