cantino / mcfly

Fly through your shell history. Great Scott!
MIT License
6.87k stars 177 forks source link

Embed init #119

Closed b3nj5m1n closed 3 years ago

b3nj5m1n commented 3 years ago

This implements a new subcommand called init, similar to starship, zoxide, etc., as proposed in #111.

Instead of having to manually download the mcfly.[bash/zsh/fish] file, they're now embed in the binary, so instead you can just call mcfly init [bash/zsh/fish], which will print the contents of that file. You would put something like this in your bashrc: eval "$(mcfly init bash)", I've already updated the README to reflect these changes.

A few notes:

I'm a beginner at rust, so if there's anything at all wrong with my code, don't hesitate to point it out.

I had to alter the checks for MCFLY_SESSION_ID and MCFLY_HISTORY, I'm not sure if this is the ideal solution, although I could also see an option to disable these checks as being something useful in the future.

I'm currently using include_str!, as @em- pointed out, there's also rust-embed, which would either read the file from disk when running in dev mode, or include the file in the binary in production. I didn't do it like that because I didn't want to add another dependency, if that's something you'd be okay with it might be worth considering to switch to that.

cantino commented 3 years ago

Cool idea @b3nj5m1n, thank you for your contribution! Maybe is_dummy could be renamed to skip_environment_check?

b3nj5m1n commented 3 years ago

Certainly a much better name, that was the very last thing I implemented, so I didn't put much thought into it, I should have the commit ready in a few minutes.

cantino commented 3 years ago

Thanks! I'll test this out and get back to you.

b3nj5m1n commented 3 years ago

@cantino Did you have a chance to try it out yet?

cantino commented 3 years ago

Hey @b3nj5m1n, I just did some testing. I found a couple issues.

  1. def caveats in HomebrewFormula/mcfly.rb needs to be updated too.
  2. Running mcfly init prints Please specify a shell to init for, but then prints out a shell script. I think you should change the following:
diff --git a/src/settings.rs b/src/settings.rs
index 22863ee..6401603 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -218,12 +218,10 @@ impl Settings {
                     .required(false)))
             .subcommand(SubCommand::with_name("init")
                 .about("Prints the shell code used to execute mcfly")
-                .subcommand(SubCommand::with_name("bash")
-                    .about("Prints the shell code used to execute mcfly in bash"))
-                .subcommand(SubCommand::with_name("zsh")
-                    .about("Prints the shell code used to execute mcfly in zsh"))
-                .subcommand(SubCommand::with_name("fish")
-                    .about("Prints the shell code used to execute mcfly in fish"))
+                .arg(Arg::with_name("shell")
+                    .help("Shell to init — one of bash, zsh, or fish")
+                    .possible_values(&["bash", "zsh", "fish"])
+                    .required(true))
             )
             .get_matches();

