on master help sign in
Add +N CLI argument to jump to first file's line number (#8521)

* Accept +num flag for opening at line number * Update +N argument feature according to feedback in original PR #5603 * Only override the line number of the first file if +N is specified --------- Co-authored-by: Nachum Barcohen <38861757+nabaco@users.noreply.github.com>

commit07a006d1d5270e81c57bbc0e712614741d3b31a7 authorBjorn Ove Hay Andersen <bjrnove@gmail.com> dateThu 12 Oct 2023, 10:35 (3 years ago) parent1852292
2 files changed+15
helix-term/src/args.rs+14 helix-term/src/main.rs+1
helix-term/src/args.rs+14
@@ -24,6 +24,7 @@ impl Args {
2424 pub fn parse_args() -> Result<Args> {
2525 let mut args = Args::default();
2626 let mut argv = std::env::args().peekable();
27+ let mut line_number = 0;
2728
2829 argv.next(); // skip the program, we don't care about that
2930
@@ -88,6 +89,13 @@ impl Args {
8889 }
8990 }
9091 }
92+ arg if arg.starts_with('+') => {
93+ let arg = &arg[1..];
94+ line_number = match arg.parse::<usize>() {
95+ Ok(n) => n.saturating_sub(1),
96+ _ => anyhow::bail!("bad line number after +"),
97+ };
98+ }
9199 arg => args.files.push(parse_file(arg)),
92100 }
93101 }
@@ -97,6 +105,12 @@ impl Args {
97105 args.files.push(parse_file(&arg));
98106 }
99107
108+ if let Some(file) = args.files.first_mut() {
109+ if line_number != 0 {
110+ file.1.row = line_number;
111+ }
112+ }
113+
100114 Ok(args)
101115 }
102116}
helix-term/src/main.rs+1
@@ -67,6 +67,7 @@ FLAGS:
6767 --vsplit Splits all given files vertically into different windows
6868 --hsplit Splits all given files horizontally into different windows
6969 -w, --working-dir <path> Specify an initial working directory
70+ +N Open the first given file at line number N
7071",
7172 env!("CARGO_PKG_NAME"),
7273 VERSION_AND_GIT_HASH,