use {
super::ShellInstall,
crate::{
conf,
errors::*,
},
directories::{
BaseDirs,
ProjectDirs,
},
std::path::PathBuf,
};
const NAME: &str = "fish";
const SCRIPT_FILENAME: &str = "br.fish";
const FISH_FUNC: &str = r"
# This script was automatically generated by the broot program
# More information can be found in https://github.com/Canop/broot
# This function starts broot and executes the command
# it produces, if any.
# It's needed because some shell commands, like `cd`,
# have no useful effect if executed in a subshell.
function br --wraps=broot
set -l cmd_file (mktemp)
if broot --outcmd $cmd_file $argv
source $cmd_file
rm -f $cmd_file
else
set -l code $status
rm -f $cmd_file
return $code
end
end
";
pub fn get_script() -> &'static str {
FISH_FUNC
}
fn get_fish_dir() -> PathBuf {
if let Some(base_dirs) = BaseDirs::new() {
let fish_dir = base_dirs.home_dir().join(".config/fish");
if fish_dir.exists() {
return fish_dir;
}
}
ProjectDirs::from("fish", "fish", "fish") .expect("Unable to find configuration directories")
.config_dir()
.to_path_buf()
}
fn get_fish_functions_dir() -> PathBuf {
get_fish_dir().join("functions")
}
fn get_link_path() -> PathBuf {
get_fish_functions_dir().join("br.fish")
}
fn get_script_path() -> PathBuf {
conf::app_dirs()
.data_dir()
.join("launcher")
.join(NAME)
.join(SCRIPT_FILENAME)
}
pub fn install(si: &mut ShellInstall) -> Result<(), ShellInstallError> {
let fish_dir = get_fish_dir();
if !fish_dir.exists() {
debug!("no fish config directory. Assuming fish isn't used.");
return Ok(());
}
info!("fish seems to be installed");
let script_path = get_script_path();
si.write_script(&script_path, FISH_FUNC)?;
let link_path = get_link_path();
si.create_link(&link_path, &script_path)?;
si.done = true;
Ok(())
}