Skip to content

Commit

Permalink
ex print command
Browse files Browse the repository at this point in the history
  • Loading branch information
hgwr committed Jun 30, 2024
1 parent 1aa5708 commit bbd7ab9
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 112 deletions.
9 changes: 8 additions & 1 deletion src/command/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@ use std::any::Any;

use crossterm::event::{KeyCode, KeyModifiers};

use crate::{editor::Editor, generic_error::GenericResult};
use crate::{data::LineRange, editor::Editor, generic_error::GenericResult};

pub trait Command {
fn execute(&mut self, editor: &mut Editor) -> GenericResult<()>;

fn is_reusable(&self) -> bool {
true
}

fn is_modeful(&self) -> bool {
false
}

fn is_undoable(&self) -> bool {
false
}

fn undo(&mut self, editor: &mut Editor) -> GenericResult<()> {
let _ = editor;
// do nothing
Ok(())
}

fn redo(&mut self, editor: &mut Editor) -> GenericResult<Option<Box<dyn Command>>> {
let _ = editor;
// do nothing
Ok(None)
}

fn set_text(&mut self, _text: String) {
// do nothing
}

fn as_any(&self) -> &dyn Any;
}

Expand Down
1 change: 1 addition & 0 deletions src/command/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod esc;
pub mod delete;
pub mod undo;
pub mod append;
pub mod print;
22 changes: 22 additions & 0 deletions src/command/commands/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::any::Any;

use crate::command::base::Command;
use crate::data::LineRange;
use crate::editor::Editor;
use crate::generic_error::GenericResult;

pub struct PrintCommand {
pub line_range: LineRange
}

impl Command for PrintCommand {
fn execute(&mut self, editor: &mut Editor) -> GenericResult<()> {
// TODO: Implement PrintCommand
log::info!("PrintCommand execute");
Ok(())
}

fn as_any(&self) -> &dyn Any {
self
}
}
48 changes: 48 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[derive(Debug, PartialEq, Clone)]
pub enum TokenType {
Colon,
Command,
Option,
Number,
Symbol,
Pattern,
AddressPattern,
Replacement,
Filename,
Separator,
EndOfInput,
Illegal,
}

#[derive(Debug, Clone)]
pub struct Token {
pub token_type: TokenType,
pub lexeme: String,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Pattern {
pub pattern: String,
}

#[derive(Debug, PartialEq, Clone)]
pub enum SimpleLineAddressType {
LineNumber(usize),
CurrentLine,
FirstLine,
LastLine,
AllLines,
Pattern(Pattern)
}

#[derive(Debug, PartialEq, Clone)]
pub enum LineAddressType {
Absolute(SimpleLineAddressType),
Relative(SimpleLineAddressType, isize),
}

#[derive(Debug, PartialEq, Clone)]
pub struct LineRange {
pub start: LineAddressType,
pub end: LineAddressType,
}
41 changes: 20 additions & 21 deletions src/ex/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
#[derive(Debug, PartialEq)]
pub enum TokenType {
Colon,
Command,
Option,
Number,
Symbol,
Pattern,
AddressPattern,
Replacement,
Filename,
Separator,
EndOfInput,
Illegal,
}

#[derive(Debug)]
pub struct Token {
pub token_type: TokenType,
pub lexeme: String,
}
use crate::data::TokenType;
use crate::data::Token;

#[derive(Debug, PartialEq)]
enum SubstitutionCommandState {
Expand Down Expand Up @@ -521,4 +502,22 @@ mod tests {
assert_eq!(tokens[2].lexeme, "q");
assert_eq!(tokens[3].token_type, TokenType::EndOfInput);
}

#[test]
fn test_tokenize_print_with_line_addresses() {
let input = ":1,2p";
let tokens = tokenize(input);
assert_eq!(tokens.len(), 6);
assert_eq!(tokens[0].token_type, TokenType::Colon);
assert_eq!(tokens[0].lexeme, ":");
assert_eq!(tokens[1].token_type, TokenType::Number);
assert_eq!(tokens[1].lexeme, "1");
assert_eq!(tokens[2].token_type, TokenType::Separator);
assert_eq!(tokens[2].lexeme, ",");
assert_eq!(tokens[3].token_type, TokenType::Number);
assert_eq!(tokens[3].lexeme, "2");
assert_eq!(tokens[4].token_type, TokenType::Command);
assert_eq!(tokens[4].lexeme, "p");
assert_eq!(tokens[5].token_type, TokenType::EndOfInput);
}
}
Loading

0 comments on commit bbd7ab9

Please sign in to comment.