Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/args.rs')
| -rw-r--r-- | helix-view/src/args.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/helix-view/src/args.rs b/helix-view/src/args.rs new file mode 100644 index 00000000..4a69e12d --- /dev/null +++ b/helix-view/src/args.rs @@ -0,0 +1,36 @@ +use helix_core::Position; +use std::path::{Path, PathBuf}; + +/// Parse arg into [`PathBuf`] and position. +pub fn parse_file(s: &str) -> (PathBuf, Position) { + let def = || (PathBuf::from(s), Position::default()); + if Path::new(s).exists() { + return def(); + } + split_path_row_col(s) + .or_else(|| split_path_row(s)) + .unwrap_or_else(def) +} + +/// Split file.rs:10:2 into [`PathBuf`], row and col. +/// +/// Does not validate if file.rs is a file or directory. +fn split_path_row_col(s: &str) -> Option<(PathBuf, Position)> { + let mut s = s.rsplitn(3, ':'); + let col: usize = s.next()?.parse().ok()?; + let row: usize = s.next()?.parse().ok()?; + let path = s.next()?.into(); + let pos = Position::new(row.saturating_sub(1), col.saturating_sub(1)); + Some((path, pos)) +} + +/// Split file.rs:10 into [`PathBuf`] and row. +/// +/// Does not validate if file.rs is a file or directory. +fn split_path_row(s: &str) -> Option<(PathBuf, Position)> { + let (path, row) = s.rsplit_once(':')?; + let row: usize = row.parse().ok()?; + let path = path.into(); + let pos = Position::new(row.saturating_sub(1), 0); + Some((path, pos)) +} |