Struct broot::shell_install::ShellInstall
source · pub struct ShellInstall {
pub should_quit: bool,
/* private fields */
}
Fields§
§should_quit: bool
Implementations§
source§impl ShellInstall
impl ShellInstall
sourcepub fn new(force_install: bool) -> Self
pub fn new(force_install: bool) -> Self
Examples found in repository?
src/cli/mod.rs (line 74)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
pub fn run() -> Result<Option<Launchable>, ProgramError> {
// parse the launch arguments we got from cli
let args = Args::parse();
let mut must_quit = false;
if let Some(dir) = &args.write_default_conf {
write_default_conf_in(dir)?;
must_quit = true;
}
// read the install related arguments
let install_args = InstallLaunchArgs::from(&args)?;
// execute installation things required by launch args
if let Some(state) = install_args.set_install_state {
write_state(state)?;
must_quit = true;
}
if let Some(shell) = &install_args.print_shell_function {
ShellInstall::print(shell)?;
must_quit = true;
}
if must_quit {
return Ok(None);
}
// read the list of specific config files
let specific_conf: Option<Vec<PathBuf>> = args.conf
.as_ref()
.map(|s| s.split(';').map(PathBuf::from).collect());
// if we don't run on a specific config file, we check the
// configuration
if specific_conf.is_none() && install_args.install != Some(false) {
let mut shell_install = ShellInstall::new(install_args.install == Some(true));
shell_install.check()?;
if shell_install.should_quit {
return Ok(None);
}
}
// read the configuration file(s): either the standard one
// or the ones required by the launch args
let mut config = match &specific_conf {
Some(conf_paths) => {
let mut conf = Conf::default();
for path in conf_paths {
conf.read_file(path.to_path_buf())?;
}
conf
}
_ => time!(Conf::from_default_location())?,
};
debug!("config: {:#?}", &config);
// verb store is completed from the config file(s)
let verb_store = VerbStore::new(&mut config)?;
let mut context = AppContext::from(args, verb_store, &config)?;
#[cfg(unix)]
if let Some(server_name) = &context.launch_args.send {
use crate::{
command::Sequence,
net::{Client, Message},
};
let client = Client::new(server_name);
if let Some(seq) = &context.launch_args.cmd {
let message = Message::Sequence(Sequence::new_local(seq.to_string()));
client.send(&message)?;
} else if !context.launch_args.get_root {
let message = Message::Command(
format!(":focus {}", context.initial_root.to_string_lossy())
);
client.send(&message)?;
};
if context.launch_args.get_root {
client.send(&Message::GetRoot)?;
}
return Ok(None);
}
let mut w = display::writer();
let app = App::new(&context)?;
w.queue(EnterAlternateScreen)?;
w.queue(cursor::Hide)?;
if context.capture_mouse {
w.queue(EnableMouseCapture)?;
}
let r = app.run(&mut w, &mut context, &config);
if context.capture_mouse {
w.queue(DisableMouseCapture)?;
}
w.queue(cursor::Show)?;
w.queue(LeaveAlternateScreen)?;
w.flush()?;
r
}
sourcepub fn print(shell: &str) -> Result<(), ProgramError>
pub fn print(shell: &str) -> Result<(), ProgramError>
write on stdout the script building the function for the given shell
Examples found in repository?
src/cli/mod.rs (line 59)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
pub fn run() -> Result<Option<Launchable>, ProgramError> {
// parse the launch arguments we got from cli
let args = Args::parse();
let mut must_quit = false;
if let Some(dir) = &args.write_default_conf {
write_default_conf_in(dir)?;
must_quit = true;
}
// read the install related arguments
let install_args = InstallLaunchArgs::from(&args)?;
// execute installation things required by launch args
if let Some(state) = install_args.set_install_state {
write_state(state)?;
must_quit = true;
}
if let Some(shell) = &install_args.print_shell_function {
ShellInstall::print(shell)?;
must_quit = true;
}
if must_quit {
return Ok(None);
}
// read the list of specific config files
let specific_conf: Option<Vec<PathBuf>> = args.conf
.as_ref()
.map(|s| s.split(';').map(PathBuf::from).collect());
// if we don't run on a specific config file, we check the
// configuration
if specific_conf.is_none() && install_args.install != Some(false) {
let mut shell_install = ShellInstall::new(install_args.install == Some(true));
shell_install.check()?;
if shell_install.should_quit {
return Ok(None);
}
}
// read the configuration file(s): either the standard one
// or the ones required by the launch args
let mut config = match &specific_conf {
Some(conf_paths) => {
let mut conf = Conf::default();
for path in conf_paths {
conf.read_file(path.to_path_buf())?;
}
conf
}
_ => time!(Conf::from_default_location())?,
};
debug!("config: {:#?}", &config);
// verb store is completed from the config file(s)
let verb_store = VerbStore::new(&mut config)?;
let mut context = AppContext::from(args, verb_store, &config)?;
#[cfg(unix)]
if let Some(server_name) = &context.launch_args.send {
use crate::{
command::Sequence,
net::{Client, Message},
};
let client = Client::new(server_name);
if let Some(seq) = &context.launch_args.cmd {
let message = Message::Sequence(Sequence::new_local(seq.to_string()));
client.send(&message)?;
} else if !context.launch_args.get_root {
let message = Message::Command(
format!(":focus {}", context.initial_root.to_string_lossy())
);
client.send(&message)?;
};
if context.launch_args.get_root {
client.send(&Message::GetRoot)?;
}
return Ok(None);
}
let mut w = display::writer();
let app = App::new(&context)?;
w.queue(EnterAlternateScreen)?;
w.queue(cursor::Hide)?;
if context.capture_mouse {
w.queue(EnableMouseCapture)?;
}
let r = app.run(&mut w, &mut context, &config);
if context.capture_mouse {
w.queue(DisableMouseCapture)?;
}
w.queue(cursor::Show)?;
w.queue(LeaveAlternateScreen)?;
w.flush()?;
r
}
sourcepub fn check(&mut self) -> Result<(), ProgramError>
pub fn check(&mut self) -> Result<(), ProgramError>
check whether the shell function is installed, install it if it wasn’t refused before or if broot is launched with –install.
Examples found in repository?
src/cli/mod.rs (line 75)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
pub fn run() -> Result<Option<Launchable>, ProgramError> {
// parse the launch arguments we got from cli
let args = Args::parse();
let mut must_quit = false;
if let Some(dir) = &args.write_default_conf {
write_default_conf_in(dir)?;
must_quit = true;
}
// read the install related arguments
let install_args = InstallLaunchArgs::from(&args)?;
// execute installation things required by launch args
if let Some(state) = install_args.set_install_state {
write_state(state)?;
must_quit = true;
}
if let Some(shell) = &install_args.print_shell_function {
ShellInstall::print(shell)?;
must_quit = true;
}
if must_quit {
return Ok(None);
}
// read the list of specific config files
let specific_conf: Option<Vec<PathBuf>> = args.conf
.as_ref()
.map(|s| s.split(';').map(PathBuf::from).collect());
// if we don't run on a specific config file, we check the
// configuration
if specific_conf.is_none() && install_args.install != Some(false) {
let mut shell_install = ShellInstall::new(install_args.install == Some(true));
shell_install.check()?;
if shell_install.should_quit {
return Ok(None);
}
}
// read the configuration file(s): either the standard one
// or the ones required by the launch args
let mut config = match &specific_conf {
Some(conf_paths) => {
let mut conf = Conf::default();
for path in conf_paths {
conf.read_file(path.to_path_buf())?;
}
conf
}
_ => time!(Conf::from_default_location())?,
};
debug!("config: {:#?}", &config);
// verb store is completed from the config file(s)
let verb_store = VerbStore::new(&mut config)?;
let mut context = AppContext::from(args, verb_store, &config)?;
#[cfg(unix)]
if let Some(server_name) = &context.launch_args.send {
use crate::{
command::Sequence,
net::{Client, Message},
};
let client = Client::new(server_name);
if let Some(seq) = &context.launch_args.cmd {
let message = Message::Sequence(Sequence::new_local(seq.to_string()));
client.send(&message)?;
} else if !context.launch_args.get_root {
let message = Message::Command(
format!(":focus {}", context.initial_root.to_string_lossy())
);
client.send(&message)?;
};
if context.launch_args.get_root {
client.send(&Message::GetRoot)?;
}
return Ok(None);
}
let mut w = display::writer();
let app = App::new(&context)?;
w.queue(EnterAlternateScreen)?;
w.queue(cursor::Hide)?;
if context.capture_mouse {
w.queue(EnableMouseCapture)?;
}
let r = app.run(&mut w, &mut context, &config);
if context.capture_mouse {
w.queue(DisableMouseCapture)?;
}
w.queue(cursor::Show)?;
w.queue(LeaveAlternateScreen)?;
w.flush()?;
r
}
sourcepub fn remove(&self, path: &Path) -> Result<()>
pub fn remove(&self, path: &Path) -> Result<()>
Examples found in repository?
src/shell_install/mod.rs (line 135)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
pub fn check(&mut self) -> Result<(), ProgramError> {
let installed_path = get_installed_path();
if self.force_install {
self.skin.print_text("You requested a clean (re)install.");
self.remove(&get_refused_path())?;
self.remove(&installed_path)?;
} else {
if installed_path.exists() {
debug!("Shell script already installed. Doing nothing.");
return Ok(());
}
debug!("No 'installed' : we ask if we can install");
if !self.can_do()? {
debug!("User refuses the installation. Doing nothing.");
return Ok(());
}
// even if the installation isn't really complete (for example
// when no bash file was found), we don't want to ask the user
// again, we'll assume it's done
write_state(ShellInstallState::Installed)?;
}
debug!("Starting install");
bash::install(self)?;
fish::install(self)?;
self.should_quit = true;
if self.done {
self.skin.print_text(MD_INSTALL_DONE);
}
Ok(())
}
pub fn remove(&self, path: &Path) -> io::Result<()> {
// path.exists() doesn't work when the file is a link (it checks whether
// the link destination exists instead of checking the link exists
// so we first check whether the link exists
if fs::read_link(path).is_ok() || path.exists() {
mad_print_inline!(self.skin, "Removing `$0`.\n", path.to_string_lossy());
fs::remove_file(path)?;
}
Ok(())
}
/// check whether we're allowed to install.
fn can_do(&mut self) -> Result<bool, ProgramError> {
if let Some(authorization) = self.authorization {
return Ok(authorization);
}
let refused_path = get_refused_path();
if refused_path.exists() {
debug!("User already refused the installation");
return Ok(false);
}
self.skin.print_text(MD_INSTALL_REQUEST);
let proceed = cli::ask_authorization()?;
debug!("proceed: {:?}", proceed);
self.authorization = Some(proceed);
if !proceed {
write_state(ShellInstallState::Refused)?;
self.skin.print_text(MD_INSTALL_CANCELLED);
}
Ok(proceed)
}
/// write the script at the given path
fn write_script(&self, script_path: &Path, content: &str) -> Result<(), ProgramError> {
self.remove(script_path)?;
info!("Writing `br` shell function in `{:?}`", &script_path);
mad_print_inline!(
&self.skin,
"Writing *br* shell function in `$0`.\n",
script_path.to_string_lossy(),
);
fs::create_dir_all(script_path.parent().unwrap())?;
fs::write(script_path, content)?;
Ok(())
}
/// create a link
fn create_link(&self, link_path: &Path, script_path: &Path) -> Result<(), ProgramError> {
info!("Creating link from {:?} to {:?}", &link_path, &script_path);
self.remove(link_path)?;
let link_path_str = link_path.to_string_lossy();
let script_path_str = script_path.to_string_lossy();
mad_print_inline!(
&self.skin,
"Creating link from `$0` to `$1`.\n",
&link_path_str,
&script_path_str,
);
fs::create_dir_all(link_path.parent().unwrap())?;
#[cfg(unix)]
os::unix::fs::symlink(script_path, link_path)?;
#[cfg(windows)]
os::windows::fs::symlink_file(&script_path, &link_path)?;
Ok(())
}