use std::path::PathBuf;
pub(crate) fn truncate_str(s: &str, max: usize) -> &str {
if s.len() <= max {
s
} else {
let mut end = max;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
}
pub(crate) fn home_dir() -> Option<PathBuf> {
std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.ok()
.map(PathBuf::from)
}
#[cfg(windows)]
pub(crate) const CREATE_NO_WINDOW: u32 = 0x0800_0000;
#[cfg(windows)]
pub(crate) fn hide_console_window(command: &mut std::process::Command) {
use std::os::windows::process::CommandExt;
command.creation_flags(CREATE_NO_WINDOW);
}
#[cfg(not(windows))]
pub(crate) fn hide_console_window(_command: &mut std::process::Command) {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hide_console_window_does_not_alter_program_or_args() {
let mut command = std::process::Command::new("cmd");
command.arg("/C").arg("exit 0");
hide_console_window(&mut command);
assert_eq!(command.get_program(), std::ffi::OsStr::new("cmd"));
assert_eq!(
command.get_args().collect::<Vec<_>>(),
[std::ffi::OsStr::new("/C"), std::ffi::OsStr::new("exit 0")]
);
}
#[cfg(windows)]
#[test]
fn create_no_window_constant_matches_the_documented_windows_value() {
assert_eq!(CREATE_NO_WINDOW, 0x0800_0000);
}
}