Skip to content

Commit

Permalink
refactor(lex): Experiment with an alt 'previous'
Browse files Browse the repository at this point in the history
Since we'll need `skip`, it made me wonder how to name `skip` and
`previous` to fit together, so I decided to play with `seek`.  Its
probably over kill but wondering if its better.
epage committed Apr 15, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 3ebf61e commit f66d8ab
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/parse/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ffi::OsStr;
use std::ffi::OsString;

pub use std::io::SeekFrom;

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub(crate) struct RawArgs {
items: Vec<OsString>,
@@ -27,8 +29,14 @@ impl RawArgs {
remaining
}

pub fn previous(&self, cursor: &mut ArgCursor) {
cursor.cursor = cursor.cursor.saturating_sub(1);
pub fn seek(&self, cursor: &mut ArgCursor, pos: SeekFrom) {
let pos = match pos {
SeekFrom::Start(pos) => pos,
SeekFrom::End(pos) => (self.items.len() as i64).saturating_add(pos).max(0) as u64,
SeekFrom::Current(pos) => (cursor.cursor as i64).saturating_add(pos).max(0) as u64,
};
let pos = (pos as usize).min(self.items.len());
cursor.cursor = pos;
}

/// Inject arguments before the [`RawArgs::next`]
2 changes: 1 addition & 1 deletion src/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -304,7 +304,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
keep_state = self
.flag_subcmd_at
.map(|at| {
raw_args.previous(&mut args_cursor);
raw_args.seek(&mut args_cursor, lexer::SeekFrom::Current(-1));
// Since we are now saving the current state, the number of flags to skip during state recovery should
// be the current index (`cur_idx`) minus ONE UNIT TO THE LEFT of the starting position.
self.flag_subcmd_skip = self.cur_idx.get() - at + 1;

0 comments on commit f66d8ab

Please sign in to comment.