Add bash completions (with some refactoring)#80
Conversation
- Implement "split_lines" to split a given buffer into an array of lines, writing the number of lines to an output pointer. - Add to src/meson.build This is helpful for a later commit where `split_lines` will be used to iterate over the lines in the embedded cities data.
- The logic in `get_city` for iterating over lines has been moved into `split_lines`. - Refactor "city.c" to use `split_lines`. - Implement `iter_cities` to apply a callback to all cities.
- Arguments are defined in the new file "arg_definitions.h" and used by including this file with appropriate macro definitions. - Add a "-b|--bash-completions" option which prints bash completions to stdout. Use `eval $(astroterm -b)` to enable them in bash. Explanation: I could have copy-pasted the arguments into the bash completions code, but this would have made it more brittle to add/change arguments in the future. This approach means you have a single place where arguments are defined, and they can be re-used with macros. This approach of iterating over cities also means that if the list of cities changes in the future, the completions will remain up-to-date.
|
Hey, first off--thanks for this incredible addition! Sorry it's taken me so long to actually take a look. My goal is to get this in during the next few days |
| Manhattan,1487536,US,America/New_York,40.78343,-73.96625 | ||
| Manila,1600000,PH,Asia/Manila,14.6042,120.9822 | ||
| Manisa,243971,TR,Europe/Istanbul,38.61202,27.42647 | ||
| Manizales,434403,CO,America/Bogota,5.0668,-75.50684 | ||
| Manizales,434403,CO,America/Bogota,5.06889,-75.51738 | ||
| Mannheim,307960,DE,Europe/Berlin,49.4891,8.46694 |
There was a problem hiding this comment.
I'm guessing the issue here is the quotes within the city name? I can make an addition to cities.csv script to fix this from the get-go
| * @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) |
There was a problem hiding this comment.
Just to confirm, this was factored out since the same logic is used in get_city and the new iter_city?
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
|
I do not think that If you have dynamic parameter lists then these should be retrieved when you actually perform a completion, not when you load the completion into the shell. And to be frank, a city list does not warrant this level of complexity. If this is the only changing parameter list, just deliver a static completion script and skip the city |
Hello,
Thanks for the awesome app! I was pointed here by https://www.youtube.com/watch?v=3BxIpVk_xZI&t=430s and thought it would be cool to contribute, so I implemented bash completions for you (#54). This turned out to be a bit more involved than I expected, and I had to do a little refactoring along the way, but this should all be explained in the commits.
The completions are printed to stdout with a new
-b|--bash-completionsargument. This is used in bash withThen you can use tab-completion at the command line, for example:
The implementation of this wasn't straightforward - it has to deal with several levels of string quoting and un-quoting since some city names have embedded spaces. I also had to refactor the code for parsing options a little so that I didn't just end up copy-pasting the options into the bash completion code.
Take a look and let me know what you think. Happy to discuss anything that's unclear or which you're not happy with.