Skip to content

Commit

Permalink
feat: Stop and print LineEditor action
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Jul 25, 2024
1 parent 068c9d4 commit ad7d357
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
52 changes: 51 additions & 1 deletion crates/konfigkoll_core/src/line_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl EditProgram {
match prog_action {
ProgramAction::Done => (),
ProgramAction::Stop => break 'input,
ProgramAction::StopAndPrint => {
print_rest_of_input(&mut output, &mut pattern_space, &mut lines);
break 'input;
}
ProgramAction::ShortCircuit => continue 'input,
}
if self.print_default {
Expand All @@ -114,6 +118,10 @@ impl EditProgram {
ActionResult::Continue => (),
ActionResult::ShortCircuit => break,
ActionResult::Stop => break,
ActionResult::StopAndPrint => {
print_rest_of_input(&mut output, &mut pattern_space, &mut lines);
break;
}
ActionResult::NextLine => {
tracing::error!("NextLine not allowed in EOF selector");
}
Expand Down Expand Up @@ -149,6 +157,7 @@ impl EditProgram {
ActionResult::Continue => (),
ActionResult::ShortCircuit => return ProgramAction::ShortCircuit,
ActionResult::Stop => return ProgramAction::Stop,
ActionResult::StopAndPrint => return ProgramAction::StopAndPrint,
ActionResult::NextLine => {
self.advance_line(pattern_space, output, &mut line, lines, line_number);
}
Expand All @@ -163,6 +172,7 @@ impl EditProgram {
match result {
ProgramAction::Done => (),
ProgramAction::Stop => return ProgramAction::Stop,
ProgramAction::StopAndPrint => return ProgramAction::StopAndPrint,
// TODO: Is this the sensible semantics?
ProgramAction::ShortCircuit => return ProgramAction::ShortCircuit,
}
Expand All @@ -174,10 +184,21 @@ impl EditProgram {
}
}

fn print_rest_of_input(output: &mut String, pattern_space: &mut String, lines: &mut Lines<'_>) {
output.push_str(&*pattern_space);
pattern_space.clear();
output.push('\n');
for line in lines.by_ref() {
output.push_str(line);
output.push('\n');
}
}

#[derive(Debug)]
enum ProgramAction {
Done,
Stop,
StopAndPrint,
ShortCircuit,
}

Expand Down Expand Up @@ -265,8 +286,10 @@ pub enum Action {
Delete,
/// Replace pattern space with next line (will print unless auto-print is disabled)
NextLine,
/// Stop processing the input and program and terminate early
/// Stop processing the input and program and terminate early (do not print rest of file)
Stop,
/// Stop processing the input and program and terminate early (auto-print rest of file)
StopAndPrint,
/// Insert a new line *before* the current line
InsertBefore(CompactString),
/// Insert a new line *after* the current line
Expand Down Expand Up @@ -300,6 +323,7 @@ impl Action {
return ActionResult::ShortCircuit;
}
Action::Stop => return ActionResult::Stop,
Action::StopAndPrint => return ActionResult::StopAndPrint,
Action::InsertBefore(s) => {
let old_pattern_space = std::mem::take(pattern_space);
*pattern_space = s.to_string();
Expand Down Expand Up @@ -345,6 +369,7 @@ impl Debug for Action {
Self::Print => write!(f, "Print"),
Self::Delete => write!(f, "Delete"),
Self::Stop => write!(f, "Stop"),
Self::StopAndPrint => write!(f, "StopAndPrint"),
Self::InsertBefore(arg0) => f.debug_tuple("InsertBefore").field(arg0).finish(),
Self::InsertAfter(arg0) => f.debug_tuple("InsertAfter").field(arg0).finish(),
Self::Replace(arg0) => f.debug_tuple("Replace").field(arg0).finish(),
Expand Down Expand Up @@ -372,6 +397,7 @@ enum ActionResult {
Subprogram(Rc<RefCell<EditProgram>>),
ShortCircuit,
Stop,
StopAndPrint,
}

#[cfg(test)]
Expand Down Expand Up @@ -581,6 +607,30 @@ mod tests {
assert_eq!(output, "foo\nfoo\n");
}

#[test]
fn test_stop_and_print() {
let mut program = EditProgram::new();
program.add(
Selector::Regex(Regex::new("x").unwrap()),
false,
Action::StopAndPrint,
);
let input = "bar\nbaz\nquux\nquack";
let output = program.apply(input);
assert_eq!(output, "bar\nbaz\nquux\nquack\n");

let mut program = EditProgram::new();
program.add(Selector::All, false, Action::Replace("foo".into()));
program.add(
Selector::Regex(Regex::new("x").unwrap()),
false,
Action::StopAndPrint,
);
let input = "bar\nbaz\nquux\nquack";
let output = program.apply(input);
assert_eq!(output, "foo\nfoo\nfoo\nquack\n");
}

#[test]
fn test_print() {
let mut program = EditProgram::new();
Expand Down
6 changes: 5 additions & 1 deletion crates/konfigkoll_script/src/plugins/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ enum Action {
/// Replace pattern space with next line (will print unless auto-print is disabled)
#[rune(constructor)]
NextLine,
/// Stop processing the input and program and terminate early
/// Stop processing the input and program and terminate early (do not print rest of file)
#[rune(constructor)]
Stop,
/// Stop processing the input and program and terminate early (auto-print rest of file)
#[rune(constructor)]
StopAndPrint,
/// Insert a new line *before* the current line
#[rune(constructor)]
InsertBefore(#[rune(get)] String),
Expand Down Expand Up @@ -173,6 +176,7 @@ impl TryFrom<&Action> for konfigkoll_core::line_edit::Action {
Action::Print => Ok(Self::Print),
Action::Delete => Ok(Self::Delete),
Action::Stop => Ok(Self::Stop),
Action::StopAndPrint => Ok(Self::StopAndPrint),
Action::InsertBefore(s) => Ok(Self::InsertBefore(s.into())),
Action::InsertAfter(s) => Ok(Self::InsertAfter(s.into())),
Action::Replace(s) => Ok(Self::Replace(s.into())),
Expand Down

0 comments on commit ad7d357

Please sign in to comment.