diff --git a/src/bin/edit/main.rs b/src/bin/edit/main.rs index 2c840045ebd3..bac25e04ad99 100644 --- a/src/bin/edit/main.rs +++ b/src/bin/edit/main.rs @@ -1,7 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -#![feature(let_chains, linked_list_cursors, os_string_truncate, string_from_utf8_lossy_owned)] +#![feature( + allocator_api, + let_chains, + linked_list_cursors, + os_string_truncate, + string_from_utf8_lossy_owned +)] mod documents; mod draw_editor; @@ -14,14 +20,14 @@ mod state; use std::borrow::Cow; #[cfg(feature = "debug-latency")] use std::fmt::Write; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::{env, process}; use draw_editor::*; use draw_filepicker::*; use draw_menubar::*; use draw_statusbar::*; -use edit::arena::{self, ArenaString, scratch_arena}; +use edit::arena::{self, Arena, ArenaString, scratch_arena}; use edit::framebuffer::{self, IndexedColor}; use edit::helpers::{KIBI, MEBI, MetricFormatter, Rect, Size}; use edit::input::{self, kbmod, vk}; @@ -233,11 +239,12 @@ fn run() -> apperr::Result<()> { // Returns true if the application should exit early. fn handle_args(state: &mut State) -> apperr::Result { + let scratch = scratch_arena(None); + let mut paths: Vec = Vec::new_in(&*scratch); let mut cwd = env::current_dir()?; - let mut path = None; // The best CLI argument parser in the world. - if let Some(arg) = env::args_os().nth(1) { + for arg in env::args_os().skip(1) { if arg == "-h" || arg == "--help" || (cfg!(windows) && arg == "/?") { print_help(); return Ok(true); @@ -245,25 +252,30 @@ fn handle_args(state: &mut State) -> apperr::Result { print_version(); return Ok(true); } else if arg == "-" { - // We'll check for a redirected stdin no matter what, so we can just ignore "-". - } else { - let p = cwd.join(Path::new(&arg)); - let p = path::normalize(&p); - if let Some(parent) = p.parent() { - cwd = parent.to_path_buf(); - } - path = Some(p); + paths.clear(); + break; + } + let p = cwd.join(Path::new(&arg)); + let p = path::normalize(&p); + if !p.is_dir() { + paths.push(p); } } + for p in &paths { + state.documents.add_file_path(p)?; + } + if let Some(parent) = paths.first().and_then(|p| p.parent()) { + cwd = parent.to_path_buf(); + } + if let Some(mut file) = sys::open_stdin_if_redirected() { let doc = state.documents.add_untitled()?; let mut tb = doc.buffer.borrow_mut(); tb.read_file(&mut file, None)?; tb.mark_as_dirty(); - } else if let Some(path) = path { - state.documents.add_file_path(&path)?; - } else { + } else if paths.is_empty() { + // No files were passed, and stdin is not redirected. state.documents.add_untitled()?; }