-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Support for fish shell + command `rsvenv install`
- Loading branch information
Showing
16 changed files
with
283 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ Cargo.lock | |
*.pdb | ||
|
||
.idea | ||
.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::fs::{read_to_string, OpenOptions}; | ||
use std::io::Write; | ||
|
||
use crate::errors::CommandExecutionError; | ||
use crate::shell::SupportedShell; | ||
use clap::Parser; | ||
use error_stack::{Report, Result, ResultExt}; | ||
use simplelog::{debug, error, info}; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Command {} | ||
|
||
fn find_occurence(filename: &str, substr: &str) -> Result<bool, CommandExecutionError> { | ||
for line in read_to_string(filename) | ||
.change_context(CommandExecutionError { | ||
command: "install".into(), | ||
}) | ||
.attach_printable("Unable to read config file")? | ||
.lines() | ||
{ | ||
if line.contains(substr) { | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
impl Command { | ||
pub fn execute(&self) -> Result<(), CommandExecutionError> { | ||
let error_context = CommandExecutionError { | ||
command: "install".into(), | ||
}; | ||
let shell = SupportedShell::new() | ||
.change_context(error_context.clone()) | ||
.attach("Unable to detect current shell")?; | ||
|
||
let config_path = shell | ||
.get_config_path() | ||
.change_context(error_context.clone())?; | ||
|
||
debug!("Installing to {config_path}"); | ||
|
||
let init_line = shell.get_init_command(); | ||
|
||
if find_occurence(&config_path, init_line)? { | ||
error!("RSVENV already installed to config"); | ||
return Err(Report::new(CommandExecutionError { | ||
command: "install".into(), | ||
})); | ||
}; | ||
let mut file = OpenOptions::new() | ||
.append(true) | ||
.open(config_path) | ||
.change_context(error_context.clone()) | ||
.attach("Unable to open config file for appending")?; | ||
|
||
file.write_all(init_line.as_bytes()) | ||
.change_context(error_context) | ||
.attach_printable("Error when writing to the file")?; | ||
|
||
info!("Successfully modified shell config. Please restart you session"); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
pub const CONFIG: &str = "~/.config/fish/config.fish"; | ||
|
||
pub const HOOK: &str = r#" | ||
if set -q rsvenv; | ||
set -e rsvenv | ||
end | ||
set -g RS_VENV_PATH (which rsvenv) | ||
function _rs_venv_virtualenv_hook --on-event fish_prompt; | ||
$RS_VENV_PATH hook | source | ||
end | ||
function rsvenv | ||
set eval_commands activate deactivate delete use | ||
if contains $argv[1] $eval_commands | ||
$RS_VENV_PATH $argv | source | ||
else | ||
$RS_VENV_PATH $argv | ||
end | ||
end | ||
"#; | ||
|
||
pub static ACTIVATE_TEMPLATE: &str = r#" | ||
source {activate_path} | ||
set -gx RSVENV_ACTIVATE_PATH {current_directory} | ||
"#; | ||
|
||
pub static DEACTIVATE_TEMPLATE: &str = r#" | ||
set -e RSVENV_DEACTIVATE_PATH | ||
deactivate | ||
{{ if forced }}set -gx RSVENV_DEACTIVATE_PATH $RSVENV_ACTIVATE_PATH{{ endif }} | ||
"#; | ||
|
||
pub static INIT_COMMAND: &str = "status --is-interactive; and source (rsvenv init |psub)"; |
Oops, something went wrong.