Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed Aug 1, 2024
1 parent ed3b4c6 commit e7090e3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = "2.4.10"

[dependencies]
anyhow = "1.0.86"
blake3 = "1.5.3"
clap = { version = "4.5.8", features = ["derive"] }
console = { version = "0.15.8", features = ["windows-console-colors"] }
dialoguer = { version = "0.11.0", features = ["completion", "fuzzy-select"] }
Expand Down
2 changes: 1 addition & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl From<Folder> for Item {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Folder {
pub name: String,
pub files: Vec<Template>,
Expand Down
2 changes: 2 additions & 0 deletions src/cache/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub fn pick_template() -> anyhow::Result<Option<Template>> {
history: Vec::new(),
}));

state.lock().update_hash();

let selected = loop {
terminal.draw(ui::ui(state.clone()))?;
let (should_quit, selected) = handle_events(&state)?;
Expand Down
14 changes: 14 additions & 0 deletions src/cache/pick/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::hash::{self, Hash, Hasher};

use parking_lot::Mutex;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
widgets::ListState,
Expand All @@ -10,11 +13,15 @@ use crate::{

pub type History = Vec<HistoryEntry>;

pub static STATE_HASH: Mutex<u64> = Mutex::new(0);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HistoryEntry {
pub folder: Folder,
pub selection: Option<usize>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct State {
// matching_templates: Vec<(Template, Vec<usize>)>,
pub search_term: String,
Expand All @@ -30,6 +37,13 @@ enum Adjustment {
}

impl State {
pub fn update_hash(&self) {
let mut hasher = hash::DefaultHasher::new();
self.hash(&mut hasher);

*STATE_HASH.lock() = hasher.finish();
}

pub fn handle_key_event(&mut self, key: KeyEvent) -> (bool, Option<Template>) {
if key.kind == KeyEventKind::Press {
match key.code {
Expand Down
6 changes: 4 additions & 2 deletions src/cache/pick/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn ui(state: Rc<Mutex<super::State>>) -> impl Fn(&mut ratatui::Frame<'_>) {
let items = state.lock().current_folder.list_items();

let list = List::new(items.iter().filter_map(|t| {
Some(Line::from({
let spans = {
let mut spans = vec![
t.get_icon().into(),
" ".into(),
Expand All @@ -96,7 +96,9 @@ pub fn ui(state: Rc<Mutex<super::State>>) -> impl Fn(&mut ratatui::Frame<'_>) {
spans.extend(indices_template(t, &indices));

spans
}).add_modifier(Modifier::DIM))
};

Some(Line::from(spans).add_modifier(Modifier::DIM))
}))
.block(
Block::bordered()
Expand Down
4 changes: 2 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use crate::cache::Cache;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Category {
Subfolder(VecDeque<String>),
Root,
Expand All @@ -34,7 +34,7 @@ impl Display for Category {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Template {
name: String,
path: PathBuf,
Expand Down

0 comments on commit e7090e3

Please sign in to comment.