Skip to content

Commit

Permalink
Update cursive
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Mar 31, 2024
1 parent 28900dd commit 808e232
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 63 deletions.
74 changes: 35 additions & 39 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ skim = "*"
# crossterm backend does not support Alt-<Key> bindings (interpret as just <Key>)
cursive = { version = "*", default-features = false, features = ["crossterm-backend"] }

[patch.crates-io]
cursive = { git = "https://github.com/azat-rust/cursive", branch = "next" }
cursive_core = { git = "https://github.com/azat-rust/cursive", branch = "next" }

[dependencies]
# Basic
anyhow = { version = "*", default-features = false, features = ["std"] }
Expand Down
33 changes: 15 additions & 18 deletions src/view/ext_table_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ use cursive::{
views::OnEventView,
wrap_impl,
};
use std::cell::RefCell;
use std::hash::Hash;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

/// A wrapper for cursive_table_view with more shortcuts:
///
/// - j/k -- for navigation
/// - PgUp/PgDown -- scroll the whole page
pub struct ExtTableView<T, H> {
inner_view: OnEventView<cursive_table_view::TableView<T, H>>,
last_size: Rc<RefCell<Vec2>>,
last_size: Arc<Mutex<Vec2>>,
}

pub use cursive_table_view::TableViewItem;

impl<T, H> ExtTableView<T, H>
where
T: 'static + cursive_table_view::TableViewItem<H>,
H: 'static + Eq + Hash + Copy + Clone,
T: 'static + cursive_table_view::TableViewItem<H> + Sync + Send,
H: 'static + Eq + Hash + Copy + Clone + Sync + Send,
{
inner_getters!(self.inner_view: OnEventView<cursive_table_view::TableView<T, H>>);
}

impl<T, H> Default for ExtTableView<T, H>
where
T: 'static + cursive_table_view::TableViewItem<H>,
H: 'static + Eq + Hash + Copy + Clone,
T: 'static + cursive_table_view::TableViewItem<H> + Sync + Send,
H: 'static + Eq + Hash + Copy + Clone + Sync + Send,
{
fn default() -> Self {
let table_view = cursive_table_view::TableView::new();

let last_size = Rc::new(RefCell::new(Vec2 { x: 1, y: 1 }));
let last_size = Arc::new(Mutex::new(Vec2 { x: 1, y: 1 }));
// FIXME: rewrite it to capture_it() or similar [1]
// [1]: https://github.com/rust-lang/rfcs/issues/2407
let last_size_clone_1 = last_size.clone();
Expand All @@ -56,7 +55,7 @@ where
let new_row = v
.row()
.map(|r| {
let height = last_size_clone_1.borrow_mut().y;
let height = last_size_clone_1.lock().unwrap().y;
let new_row = if r > height { r - height + 1 } else { 0 };
return new_row;
})
Expand All @@ -70,7 +69,7 @@ where
.row()
.map(|r| {
let len = v.len();
let height = last_size_clone_2.borrow_mut().y;
let height = last_size_clone_2.lock().unwrap().y;

if len > height + r {
r + height - 1
Expand All @@ -95,20 +94,18 @@ where

impl<T, H> ViewWrapper for ExtTableView<T, H>
where
T: 'static + cursive_table_view::TableViewItem<H>,
H: 'static + Eq + Hash + Copy + Clone,
T: 'static + cursive_table_view::TableViewItem<H> + Sync + Send,
H: 'static + Eq + Hash + Copy + Clone + Sync + Send,
{
wrap_impl!(self.inner_view: OnEventView<cursive_table_view::TableView<T, H>>);

fn wrap_layout(&mut self, size: Vec2) {
self.last_size.replace(size);

let mut last_size = self.last_size.borrow_mut();
if last_size.y > 2 {
let mut size = size;
if size.y > 2 {
// header and borders
last_size.y -= 2;
size.y -= 2;
}

*self.last_size.lock().unwrap() = size;
self.inner_view.layout(size);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/view/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub trait Navigation {
on_submit: Option<F>,
settings: &HashMap<&str, &str>,
) where
F: Fn(&mut Cursive, view::QueryResultRow) + 'static;
F: Fn(&mut Cursive, view::QueryResultRow) + Send + Sync + 'static;

// TODO: move into separate trait
fn call_on_name_or_render_error<V, F>(&mut self, name: &str, callback: F)
Expand Down Expand Up @@ -979,7 +979,7 @@ impl Navigation for Cursive {
on_submit: Option<F>,
settings: &HashMap<&str, &str>,
) where
F: Fn(&mut Cursive, view::QueryResultRow) + 'static,
F: Fn(&mut Cursive, view::QueryResultRow) + Send + Sync + 'static,
{
if self.has_view(table) {
return;
Expand Down
8 changes: 4 additions & 4 deletions src/view/query_result_view.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::rc::Rc;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use size::{Base, SizeFormatter, Style};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl TableViewItem<u8> for Row {
}
}

type RowCallback = Rc<dyn Fn(&mut Cursive, Row)>;
type RowCallback = Arc<dyn Fn(&mut Cursive, Row) + Send + Sync>;

pub struct QueryResultView {
table: ExtTableView<Row, u8>,
Expand Down Expand Up @@ -153,9 +153,9 @@ impl QueryResultView {

pub fn set_on_submit<F>(&mut self, cb: F)
where
F: Fn(&mut Cursive, Row) + 'static,
F: Fn(&mut Cursive, Row) + Send + Sync + 'static,
{
self.on_submit = Some(Rc::new(cb));
self.on_submit = Some(Arc::new(cb));
}

pub fn new(
Expand Down

0 comments on commit 808e232

Please sign in to comment.