-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
pub struct Backstack<T: Eq> { | ||
cursor: usize, | ||
stack: Vec<T>, | ||
} | ||
|
||
impl<T: Eq> Backstack<T> { | ||
pub fn new(item: T) -> Self { Self { cursor: 0, stack: vec![item] } } | ||
|
||
pub fn push(&mut self, item: T) { | ||
if self.stack[self.cursor] == item { | ||
return; | ||
} | ||
|
||
self.cursor += 1; | ||
if self.cursor == self.stack.len() { | ||
self.stack.push(item); | ||
} else { | ||
self.stack[self.cursor] = item; | ||
self.stack.truncate(self.cursor + 1); | ||
} | ||
|
||
// Only keep 30 items before the cursor, the cleanup threshold is 60 | ||
if self.stack.len() > 60 { | ||
let start = self.cursor.saturating_sub(30); | ||
self.stack.drain(..start); | ||
self.cursor -= start; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[inline] | ||
pub fn current(&self) -> &T { &self.stack[self.cursor] } | ||
|
||
pub fn shift_backward(&mut self) -> Option<&T> { | ||
if self.cursor > 0 { | ||
self.cursor -= 1; | ||
Some(&self.stack[self.cursor]) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn shift_forward(&mut self) -> Option<&T> { | ||
if self.cursor + 1 == self.stack.len() { | ||
None | ||
} else { | ||
self.cursor += 1; | ||
Some(&self.stack[self.cursor]) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_backstack() { | ||
let mut backstack = Backstack::<u32>::new(1); | ||
assert_eq!(backstack.current(), &1); | ||
|
||
backstack.push(2); | ||
backstack.push(3); | ||
assert_eq!(backstack.current(), &3); | ||
|
||
assert_eq!(backstack.shift_backward(), Some(&2)); | ||
assert_eq!(backstack.shift_backward(), Some(&1)); | ||
assert_eq!(backstack.shift_backward(), None); | ||
assert_eq!(backstack.shift_backward(), None); | ||
assert_eq!(backstack.current(), &1); | ||
assert_eq!(backstack.shift_forward(), Some(&2)); | ||
assert_eq!(backstack.shift_forward(), Some(&3)); | ||
assert_eq!(backstack.shift_forward(), None); | ||
|
||
backstack.shift_backward(); | ||
backstack.push(4); | ||
|
||
assert_eq!(backstack.current(), &4); | ||
assert_eq!(backstack.shift_forward(), None); | ||
assert_eq!(backstack.shift_backward(), Some(&2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT"],"version":"0.2","flagWords":[]} | ||
{"version":"0.2","words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","backstack"],"flagWords":[],"language":"en"} |