@@ -411,18 +409,17 @@ impl Settings {

             ("init", Some(init_matches)) => {
                 settings.mode = Mode::Init;
-                match init_matches.subcommand() {
-                    ("bash", Some(_bash_matches)) => {
+                match init_matches.value_of("shell").unwrap() {
+                    "bash" => {
                         settings.init_mode = InitMode::Bash;
                     }
-                    ("zsh", Some(_zsh_matches)) => {
+                    "zsh" => {
                         settings.init_mode = InitMode::Zsh;
                     }
-                    ("fish", Some(_fish_matches)) => {
+                    "fish" => {
                         settings.init_mode = InitMode::Fish;
                     }
-                    ("", None) => println!("Please specify a shell to init for"), // If no subcommand was used it'll match the tuple ("", None)
-                    _ => unreachable!(), // If all subcommands are defined above, anything else is unreachable!()
+                    _ => unreachable!(),
                 }
             }
b3nj5m1n commented 3 years ago

Running mcfly init prints Please specify a shell to init for, but then prints out a shell script. I think you should change the following:

I noticed that yesterday as well, I fixed it in a different way, which might be suboptimal, feel free to request changes again if you want it done like in your example.

b3nj5m1n commented 3 years ago

@cantino Sorry I was busy the last few days, I believe the failed check wasn't a mistake on my part but rather a network error:

Logs

``` $ bash ci/script.sh +'[' -z ']' +main +cross build --target arm-unknown-linux-gnueabi Unable to find image 'rustembedded/cross:arm-unknown-linux-gnueabi-0.2.1' locally arm-unknown-linux-gnueabi-0.2.1: Pulling from rustembedded/cross b5e173e44934: Pulling fs layer 29047100b040: Pulling fs layer 15743a713c2a: Pulling fs layer 4b6bc9e29877: Pulling fs layer 830723f5e0c6: Pulling fs layer a08065183a68: Pulling fs layer 45b3d9d602a9: Pulling fs layer e1c7941152c8: Pulling fs layer 00bc118d4a1e: Pulling fs layer 84033d57b1a9: Pulling fs layer d1a0f5ce8cd6: Pulling fs layer 31a6615b386b: Pulling fs layer 4b6bc9e29877: Waiting 830723f5e0c6: Waiting a08065183a68: Waiting 45b3d9d602a9: Waiting e1c7941152c8: Waiting 00bc118d4a1e: Waiting 84033d57b1a9: Waiting d1a0f5ce8cd6: Waiting 31a6615b386b: Waiting b5e173e44934: Retrying in 5 seconds 29047100b040: Retrying in 5 seconds 15743a713c2a: Retrying in 5 seconds b5e173e44934: Retrying in 4 seconds 29047100b040: Retrying in 4 seconds 15743a713c2a: Retrying in 4 seconds b5e173e44934: Retrying in 3 seconds 29047100b040: Retrying in 3 seconds 15743a713c2a: Retrying in 3 seconds b5e173e44934: Retrying in 2 seconds 29047100b040: Retrying in 2 seconds 15743a713c2a: Retrying in 2 seconds b5e173e44934: Retrying in 1 second 29047100b040: Retrying in 1 second 15743a713c2a: Retrying in 1 second 29047100b040: Retrying in 10 seconds b5e173e44934: Retrying in 10 seconds 15743a713c2a: Retrying in 10 seconds 29047100b040: Retrying in 9 seconds b5e173e44934: Retrying in 9 seconds 15743a713c2a: Retrying in 9 seconds 29047100b040: Retrying in 8 seconds b5e173e44934: Retrying in 8 seconds 15743a713c2a: Retrying in 8 seconds 29047100b040: Retrying in 7 seconds b5e173e44934: Retrying in 7 seconds 15743a713c2a: Retrying in 7 seconds 29047100b040: Retrying in 6 seconds b5e173e44934: Retrying in 6 seconds 15743a713c2a: Retrying in 6 seconds 29047100b040: Retrying in 5 seconds b5e173e44934: Retrying in 5 seconds 15743a713c2a: Retrying in 5 seconds 29047100b040: Retrying in 4 seconds b5e173e44934: Retrying in 4 seconds 15743a713c2a: Retrying in 4 seconds 29047100b040: Retrying in 3 seconds b5e173e44934: Retrying in 3 seconds 15743a713c2a: Retrying in 3 seconds 29047100b040: Retrying in 2 seconds b5e173e44934: Retrying in 2 seconds 15743a713c2a: Retrying in 2 seconds 29047100b040: Retrying in 1 second b5e173e44934: Retrying in 1 second 15743a713c2a: Retrying in 1 second 29047100b040: Retrying in 15 seconds b5e173e44934: Retrying in 15 seconds 15743a713c2a: Retrying in 15 seconds 29047100b040: Retrying in 14 seconds b5e173e44934: Retrying in 14 seconds 15743a713c2a: Retrying in 14 seconds 29047100b040: Retrying in 13 seconds 15743a713c2a: Retrying in 13 seconds b5e173e44934: Retrying in 13 seconds 29047100b040: Retrying in 12 seconds b5e173e44934: Retrying in 12 seconds 15743a713c2a: Retrying in 12 seconds 29047100b040: Retrying in 11 seconds b5e173e44934: Retrying in 11 seconds 15743a713c2a: Retrying in 11 seconds 29047100b040: Retrying in 10 seconds b5e173e44934: Retrying in 10 seconds 15743a713c2a: Retrying in 10 seconds 29047100b040: Retrying in 9 seconds 15743a713c2a: Retrying in 9 seconds b5e173e44934: Retrying in 9 seconds 29047100b040: Retrying in 8 seconds b5e173e44934: Retrying in 8 seconds 15743a713c2a: Retrying in 8 seconds 29047100b040: Retrying in 7 seconds b5e173e44934: Retrying in 7 seconds 15743a713c2a: Retrying in 7 seconds 29047100b040: Retrying in 6 seconds b5e173e44934: Retrying in 6 seconds 15743a713c2a: Retrying in 6 seconds 29047100b040: Retrying in 5 seconds b5e173e44934: Retrying in 5 seconds 15743a713c2a: Retrying in 5 seconds 29047100b040: Retrying in 4 seconds b5e173e44934: Retrying in 4 seconds 15743a713c2a: Retrying in 4 seconds 29047100b040: Retrying in 3 seconds b5e173e44934: Retrying in 3 seconds 15743a713c2a: Retrying in 3 seconds 29047100b040: Retrying in 2 seconds b5e173e44934: Retrying in 2 seconds 15743a713c2a: Retrying in 2 seconds 29047100b040: Retrying in 1 second b5e173e44934: Retrying in 1 second 15743a713c2a: Retrying in 1 second 29047100b040: Retrying in 20 seconds b5e173e44934: Retrying in 20 seconds 15743a713c2a: Retrying in 20 seconds 29047100b040: Retrying in 19 seconds b5e173e44934: Retrying in 19 seconds 15743a713c2a: Retrying in 19 seconds 29047100b040: Retrying in 18 seconds b5e173e44934: Retrying in 18 seconds 15743a713c2a: Retrying in 18 seconds 29047100b040: Retrying in 17 seconds b5e173e44934: Retrying in 17 seconds 15743a713c2a: Retrying in 17 seconds 29047100b040: Retrying in 16 seconds b5e173e44934: Retrying in 16 seconds 15743a713c2a: Retrying in 16 seconds 29047100b040: Retrying in 15 seconds b5e173e44934: Retrying in 15 seconds 15743a713c2a: Retrying in 15 seconds 29047100b040: Retrying in 14 seconds b5e173e44934: Retrying in 14 seconds 15743a713c2a: Retrying in 14 seconds 29047100b040: Retrying in 13 seconds b5e173e44934: Retrying in 13 seconds 15743a713c2a: Retrying in 13 seconds 29047100b040: Retrying in 12 seconds b5e173e44934: Retrying in 12 seconds 15743a713c2a: Retrying in 12 seconds 29047100b040: Retrying in 11 seconds b5e173e44934: Retrying in 11 seconds 15743a713c2a: Retrying in 11 seconds 29047100b040: Retrying in 10 seconds 15743a713c2a: Retrying in 10 seconds b5e173e44934: Retrying in 10 seconds 29047100b040: Retrying in 9 seconds b5e173e44934: Retrying in 9 seconds 15743a713c2a: Retrying in 9 seconds 29047100b040: Retrying in 8 seconds b5e173e44934: Retrying in 8 seconds 15743a713c2a: Retrying in 8 seconds 29047100b040: Retrying in 7 seconds b5e173e44934: Retrying in 7 seconds 15743a713c2a: Retrying in 7 seconds 29047100b040: Retrying in 6 seconds b5e173e44934: Retrying in 6 seconds 15743a713c2a: Retrying in 6 seconds 29047100b040: Retrying in 5 seconds b5e173e44934: Retrying in 5 seconds 15743a713c2a: Retrying in 5 seconds 29047100b040: Retrying in 4 seconds b5e173e44934: Retrying in 4 seconds 15743a713c2a: Retrying in 4 seconds 29047100b040: Retrying in 3 seconds b5e173e44934: Retrying in 3 seconds 15743a713c2a: Retrying in 3 seconds 29047100b040: Retrying in 2 seconds b5e173e44934: Retrying in 2 seconds 15743a713c2a: Retrying in 2 seconds 29047100b040: Retrying in 1 second b5e173e44934: Retrying in 1 second 15743a713c2a: Retrying in 1 second /usr/bin/docker: toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit. See '/usr/bin/docker run --help'. ```

b3nj5m1n commented 3 years ago

@cantino I had some time today, so I made the changes you originally requested, let me know if you have any feedback!

b3nj5m1n commented 3 years ago

@cantino Have you had the chance to take a look?

cantino commented 3 years ago

Sorry for the delay :)

Looks great! One final thing: please run cargo clippy and cargo fmt to fix some style errors, then I think we're good to go.

b3nj5m1n commented 3 years ago

@cantino That should have fixed everything I worked on, let me know if I've missed anything.

cantino commented 3 years ago

Thanks @b3nj5m1n!