Skip to main content

dev_bestia_cargo_completion_lib/
lib.rs

1// dev_bestia_cargo_completion/src/lib.rs
2
3// logo for docs.rs in png
4#![doc(html_logo_url = "https://github.com/automation-tasks-rs/cargo-auto/raw/main/images/logo/logo_cargo_auto.svg")]
5// region: auto_md_to_doc_comments include README.md A //!
6//! # dev_bestia_cargo_completion  
7//!
8//! **Auto-completion for cargo-auto and automation_tasks_rs and partial auto-completion for cargo in bash**  
9//! ***version: 2026.306.1702 date: 2026-03-06 author: [bestia.dev](https://bestia.dev) repository: [codeberg.org](https://codeberg.org/automation-tasks-rs/dev_bestia_cargo_completion)***
10//!
11//!  ![maintained](https://img.shields.io/badge/maintained-green)
12//!  ![ready-for-use](https://img.shields.io/badge/ready_for_use-green)
13//!  ![rustlang](https://img.shields.io/badge/rustlang-orange)
14//!  ![automation](https://img.shields.io/badge/automation-orange)
15//!  ![workflow](https://img.shields.io/badge/workflow-orange)
16//!
17//!  ![logo](https://codeberg.org/automation-tasks-rs/cargo-auto/raw/branch/main/images/logo/logo_cargo_auto.svg)
18//!  dev_bestia_cargo_completion is part of the [automation_tasks_rs](https://codeberg.org/automation-tasks-rs) project
19//!
20//!  [![crates.io](https://img.shields.io/crates/v/dev_bestia_cargo_completion.svg)](https://crates.io/crates/dev_bestia_cargo_completion)
21//!  [![Documentation](https://img.shields.io/badge/docs-stable-green.svg)](https://docs.rs/dev_bestia_cargo_completion/)
22//!  ![License](https://img.shields.io/badge/license-MIT-blue.svg)
23//!
24//!  [![Newest docs](https://img.shields.io/badge/docs-development-yellow.svg)](https://bestia.dev/docs/dev_bestia_cargo_completion)
25//!  ![dev_bestia_cargo_completion](https://bestia.dev/webpage_hit_counter/get_svg_image/710310517.svg)
26//!
27//!   ![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-71-green.svg)
28//!   ![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-222-blue.svg)
29//!   ![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-20-purple.svg)
30//!   ![Lines in examples](https://img.shields.io/badge/Lines_in_examples-0-yellow.svg)
31//!   ![Lines in tests](https://img.shields.io/badge/Lines_in_tests-0-orange.svg)
32//!
33//! Hashtags: #maintained #ready-for-use #rustlang #automation #workflow  
34//! My projects on codeberg.org are more like a tutorial than a finished product: [bestia-dev tutorials](https://codeberg.org/bestia-dev-ready-for-use/tutorials_rust_wasm).  
35//! I recommend using the [CRUSTDE - Containerized Rust Development Environment](https://codeberg.org/CRUSTDE-ContainerizedRustDevEnv/crustde_cnt_img_pod) to write Rust projects on Linux, isolated from your system.  
36//!
37//! ## Try it
38//!
39//! Install the binary:
40//!
41//! ```bash
42//! cargo install dev_bestia_cargo_completion
43//! ```
44//!
45//! Save definition for auto_completion in bash:
46//!
47//! ```bash
48//! complete -C "dev_bestia_cargo_completion" cargo
49//! ```
50//!
51//! Start typing `cargo b` and press `tab`.  
52//! It should auto-complete to `cargo build`.  
53//! Congratulation! You just used auto-completion :-)  
54//!
55//! ## bash auto completion
56//!
57//! Auto-completion in Debian bash is a great tool. You type the first letters, press tab and the word is auto-completed.  
58//! Bash can call an executable binary to return the available words. So it can be written in Rust. This can produce even better results as known as `dynamic auto-completion`.  
59//! For my knowledge `cargo` does not have auto-completion yet. There are many plans. It can take some time.  
60//! I will build what I need now. Something simple. It doesn't need to be perfect.  
61//! This is a great blog:  
62//! <https://www.joshmcguigan.com/blog/shell-completions-pure-rust/>
63//!
64//! ## complete, the Linux command
65//!
66//! The Linux command `complete` adds auto_completion definitions to bash.  
67//! But it is only for the current session. If you want to make it persistent add it to the `~/.bashrc` file.  
68//!
69//! ```bash
70//! # list the definitions
71//! complete
72//! # delete a definition
73//! complete -r cargo
74//! # define a binary to auto-complete the command
75//! complete -C "binary" command
76//! # for example
77//! complete -C "dev_bestia_cargo_completion" cargo
78//! ```
79//!
80//! ## Development details
81//!
82//! Read the development details in a separate md file:  
83//! [DEVELOPMENT.md](https://codeberg.org/automation-tasks-rs/dev_bestia_cargo_completion/src/main/DEVELOPMENT.md)
84//!
85//! ## Releases changelog
86//!
87//! Read the changelog in a separate md file:  
88//! [RELEASES.md](https://codeberg.org/automation-tasks-rs/dev_bestia_cargo_completion/src/main/RELEASES.md)
89//!
90//! ## TODO
91//!
92//! Nothing big in the near future.
93//!
94//! ## Open-source and free as a beer
95//!
96//! My open-source projects are free as a beer (MIT license).  
97//! I just love programming.  
98//! But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my [PayPal](https://paypal.me/LucianoBestia).  
99//! You know the price of a beer in your local bar ;-)  
100//! So I can drink a free beer for your health :-)  
101//! [Na zdravje!](https://translate.google.com/?hl=en&sl=sl&tl=en&text=Na%20zdravje&op=translate) [Alla salute!](https://dictionary.cambridge.org/dictionary/italian-english/alla-salute) [Prost!](https://dictionary.cambridge.org/dictionary/german-english/prost) [Nazdravlje!](https://matadornetwork.com/nights/how-to-say-cheers-in-50-languages/) 🍻
102//!
103//! [//bestia.dev](https://bestia.dev)  
104//! [//codeberg.org/bestia-dev](https://codeberg.org/bestia-dev)  
105//! [//github.com/bestia-dev](https://github.com/bestia-dev)  
106//! [//bestiadev.substack.com](https://bestiadev.substack.com)  
107//! [//youtube.com/@bestia-dev-tutorials](https://youtube.com/@bestia-dev-tutorials)  
108//!
109// endregion: auto_md_to_doc_comments include README.md A //!
110
111/// partial completion of the cargo command
112/// first word after `cargo`
113pub fn complete_cargo_partial(vec_comp_line_len: usize, word_being_completed: &str) {
114    let sub_commands_after_cargo = vec!["auto", "build", "check", "new", "doc", "test", "fmt", "install"];
115    for sub_command in sub_commands_after_cargo {
116        // list all for `tab tab` or list only one starting with the word
117        if vec_comp_line_len == 1 || sub_command.starts_with(word_being_completed) {
118            println!("{}", sub_command);
119        }
120    }
121}
122
123/// partial completion of the cargo build command
124/// first word after `cargo build`
125pub fn complete_cargo_build_partial(vec_comp_line_len: usize, word_being_completed: &str) {
126    let sub_commands = vec!["--release"];
127
128    for sub_command in sub_commands {
129        // list all for `tab tab` or list only one starting with the word
130        if vec_comp_line_len == 2 || sub_command.starts_with(word_being_completed) {
131            println!("{}", sub_command);
132        }
133    }
134}
135
136/// words after `cargo auto` execute the appropriate binary, that responds with println
137/// 1st argument in "completion"
138/// 2rd argument is the `word_being_completed`
139/// 3nd argument is the `last_word`
140pub fn complete_automation(word_being_completed: &str, last_word: &str) {
141    let path_to_automation = "automation_tasks_rs/target/debug/automation_tasks_rs";
142    if std::path::Path::new(path_to_automation).exists() {
143        std::process::Command::new(path_to_automation)
144            .arg("completion")
145            .arg(word_being_completed)
146            .arg(last_word)
147            .spawn()
148            .unwrap()
149            .wait()
150            .unwrap();
151    }
152}
153
154/// words after `cargo auto` execute the appropriate binary, that responds with println
155/// 1st argument in "completion"
156/// 2rd argument is the `word_being_completed`
157/// 3nd argument is the `last_word`
158pub fn complete_cargo_auto(word_being_completed: &str, last_word: &str) {
159    std::process::Command::new("cargo-auto")
160        .arg("completion")
161        .arg(word_being_completed)
162        .arg(last_word)
163        .spawn()
164        .unwrap()
165        .wait()
166        .unwrap();
167}