Skip to content

Add bash completions (with some refactoring)#80

Merged
da-luce merged 15 commits intoda-luce:mainfrom
adamtuft:dev-bash-completions
Apr 1, 2026
Merged

Add bash completions (with some refactoring)#80
da-luce merged 15 commits intoda-luce:mainfrom
adamtuft:dev-bash-completions

Conversation

@adamtuft
Copy link
Copy Markdown
Contributor

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-completions argument. This is used in bash with

eval "$(astroterm -b)"

Then you can use tab-completion at the command line, for example:

$ astroterm --city Los\ <Tab>
Los\ Angeles   Los\ Mochis    Los\ Teques

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.

adamtuft added 4 commits June 10, 2025 10:04
- 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.
@da-luce da-luce self-assigned this Mar 30, 2026
@da-luce da-luce linked an issue Mar 30, 2026 that may be closed by this pull request
@da-luce
Copy link
Copy Markdown
Owner

da-luce commented Mar 30, 2026

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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Owner

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_city and the new iter_city?

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 73.74302% with 47 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/city.c 63.63% 8 Missing and 16 partials ⚠️
src/split_lines.c 38.46% 11 Missing and 5 partials ⚠️
test/misc_test.c 89.28% 0 Missing and 6 partials ⚠️
test/city_test.c 95.83% 0 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
src/core.c 68.77% <100.00%> (ø)
test/drawing_test.c 81.50% <100.00%> (ø)
test/city_test.c 86.30% <95.83%> (+4.66%) ⬆️
test/misc_test.c 89.28% <89.28%> (ø)
src/split_lines.c 38.46% <38.46%> (ø)
src/city.c 48.80% <63.63%> (-0.70%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@da-luce da-luce merged commit a6141be into da-luce:main Apr 1, 2026
@andreasstieger
Copy link
Copy Markdown

I do not think that eval "$(astroterm -b)" is the best way to deliver this.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add bash completions

3 participants