Skip to content

Commit

Permalink
Use sift-state in sift-tui
Browse files Browse the repository at this point in the history
  • Loading branch information
matta committed Aug 20, 2024
1 parent 0d06521 commit dad768b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 262 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

18 changes: 9 additions & 9 deletions sift-state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl State {
state
}

pub(crate) fn index_of_id(&mut self, id: Option<TaskId>) -> Option<usize> {
pub fn index_of_id(&mut self, id: Option<TaskId>) -> Option<usize> {
self.list_tasks_for_display()
.into_iter()
.enumerate()
Expand Down Expand Up @@ -81,7 +81,7 @@ impl State {
}
}

pub(crate) fn snooze(&mut self) {
pub fn snooze(&mut self) {
if let Some(id) = self.selected {
self.store
.with_transaction(|txn| {
Expand Down Expand Up @@ -146,15 +146,15 @@ impl State {
last
}

pub(crate) fn next(&mut self) {
pub fn next(&mut self) {
self.selected = self.next_id();
}

pub(crate) fn previous(&mut self) {
pub fn previous(&mut self) {
self.selected = self.previous_id();
}

pub(crate) fn move_up(&mut self) {
pub fn move_up(&mut self) {
if let Some(selected) = self.selected {
let ids = self.task_ids_for_move();
for (prev_prev_id, _, id) in ids.iter().circular_tuple_windows() {
Expand Down Expand Up @@ -185,7 +185,7 @@ impl State {
.collect()
}

pub(crate) fn move_down(&mut self) {
pub fn move_down(&mut self) {
if let Some(selected) = self.selected {
let ids = self.task_ids_for_move();
for (id, successor_id) in ids.iter().circular_tuple_windows() {
Expand All @@ -199,7 +199,7 @@ impl State {
}
}

pub(crate) fn delete_selected(&mut self) {
pub fn delete_selected(&mut self) {
let mut deletions = Vec::new();
let mut new_selected = None;
let mut saw_selected = false;
Expand Down Expand Up @@ -228,11 +228,11 @@ impl State {
.expect("TODO: handle errors here");
}

pub(crate) fn undo(&mut self) {
pub fn undo(&mut self) {
let _ignored = self.store.undo();
}

pub(crate) fn redo(&mut self) {
pub fn redo(&mut self) {
let _ignored = self.store.redo();
}
}
3 changes: 1 addition & 2 deletions sift-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ edition = "2021"
[dependencies]
sift-core = { path = "../sift-core" }
sift-persist = { path = "../sift-persist" }
sift-state = { path = "../sift-state" }
anyhow = "1.0.86"
chrono = "0.4.38"
cli-log = "2.0.0"
crokey = "1.1.0"
itertools = "0.13.0"
ratatui = "0.28.0"
serde = "1.0.206"
thiserror = "1.0.63"
Expand Down
7 changes: 3 additions & 4 deletions sift-tui/src/screen/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use sift_persist::{Store, TaskId, Transaction};
use tui_prompts::{State as _, TextPrompt};

use crate::screen;
use crate::ui_state::CommonState;

pub(crate) struct State {
id: TaskId,
Expand All @@ -23,7 +22,7 @@ impl State {

fn do_handle_key_event(
&mut self,
context: &mut CommonState,
context: &mut sift_state::State,
key_combination: crokey::KeyCombination,
) -> Option<Box<dyn screen::Screen>> {
let mut text_state = self.text.borrow_mut();
Expand Down Expand Up @@ -58,7 +57,7 @@ fn set_title(txn: &mut dyn Transaction, id: &TaskId, title: &str) -> Result<(),
impl screen::Screen for State {
fn handle_key_event(
mut self: Box<Self>,
context: &mut CommonState,
context: &mut sift_state::State,
key_combination: crokey::KeyCombination,
) -> Box<dyn screen::Screen> {
if let Some(screen) = self.do_handle_key_event(context, key_combination) {
Expand All @@ -68,7 +67,7 @@ impl screen::Screen for State {
}
}

fn render(&self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, _conext: &mut sift_state::State, frame: &mut ratatui::Frame) {
let prompt = TextPrompt::new(Cow::Borrowed("edit"));
frame.render_stateful_widget(prompt, frame.area(), &mut self.text.borrow_mut());
let (x, y) = self.text.borrow().cursor();
Expand Down
11 changes: 5 additions & 6 deletions sift-tui/src/screen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ratatui::widgets::{Block, Borders, List, ListItem, ListState, StatefulWidget
use sift_persist::{Store, Task};

use crate::screen::Screen;
use crate::ui_state::CommonState;
use crate::{keys, screen};

fn render_task(s: &Task) -> ListItem<'_> {
Expand All @@ -24,7 +23,7 @@ impl State {
}
}

fn add(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
fn add(common_state: &mut sift_state::State) -> Option<Box<dyn Screen>> {
// FIXME: make generating new tasks less cumbersome
// FIXME: handle error
let task = Task::new(Task::new_id(), String::new(), None, None, None);
Expand All @@ -36,7 +35,7 @@ fn add(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
edit(common_state)
}

fn edit(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
fn edit(common_state: &mut sift_state::State) -> Option<Box<dyn Screen>> {
if let Some((id, text)) = {
if let Some(id) = common_state.selected {
let title = common_state.store.get_task(&id).unwrap().title().into();
Expand All @@ -56,7 +55,7 @@ fn edit(common_state: &mut CommonState) -> Option<Box<dyn Screen>> {
}

fn do_handle_key_event(
common_state: &mut CommonState,
common_state: &mut sift_state::State,
key_combination: crokey::KeyCombination,
) -> Option<Box<dyn Screen>> {
let bindings = crate::keys::default_bindings();
Expand Down Expand Up @@ -105,7 +104,7 @@ fn do_handle_key_event(
impl screen::Screen for State {
fn handle_key_event(
self: Box<Self>,
common_state: &mut CommonState,
common_state: &mut sift_state::State,
key_combination: crokey::KeyCombination,
) -> Box<dyn Screen> {
if let Some(screen) = do_handle_key_event(common_state, key_combination) {
Expand All @@ -115,7 +114,7 @@ impl screen::Screen for State {
}
}

fn render(&self, common_state: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, common_state: &mut sift_state::State, frame: &mut ratatui::Frame) {
// Set the list widet's selected state based on the list state.
let state: &mut ListState = &mut self.list.borrow_mut();
state.select(common_state.index_of_id(common_state.selected));
Expand Down
8 changes: 3 additions & 5 deletions sift-tui/src/screen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::ui_state::CommonState;

pub mod edit;
pub mod main;
pub mod quit;
Expand All @@ -9,17 +7,17 @@ pub trait Screen {
#[must_use]
fn handle_key_event(
self: Box<Self>,
context: &mut CommonState,
context: &mut sift_state::State,
key_combination: crokey::KeyCombination,
) -> Box<dyn Screen>;

fn render(&self, conext: &mut CommonState, frame: &mut ratatui::Frame);
fn render(&self, conext: &mut sift_state::State, frame: &mut ratatui::Frame);

// FIXME: replace this with a back channel to the event queue logic?
// ...at which point should handle_key_event return a Box<dyn Screen>
// ...or should it return an enum with a NewScreen variant and another
// ...for the quit case?
fn should_quit(&self, context: &mut CommonState) -> bool {
fn should_quit(&self, context: &mut sift_state::State) -> bool {
_ = context;
false
}
Expand Down
7 changes: 3 additions & 4 deletions sift-tui/src/screen/quit.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use ratatui::text::Line;

use super::Screen;
use crate::ui_state::CommonState;

pub(crate) struct State {}

impl Screen for State {
fn handle_key_event(
self: Box<Self>,
_context: &mut CommonState,
_context: &mut sift_state::State,
_key_combination: crokey::KeyCombination,
) -> Box<dyn Screen> {
self
}

fn render(&self, _conext: &mut CommonState, frame: &mut ratatui::Frame) {
fn render(&self, _conext: &mut sift_state::State, frame: &mut ratatui::Frame) {
frame.render_widget(Line::from("quitting..."), frame.area());
}

fn should_quit(&self, _context: &mut CommonState) -> bool {
fn should_quit(&self, _context: &mut sift_state::State) -> bool {
true
}
}
Loading

0 comments on commit dad768b

Please sign in to comment.