-
Notifications
You must be signed in to change notification settings - Fork 47
Add bash completions (with some refactoring) #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4ed42e5
Add "split_lines" function to split a buffer into an array of lines
adamtuft 80ad46e
Refactor "city.c" and add `iter_cities` function.
adamtuft 7dd41b2
Move argument definitions into header file and define with macros.
adamtuft 2307a27
Fix a city with comma-separated name - wasn't getting parsed correctly
adamtuft b155651
feat: merge with main and update completion logic
da-luce e107a83
style: run format
da-luce b1c6851
ci: update clang-format
da-luce 9423ef4
fix: brute force clang
da-luce ca881e0
fix: update weird city stuff
da-luce c3cb21d
fix: update city find search
da-luce 7d167b4
fix: fix leak
da-luce 76c0030
test: add tests for iter_city
da-luce 5eaf855
fix: fix leak in iter city
da-luce 80bb1a8
test: add test for split_lines
da-luce ec192bf
style: update test comments
da-luce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* Contains definitions for command line arguments. | ||
| * | ||
| * Each line is an invocation of a macro whose arguments define a command line argument of a particular type. | ||
| * | ||
| * The macro arguments are: | ||
| * - The source-code variable name of the argument definition. | ||
| * - The short and long versions of the argument. | ||
| * - The type of the argument (where applicable). | ||
| * - The help text for the argument. | ||
| * | ||
| * To use these definitions, define the 4 macros below so that they use the arguments, then | ||
| * include this file in the source code. | ||
| * | ||
| * This approach is based on the idea of "X macros" (https://en.wikipedia.org/wiki/X_macro) | ||
| * | ||
| */ | ||
|
|
||
| #if !defined(INCLUDE_ARG_DEFINITION_DBL0) | ||
| #define INCLUDE_ARG_DEFINITION_DBL0(...) | ||
| #endif | ||
|
|
||
| #if !defined(INCLUDE_ARG_DEFINITION_STR0) | ||
| #define INCLUDE_ARG_DEFINITION_STR0(...) | ||
| #endif | ||
|
|
||
| #if !defined(INCLUDE_ARG_DEFINITION_LIT0) | ||
| #define INCLUDE_ARG_DEFINITION_LIT0(...) | ||
| #endif | ||
|
|
||
| #if !defined(INCLUDE_ARG_DEFINITION_INT0) | ||
| #define INCLUDE_ARG_DEFINITION_INT0(...) | ||
| #endif | ||
|
|
||
| INCLUDE_ARG_DEFINITION_DBL0(latitude_arg, "a", "latitude", "<degrees>", "Observer latitude [-90°, 90°] (default: 0.0)"); | ||
| INCLUDE_ARG_DEFINITION_DBL0(longitude_arg, "o", "longitude", "<degrees>", "Observer longitude [-180°, 180°] (default: 0.0)"); | ||
| INCLUDE_ARG_DEFINITION_DBL0(threshold_arg, "t", "threshold", "<float>", "Only render stars brighter than this magnitude (default: 5.0)"); | ||
| INCLUDE_ARG_DEFINITION_DBL0(label_arg, "l", "label-thresh", "<float>", "Label stars brighter than this magnitude (default: 0.25)"); | ||
| INCLUDE_ARG_DEFINITION_DBL0(speed_arg, "s", "speed", "<float>", "Animation speed multiplier (default: 1.0)"); | ||
| INCLUDE_ARG_DEFINITION_DBL0(ratio_arg, "r", "aspect-ratio", "<float>", "Override the calculated terminal cell aspect ratio. Use this if your projection is not 'square.' A value around 2.0 works well for most cases"); | ||
| INCLUDE_ARG_DEFINITION_STR0(datetime_arg, "d", "datetime", "<yyyy-mm-ddThh:mm:ss>", "Observation datetime in UTC"); | ||
| INCLUDE_ARG_DEFINITION_STR0(city_arg, "i", "city", "<city_name>", "Use the latitude and longitude of the provided city. If the name contains multiple words, enclose the name in single or double quotes. For a list of available cities, see: https://github.com/da-luce/astroterm/blob/v" PROJ_VERSION "/data/cities.csv"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(color_arg, "c", "color", "Enable terminal colors"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(constell_arg, "C", "constellations", "Draw constellation stick figures. Note: a constellation is only drawn if all stars in the figure are over the threshold"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(grid_arg, "g", "grid", "Draw an azimuthal grid"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(unicode_arg, "u", "unicode", "Use unicode characters"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(quit_arg, "q", "quit-on-any", "Quit on any keypress (default is to quit on 'q' or 'ESC' only)"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(meta_arg, "m", "metadata", "Display metadata"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(help_arg, "h", "help", "Print this help message"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(completions_arg, "b", "bash-completions", "Print bash completions"); | ||
| INCLUDE_ARG_DEFINITION_LIT0(version_arg, "v", "version", "Display version info and exit"); | ||
| INCLUDE_ARG_DEFINITION_INT0(fps_arg, "f", "fps", "<int>", "Frames per second (default: 24)"); | ||
|
|
||
| #undef INCLUDE_ARG_DEFINITION_DBL0 | ||
| #undef INCLUDE_ARG_DEFINITION_STR0 | ||
| #undef INCLUDE_ARG_DEFINITION_LIT0 | ||
| #undef INCLUDE_ARG_DEFINITION_INT0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #ifndef SPLIT_LINES_H | ||
| #define SPLIT_LINES_H | ||
|
|
||
| char **split_lines(char *data, int *line_count_out); | ||
|
|
||
| #endif // SPLIT_LINES_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #include "split_lines.h" | ||
|
|
||
| /** | ||
| * Splits a buffer into lines. | ||
| * | ||
| * @param data The buffer to split (will be modified). | ||
| * @param line_count_out Pointer to int to store the number of lines. | ||
| * @return Array of lines, or NULL on error. Caller must free each line and the array. | ||
| */ | ||
| char **split_lines(char *data, int *line_count_out) | ||
| { | ||
| char **lines = NULL; | ||
| size_t line_count = 0; | ||
| const char *line = strtok(data, "\n"); | ||
| while (line != NULL) | ||
| { | ||
| char *line_copy = strdup(line); | ||
| if (line_copy == NULL) | ||
| { | ||
| perror("Memory allocation failed"); | ||
| for (size_t i = 0; i < line_count; i++) | ||
| { | ||
| free(lines[i]); | ||
| } | ||
| free(lines); | ||
| return NULL; | ||
| } | ||
|
|
||
| char **temp = realloc(lines, (line_count + 1) * sizeof(char *)); | ||
| if (temp == NULL) | ||
| { | ||
| perror("Memory allocation failed"); | ||
| free(line_copy); | ||
| for (size_t i = 0; i < line_count; i++) | ||
| { | ||
| free(lines[i]); | ||
| } | ||
| free(lines); | ||
| return NULL; | ||
| } | ||
| lines = temp; | ||
|
|
||
| lines[line_count++] = line_copy; | ||
| line = strtok(NULL, "\n"); | ||
| } | ||
| if (line_count_out) | ||
| *line_count_out = (int)line_count; | ||
| return lines; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, this was factored out since the same logic is used in
get_cityand the newiter_city?