Skip to content

Commit

Permalink
Merge pull request #119 from b3nj5m1n/embed-init
Browse files Browse the repository at this point in the history
Embed init
  • Loading branch information
cantino committed Mar 1, 2021
2 parents 0c7669c + d03351e commit d87500f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 62 deletions.
57 changes: 16 additions & 41 deletions README.md
Expand Up @@ -51,24 +51,18 @@ When suggesting a command, McFly takes into consideration:

Bash:
```bash
if [[ -r "/usr/local/opt/mcfly/mcfly.bash" ]]; then
source "/usr/local/opt/mcfly/mcfly.bash"
fi
eval "$(mcfly init bash)"
```

Zsh:
```bash
if [[ -r "/usr/local/opt/mcfly/mcfly.zsh" ]]; then
source "/usr/local/opt/mcfly/mcfly.zsh"
fi
eval "$(mcfly init zsh)"
```

Fish:
```bash
if test -r "/usr/local/opt/mcfly/mcfly.fish"
source "/usr/local/opt/mcfly/mcfly.fish"
mcfly_key_bindings
end
mcfly init fish | source
mcfly_key_bindings
```
1. Run `. ~/.bashrc` / `. ~/.zshrc` / `source ~/.config/fish/config.fish` or restart your terminal emulator.

Expand Down Expand Up @@ -98,24 +92,18 @@ When suggesting a command, McFly takes into consideration:

Bash:
```bash
if [[ -r "/opt/local/share/mcfly/mcfly.bash" ]]; then
source "/opt/local/share/mcfly/mcfly.bash"
fi
eval "$(mcfly init bash)"
```

Zsh:
```bash
if [[ -r "/opt/local/share/mcfly/mcfly.zsh" ]]; then
source "/opt/local/share/mcfly/mcfly.zsh"
fi
eval "$(mcfly init zsh)"
```

Fish:
```bash
if test -r "/opt/local/share/mcfly/mcfly.fish"
source "/opt/local/share/mcfly/mcfly.fish"
mcfly_key_bindings
end
mcfly init fish | source
mcfly_key_bindings
```
1. Run `. ~/.bashrc` / `. ~/.zshrc` / `source ~/.config/fish/config.fish` or restart your terminal emulator.

Expand All @@ -131,29 +119,22 @@ When suggesting a command, McFly takes into consideration:

