diff --git a/src/cmd_line.rs b/src/cmd_line.rs index 683b63c..c7bf7fd 100644 --- a/src/cmd_line.rs +++ b/src/cmd_line.rs @@ -10,7 +10,7 @@ pub struct CmdLineSettings { pub neovim_bin: Option, pub files_to_open: Vec, - pub disowned: bool, + pub nofork: bool, pub geometry: WindowGeometry, pub wsl: bool, pub remote_tcp: Option, @@ -29,7 +29,7 @@ impl Default for CmdLineSettings { log_to_file: false, neovim_args: vec![], files_to_open: vec![], - disowned: false, + nofork: false, geometry: DEFAULT_WINDOW_GEOMETRY, wsl: false, remote_tcp: None, @@ -51,7 +51,7 @@ pub fn handle_command_line_arguments() -> Result<(), String> { Arg::with_name("verbosity") .short("v") .multiple(true) - .help("Set the level of verbosity"), + .help("Increase verbosity level (repeatable up to 4 times; implies --nofork)"), ) .arg( Arg::with_name("log_to_file") @@ -59,9 +59,9 @@ pub fn handle_command_line_arguments() -> Result<(), String> { .help("Log to a file"), ) .arg( - Arg::with_name("disowned") - .long("disowned") - .help("Disown the process. (only on macos)"), + Arg::with_name("nofork") + .long("nofork") + .help("Do not detach process from terminal"), ) .arg( Arg::with_name("maximized") @@ -101,7 +101,7 @@ pub fn handle_command_line_arguments() -> Result<(), String> { Arg::with_name("files") .multiple(true) .takes_value(true) - .help("Specify the Geometry of the window"), + .help("Files to open"), ) .arg( Arg::with_name("neovim_args") @@ -146,7 +146,7 @@ pub fn handle_command_line_arguments() -> Result<(), String> { || std::env::var("NeovideMultiGrid").is_ok() || matches.is_present("multi_grid"), remote_tcp: matches.value_of("remote_tcp").map(|i| i.to_owned()), - disowned: matches.is_present("disowned"), + nofork: matches.is_present("nofork") || matches.is_present("verbosity"), wsl: matches.is_present("wsl"), frameless: matches.is_present("frameless") || std::env::var("NEOVIDE_FRAMELESS").is_ok(), geometry: parse_window_geometry(matches.value_of("geometry").map(|i| i.to_owned()))?, diff --git a/src/main.rs b/src/main.rs index ce29ef8..5f826ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,11 +35,9 @@ use std::sync::{atomic::AtomicBool, mpsc::channel, Arc}; use crossfire::mpsc::unbounded_future; use bridge::start_bridge; -#[cfg(not(test))] use cmd_line::CmdLineSettings; use editor::start_editor; use renderer::{cursor_renderer::CursorSettings, RendererSettings}; -#[cfg(not(test))] use settings::SETTINGS; use window::{create_window, WindowSettings}; @@ -124,6 +122,8 @@ fn main() { #[cfg(not(test))] init_logger(); + maybe_disown(); + #[cfg(target_os = "windows")] windows_fix_dpi(); @@ -175,29 +175,46 @@ fn main() { #[cfg(not(test))] pub fn init_logger() { - let verbosity = match SETTINGS.get::().verbosity { + let settings = SETTINGS.get::(); + + let verbosity = match settings.verbosity { 0 => "warn", 1 => "info", 2 => "debug", _ => "trace", }; - let log_to_file = SETTINGS.get::().log_to_file; - - if log_to_file { - Logger::with_env_or_str("neovide") + let logger = match settings.log_to_file { + true => Logger::with_env_or_str("neovide") .duplicate_to_stderr(Duplicate::Error) .log_to_file() .rotate( Criterion::Size(10_000_000), Naming::Timestamps, Cleanup::KeepLogFiles(1), - ) - .start() - .expect("Could not start logger"); + ), + false => Logger::with_env_or_str(format!("neovide = {}", verbosity)), + }; + logger.start().expect("Could not start logger"); +} + +fn maybe_disown() { + use std::{env, process}; + + let settings = SETTINGS.get::(); + + if cfg!(debug_assertions) || settings.nofork { + return; + } + + if let Ok(current_exe) = env::current_exe() { + assert!(process::Command::new(current_exe) + .args(env::args().skip(1)) + .arg("--nofork") + .spawn() + .is_ok()); + process::exit(0); } else { - Logger::with_env_or_str(format!("neovide = {}", verbosity)) - .start() - .expect("Could not start logger"); + eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning..."); } } @@ -212,22 +229,8 @@ fn windows_fix_dpi() { #[cfg(target_os = "macos")] fn handle_macos() { - // incase of app bundle, we can just pass --disowned option straight away to bypass this check - #[cfg(not(debug_assertions))] - if !SETTINGS.get::().disowned { - if let Ok(curr_exe) = std::env::current_exe() { - assert!(std::process::Command::new(curr_exe) - .args(std::env::args().skip(1)) - .arg("--disowned") - .spawn() - .is_ok()); - std::process::exit(0); - } else { - eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning..."); - } - } - use std::env; + if env::var_os("TERM").is_none() { let mut profile_path = dirs::home_dir().unwrap(); profile_path.push(".profile");