aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--graph.c54
-rw-r--r--graph.h14
-rw-r--r--main.c13
3 files changed, 55 insertions, 26 deletions
diff --git a/graph.c b/graph.c
index ffbafd40bd26..9313ebfb4a17 100644
--- a/graph.c
+++ b/graph.c
@@ -1,13 +1,37 @@
#include "graph.h"
+void graph_main(const char* ticker_name_string){
+ initscr();
+ noecho();
+ keypad(stdscr, TRUE);
+ curs_set(0);
+ double* data = api_get_hist_5y(ticker_name_string);
+ if (data != NULL) {
+ time_t now = time(NULL);
+ struct tm* ts = localtime(&now);
+ ts->tm_year -= 5;
+ graph_print(data, ts);
+ }
+ else puts("Invalid symbol.");
+
+ int ch;
+ while (1) {
+ ch = getch();
+ if (ch == 'q')
+ break;
+ }
+ endwin();
+ free(data);
+}
+
void graph_print(const double* points, struct tm* start_time) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
- int cols = w.ws_col - 10; // 10 offset per side to give space for graph labels
+ int cols = w.ws_col - 11; // 10 offset to give space for graph labels + 1 for right side
int lines = w.ws_row - ROWS_SPACING;
lines -= lines % ROWS_SPACING; // Round down to multiple of 5
if (cols < 10 || lines < 10) {
- puts("Terminal not large enough.\n");
+ puts("Terminal not large enough.");
return;
}
@@ -24,35 +48,35 @@ void graph_print(const double* points, struct tm* start_time) {
for (int i = lines; i >= 0; i--) {
if (i % ROWS_SPACING == 0) // Print y-axis price labels with width 10
- printf("%9.2lf ", (max - ((lines - i) * line_diff)));
- else printf(" ");
+ printw("%9.2lf ", (max - ((lines - i) * line_diff)));
+ else printw(" ");
for (int j = 0; j < cols; j++) {
dat = points[(int) ((double) j * data_length / cols)];
if (dat <= (max - ((lines - i) * line_diff)) && dat > (min + ((i - 1) * line_diff)))
- putchar('*');
+ addch(ACS_DIAMOND);
else if (i % ROWS_SPACING == 0 && j % COLS_SPACING == 0)
- putchar('+');
+ addch(ACS_PLUS);
else if (i % ROWS_SPACING == 0)
- putchar('-');
+ addch(ACS_HLINE);
else if (j % COLS_SPACING == 0)
- putchar('|');
- else putchar(' ');
+ addch(ACS_VLINE);
+ else addch(' ');
}
- putchar('\n');
+ addch('\n');
}
- printf(" ");
+ printw(" ");
char time_string[16];
- int x = data_length / (COLS_SPACING - 1);
+ double x = (DAYS_TO_BUSINESS_DAYS_RATIO * data_length) / (cols / COLS_SPACING);
struct tm copy = *start_time;
for (int i = 0; i < cols; i++) {
if (i % (2 * COLS_SPACING) == 0 && cols - i > 10) { // Print x-axis date labels every two
if (i != 0)
- copy.tm_mday += 2 * x;
+ copy.tm_mday += x * 2;
mktime(&copy);
strftime(time_string, 16, "%m/%d/%Y", &copy);
- printf("%s ", time_string); // Width 2 * COLS_SPACING
+ printw("%s ", time_string); // Width 2 * COLS_SPACING
}
}
- putchar('\n');
+ addch('\n');
} \ No newline at end of file
diff --git a/graph.h b/graph.h
index 291e7d90fa15..175b0967fa43 100644
--- a/graph.h
+++ b/graph.h
@@ -3,6 +3,7 @@
#define ROWS_SPACING 5
#define COLS_SPACING 12
+#define DAYS_TO_BUSINESS_DAYS_RATIO (29.0/20.0)
#include <stdio.h>
#include <stdlib.h>
@@ -11,6 +12,19 @@
#include <unistd.h>
#include "api.h"
+/**
+ * Main input loop
+ * @param ticker_name_string symbol
+ */
+void graph_main(const char* ticker_name_string);
+
+/**
+ * Prints out a NCurses based graph given an array of daily close prices.
+ * x-axis -- close price
+ * y-axis -- date
+ * @param points daily close prices
+ * @param start_time the starting date
+ */
void graph_print(const double* points, struct tm* start_time);
#endif \ No newline at end of file
diff --git a/main.c b/main.c
index 5099f45ac97d..e53359cb7f1b 100644
--- a/main.c
+++ b/main.c
@@ -51,17 +51,8 @@ int main(int argc, char* argv[]) {
// Info
else if (strcmp(cmd, "info") == 0 && argc == 3)
api_print_info(sym);
- else if (strcmp(cmd, "graph") == 0 && argc == 3) {
- double* data = api_get_hist_5y(sym);
- if (data != NULL) {
- time_t now = time(NULL);
- struct tm* ts = localtime(&now);
- ts->tm_year -= 5; // Lowerbound date is 14 days earlier
- graph_print(data, ts);
- free(data);
- }
- else printf("Invalid symbol.\n");
- }
+ else if (strcmp(cmd, "graph") == 0 && argc == 3)
+ graph_main(sym);
// Check
else if (strcmp(cmd, "check") == 0) {