1. Download the [latest release from GitHub](https://github.com/cantino/mcfly/releases).
1. Install to a location in your `$PATH`. (For example, you could create a directory at `~/bin`, copy `mcfly` to this location, and add `export PATH="$PATH:$HOME/bin"` to your `.bashrc` / `.zshrc`, or run `set -Ua fish_user_paths "$HOME/bin"` for fish.)
1. Copy `mcfly.bash`, `mcfly.zsh`, or `mcfly.fish` to a known location.
1. Add the following to the end of your `~/.bashrc`, `~/.zshrc`, or `~/.config/fish/config.fish` file, respectively:

Bash:
```bash
if [[ -r /path/to/mcfly.bash ]]; then
source /path/to/mcfly.bash
fi
eval "$(mcfly init bash)"
```

Zsh:
```bash
if [[ -r /path/to/mcfly.zsh ]]; then
source /path/to/mcfly.zsh
fi
eval "$(mcfly init zsh)"
```

Fish:
```bash
if test -r /path/to/mcfly.fish
source /path/to/mcfly.fish
mcfly_key_bindings
end
mcfly init fish | source
mcfly_key_bindings
```
1. Run `. ~/.bashrc` / `. ~/.zshrc` / `source ~/.config/fish/config.fish` or restart your terminal emulator.

Expand All @@ -167,24 +148,18 @@ When suggesting a command, McFly takes into consideration:

Bash:
```bash
if [[ -r /path/to/mcfly.bash ]]; then
source /path/to/mcfly.bash
fi
eval "$(mcfly init bash)"
```

Zsh:
```bash
if [[ -r /path/to/mcfly.zsh ]]; then
source /path/to/mcfly.zsh
fi
eval "$(mcfly init zsh)"
```

Fish:
```bash
if test -r /path/to/mcfly.fish
source /path/to/mcfly.fish
mcfly_key_bindings
end
mcfly init fish | source
mcfly_key_bindings
```
1. Run `. ~/.bashrc` / `. ~/.zshrc` / `source ~/.config/fish/config.fish` or restart your terminal emulator.

Expand Down
20 changes: 5 additions & 15 deletions pkg/brew/mcfly.rb
Expand Up @@ -20,34 +20,24 @@ class Mcfly < Formula
end

def install
prefix.install "mcfly.bash"
prefix.install "mcfly.zsh"
prefix.install "mcfly.fish"
bin.install "mcfly"
end

def caveats
<<~EOS
ONE MORE STEP!
Add the following to the end of your ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish file,
as appropriate, changing /usr/local to your 'brew --prefix' if needed:
Add the following to the end of your ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish file.
Bash:
if [ -r /usr/local/opt/mcfly/mcfly.bash ]; then
. /usr/local/opt/mcfly/mcfly.bash
fi
eval "$(mcfly init bash)"
Zsh:
if [ -r /usr/local/opt/mcfly/mcfly.zsh ]; then
. /usr/local/opt/mcfly/mcfly.zsh
fi
eval "$(mcfly init zsh)"
Fish:
if test -r /usr/local/opt/mcfly/mcfly.fish
source /usr/local/opt/mcfly/mcfly.fish
mcfly_key_bindings
end
mcfly init fish | source
mcfly_key_bindings
EOS
end
end
32 changes: 32 additions & 0 deletions src/init.rs
@@ -0,0 +1,32 @@
use super::settings::InitMode;

pub struct Init {}

impl Init {
pub fn new(init_mode: &InitMode) -> Self {
match init_mode {
InitMode::Bash => {
Init::init_bash();
}
InitMode::Zsh => {
Init::init_zsh();
}
InitMode::Fish => {
Init::init_fish();
}
}
Self {}
}
pub fn init_bash() {
let script = include_str!("../mcfly.bash");
print!("{}", script);
}
pub fn init_zsh() {
let script = include_str!("../mcfly.zsh");
print!("{}", script);
}
pub fn init_fish() {
let script = include_str!("../mcfly.fish");
print!("{}", script);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -3,6 +3,7 @@ pub mod fake_typer;
pub mod fixed_length_grapheme_string;
pub mod history;
pub mod history_cleaner;
pub mod init;
pub mod interface;
pub mod network;
pub mod node;
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
@@ -1,5 +1,6 @@
use mcfly::fake_typer;
use mcfly::history::History;
use mcfly::init::Init;
use mcfly::interface::Interface;
use mcfly::settings::Mode;
use mcfly::settings::Settings;
Expand Down Expand Up @@ -93,6 +94,10 @@ fn handle_move(settings: &Settings, history: &mut History) {
history.update_paths(&settings.old_dir.clone().unwrap(), &settings.dir, true);
}

fn handle_init(settings: &Settings, _history: &mut History) {
Init::new(&settings.init_mode);
}

fn main() {
let settings = Settings::parse_args();

Expand All @@ -111,5 +116,8 @@ fn main() {
Mode::Move => {
handle_move(&settings, &mut history);
}
Mode::Init => {
handle_init(&settings, &mut history);
}
}
}
63 changes: 57 additions & 6 deletions src/settings.rs
Expand Up @@ -15,6 +15,7 @@ pub enum Mode {
Search,
Train,
Move,
Init,
}

#[derive(Debug)]
Expand All @@ -23,6 +24,13 @@ pub enum KeyScheme {
Vim,
}

#[derive(Debug)]
pub enum InitMode {
Bash,
Zsh,
Fish,
}

#[derive(Debug, Clone, Copy)]
pub enum HistoryFormat {
/// bash format - commands in plain text, one per line, with multi-line commands joined.
Expand Down Expand Up @@ -60,6 +68,8 @@ pub struct Settings {
pub lightmode: bool,
pub key_scheme: KeyScheme,
pub history_format: HistoryFormat,
pub skip_environment_check: bool,
pub init_mode: InitMode,
}

impl Default for Settings {
Expand All @@ -82,6 +92,8 @@ impl Default for Settings {
lightmode: false,
key_scheme: KeyScheme::Emacs,
history_format: HistoryFormat::Bash,
skip_environment_check: false,
init_mode: InitMode::Bash,
}
}
}
Expand Down Expand Up @@ -203,28 +215,51 @@ impl Settings {
.long("refresh_cache")
.help("Directory where command was run")
.required(false)))
.subcommand(SubCommand::with_name("init")
.about("Prints the shell code used to execute mcfly")
.arg(Arg::with_name("shell")
.help("Shell to init — one of bash, zsh, or fish")
.possible_values(&["bash", "zsh", "fish"])
.required(true))
)
.get_matches();

let mut settings = Settings::default();
if matches.is_present("init") {
settings.skip_environment_check = true;
}

settings.debug = matches.is_present("debug") || env::var("MCFLY_DEBUG").is_ok();
settings.session_id = matches
.value_of("session_id")
.map(|s| s.to_string())
.unwrap_or_else( ||
env::var("MCFLY_SESSION_ID")
.unwrap_or_else(|err| panic!(format!("McFly error: Please ensure that MCFLY_SESSION_ID contains a random session ID ({})", err))),
);
.unwrap_or_else(|err| {
if !settings.skip_environment_check
{
panic!(format!(
"McFly error: Please ensure that MCFLY_SESSION_ID contains a random session ID ({})",
err))
}
else {
std::string::String::new()
}
}));
settings.mcfly_history = PathBuf::from(
matches
.value_of("mcfly_history")
.map(|s| s.to_string())
.unwrap_or_else(|| {
env::var("MCFLY_HISTORY").unwrap_or_else(|err| {
panic!(format!(
"McFly error: Please ensure that MCFLY_HISTORY is set ({})",
err
))
if !settings.skip_environment_check {
panic!(format!(
"McFly error: Please ensure that MCFLY_HISTORY is set ({})",
err
))
} else {
std::string::String::new()
}
})
}),
);
Expand Down Expand Up @@ -369,6 +404,22 @@ impl Settings {
);
}

("init", Some(init_matches)) => {
settings.mode = Mode::Init;
match init_matches.value_of("shell").unwrap() {
"bash" => {
settings.init_mode = InitMode::Bash;
}
"zsh" => {
settings.init_mode = InitMode::Zsh;
}
"fish" => {
settings.init_mode = InitMode::Fish;
}
_ => unreachable!(),
}
}

("", None) => println!("No subcommand was used"), // If no subcommand was used it'll match the tuple ("", None)
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable!()
}
Expand Down

0 comments on commit d87500f

Please sign in to comment.