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