Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/bin/edit/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -233,37 +239,43 @@ fn run() -> apperr::Result<()> {

// Returns true if the application should exit early.
fn handle_args(state: &mut State) -> apperr::Result<bool> {
let scratch = scratch_arena(None);
let mut paths: Vec<PathBuf, &Arena> = 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);
} else if arg == "-v" || arg == "--version" {
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()?;
}

Expand Down