From 52422d4ce9f00f225814687618e9aa08698a1828 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 10 Feb 2021 09:01:00 -0500 Subject: [PATCH] Add helper commands to navigate terminal --- documents/references.md | 7 +++ makefile | 2 +- src/main.c | 9 ++-- src/simulate_loads.c | 1 + src/terminal_commands.c | 102 ++++++++++++++++++++++++++++++++++++++++ src/terminal_commands.h | 23 +++++++++ 6 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 src/terminal_commands.c create mode 100644 src/terminal_commands.h diff --git a/documents/references.md b/documents/references.md index ab04805..d999b70 100644 --- a/documents/references.md +++ b/documents/references.md @@ -26,3 +26,10 @@ Most parallelization logic is from the book "Introduction to Parallel Programmin ### Other #### strtol Function <https://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm> + +#### Pointer to Pointer +<https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm> + +#### Moving Cursor in Terminal +<https://stackoverflow.com/a/35190285> +<https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html> diff --git a/makefile b/makefile index f98ed0e..55bfd53 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,3 @@ all: - gcc -Wall -Wpedantic -std=c99 src/main.c src/structs.c src/simulate_loads.c -g -o main.out -pthread + gcc -Wall -Wpedantic -std=c99 src/main.c src/structs.c src/simulate_loads.c src/terminal_commands.c -g -o main.out -pthread diff --git a/src/main.c b/src/main.c index 6c86b1a..d512d6b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,7 @@ #define user_headers #include "simulate_loads.h" #include "structs.h" + #include "terminal_commands.h" #endif @@ -93,8 +94,8 @@ void validate_args(int argc, char* argv[]) { // Validate "indexes_per_load" value. Should be between 100 and 10,000. indexes_per_load = strtol(argv[2], NULL, 10); - if ((indexes_per_load < 100) || (indexes_per_load > 10000)) { - printf("Arg2 (indexes_per_load) should be int between 100 and 10,000.\n"); + if ((indexes_per_load < 10) || (indexes_per_load > 10000)) { + printf("Arg2 (indexes_per_load) should be int between 10 and 10,000.\n"); printf("\n"); printf("Terminating program.\n"); exit(1); @@ -102,8 +103,8 @@ void validate_args(int argc, char* argv[]) { // Validate "total_loads" value. Should be between 100 and 10,000. total_loads = strtol(argv[3], NULL, 10); - if ((total_loads < 100) || (total_loads > 10000)) { - printf("Arg3 (total_loads) should be int between 100 and 10,000.\n"); + if ((total_loads < 10) || (total_loads > 10000)) { + printf("Arg3 (total_loads) should be int between 10 and 10,000.\n"); printf("\n"); printf("Terminating program.\n"); exit(1); diff --git a/src/simulate_loads.c b/src/simulate_loads.c index ea3a6d4..5d3cb5d 100644 --- a/src/simulate_loads.c +++ b/src/simulate_loads.c @@ -8,6 +8,7 @@ #define user_headers #include "simulate_loads.h" #include "structs.h" + #include "terminal_commands.h" #endif diff --git a/src/terminal_commands.c b/src/terminal_commands.c new file mode 100644 index 0000000..a44a46b --- /dev/null +++ b/src/terminal_commands.c @@ -0,0 +1,102 @@ +/** + * File for terminal logic. + * + * Note that all of these are in relation to the line the cursor is currently on. + */ + +// User Import Headers. +#ifndef user_headers + #define user_headers + #include "simulate_loads.h" + #include "structs.h" + #include "terminal_commands.h" +#endif + + +/** + * Move to start of current line. + * Note that this does not erase, but any output will write over the respective character. + */ +void terminal_line_start() { + printf("\r"); +} + + +/** + * Move to end of current line. + * Note that th is does appear to erase, from where cursor was, up until end of line. + */ +void terminal_line_end() { + printf("\033[K"); +} + + +/** + * Moves cursor up one single line. + * Cursor will stay at the current column place. + */ +void terminal_line_up() { + printf("\033[A"); +} + + +/** + * Moves cursor down one single line. + * Cursor will stay at the current column place. + */ +void terminal_line_down() { + printf("\033[B"); +} + + +/** + * Moves curor left by one place. + */ +void terminal_arrow_left() { + printf("\033[D"); +} + + +/** + * Moves cursor right by one place. + */ +void terminal_arrow_right() { + printf("\033[C"); +} + + +/** + * Erases entire line cursor is on, and moves cursor back to start of line. + */ +void terminal_line_erase() { + printf("\33[2K"); + terminal_line_start(); +} + + +/** + * Prints full Lorem Ipsum paragraph, with no break characters. + */ +void print_lorem_ipsum_paragraph() { + printf("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tristique lacus vitae pharetra" + "lacinia. Cras eu odio eros. Aliquam erat volutpat. Pellentesque habitant morbi tristique senectus et netus " + "et malesuada fames ac turpis egestas. Mauris neque lacus, tristique eu arcu vel, auctor vehicula sapien. " + "Cras blandit pretium aliquam. Integer malesuada, purus eget interdum placerat, est ipsum auctor lorem, quis" + " mattis magna libero in neque. Morbi eget nunc lobortis, venenatis purus ac, mollis diam."); +} + + +/** + * Prints full Lorem Ipsum paragraph, with each sentence on its own line. + */ +void print_lorem_ipsum_lines() { + printf("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"); + printf("Pellentesque tristique lacus vitae pharetra lacinia.\n"); + printf("Cras eu odio eros.\n"); + printf("Aliquam erat volutpat.\n"); + printf("Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\n"); + printf("Mauris neque lacus, tristique eu arcu vel, auctor vehicula sapien.\n"); + printf("Cras blandit pretium aliquam.\n"); + printf("Integer malesuada, purus eget interdum placerat, est ipsum auctor lorem, quis mattis magna libero in neque.\n"); + printf("Morbi eget nunc lobortis, venenatis purus ac, mollis diam.\n"); +} diff --git a/src/terminal_commands.h b/src/terminal_commands.h new file mode 100644 index 0000000..8a9aca6 --- /dev/null +++ b/src/terminal_commands.h @@ -0,0 +1,23 @@ +/** + * Header file for project terminal logic. + */ + + +// System Import Headers. +#include <ctype.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +// Function Prototypes. +void terminal_line_start(); +void terminal_line_end(); +void terminal_line_up(); +void terminal_line_down(); +void terminal_arrow_left(); +void terminal_arrow_right(); +void terminal_line_erase(); +void print_lorem_ipsum_paragraph(); +void print_lorem_ipsum_lines(); -- GitLab