Skip to content

Commit

Permalink
Add FullHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Aug 22, 2023
1 parent 70b5d01 commit deb0b20
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 40 deletions.
41 changes: 1 addition & 40 deletions patican/src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
use crate::{
color::{Color, Rgba},
command::{Command, Metadata, PutCommand, RemoveCommand},
history::History,
spatial::{Point, RectangularArea},
};
use std::collections::BTreeMap;

pub trait History {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}

fn append_command(&mut self, command: Command);
fn get_redo_command(&self, index: usize) -> Option<&Command>;
fn get_undo_command(&self, index: usize) -> Option<&Command>;
}

// TODO: PerfectHistory or FullHistory

#[derive(Debug, Default, Clone)]
pub struct NoopHistory {
len: usize,
}

impl NoopHistory {
pub fn new() -> Self {
Self::default()
}
}

impl History for NoopHistory {
fn append_command(&mut self, _command: Command) {}

fn len(&self) -> usize {
self.len
}

fn get_redo_command(&self, _index: usize) -> Option<&Command> {
None
}

fn get_undo_command(&self, _index: usize) -> Option<&Command> {
None
}
}

#[derive(Debug, Clone)]
pub struct Canvas<H> {
cursor: Point,
Expand Down
115 changes: 115 additions & 0 deletions patican/src/history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use std::collections::VecDeque;

use crate::command::Command;

pub trait History {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}

fn append_command(&mut self, command: Command);
fn get_redo_command(&self, index: usize) -> Option<&Command>;
fn get_undo_command(&self, index: usize) -> Option<&Command>;
}

#[derive(Debug, Default, Clone)]
pub struct NullHistory {
len: usize,
}

impl NullHistory {
pub fn new() -> Self {
Self::default()
}
}

impl History for NullHistory {
fn append_command(&mut self, _command: Command) {}

fn len(&self) -> usize {
self.len
}

fn get_redo_command(&self, _index: usize) -> Option<&Command> {
None
}

fn get_undo_command(&self, _index: usize) -> Option<&Command> {
None
}
}

#[derive(Debug, Clone)]
pub struct FullHistory(LimitedHistory);

impl FullHistory {
pub fn new() -> Self {
Self(LimitedHistory::new(usize::MAX))
}
}

impl Default for FullHistory {
fn default() -> Self {
Self::new()
}
}

impl History for FullHistory {
fn append_command(&mut self, command: Command) {
self.0.append_command(command);
}

fn len(&self) -> usize {
self.0.len()
}

fn get_redo_command(&self, index: usize) -> Option<&Command> {
self.0.get_redo_command(index)
}

fn get_undo_command(&self, index: usize) -> Option<&Command> {
self.0.get_undo_command(index)
}
}

#[derive(Debug, Default, Clone)]
pub struct LimitedHistory {
limit: usize,
len: usize,
commands: VecDeque<Command>,
}

impl LimitedHistory {
pub fn new(limit: usize) -> Self {
Self {
limit,
len: 0,
commands: VecDeque::new(),
}
}
}

impl History for LimitedHistory {
fn len(&self) -> usize {
self.len
}

fn append_command(&mut self, command: Command) {
self.len += 1;
self.commands.push_back(command);
if self.commands.len() > self.limit {
self.commands.pop_front();
}
}

fn get_redo_command(&self, index: usize) -> Option<&Command> {
let dropepd = self.len - self.commands.len();
let i = index.checked_sub(dropepd)?;
self.commands.get(i)
}

fn get_undo_command(&self, index: usize) -> Option<&Command> {

Check failure on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Lints (beta)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Check (beta)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Check (nightly)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Test Suite (beta)

unused variable: `index`

Check warning on line 112 in patican/src/history.rs

View workflow job for this annotation

GitHub Actions / Test Suite (nightly)

unused variable: `index`
todo!()
}
}
1 change: 1 addition & 0 deletions patican/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod canvas;
pub mod color;
pub mod command;
pub mod history;
pub mod spatial;

0 comments on commit deb0b20

Please sign in to comment.