Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cursive #45

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 7 additions & 34 deletions src/view/log_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use cursive::{
lines::spans::{LinesIterator, Row},
markup::StyledString,
},
view::{
scroll::Scroller, Nameable, Resizable, ScrollStrategy, Scrollable, SizeCache, View,
ViewWrapper,
},
view::{scroll, Nameable, Resizable, ScrollStrategy, Scrollable, SizeCache, View, ViewWrapper},
views::{EditView, NamedView, OnEventView, ScrollView},
wrap_impl, Cursive, Printer, Vec2, XY,
};
Expand Down Expand Up @@ -205,38 +202,14 @@ impl LogView {
// NOTE: we cannot pass mutable ref to view in search_prompt callback, sigh.
let v = v.with_name("logs");

// Once the following patches for cursive will be merged and release will be made, this
// could be reverted:
// - https://github.com/gyscos/cursive/pull/761
// - https://github.com/gyscos/cursive/pull/764 (for horizontal scrolling, which is not
// required with wrapping)
let scroll_page =
move |v: &mut NamedView<ScrollView<LogViewBase>>, e: &Event| -> Option<EventResult> {
let mut base = v.get_mut();
let scroller = base.get_scroller_mut();
let size = scroller.last_available_size();

match e {
Event::Key(Key::PageUp) => {
if scroller.can_scroll_up() {
log::trace!("scrolling up to: {}", size.y);
scroller.scroll_up(size.y);
}
scroller.set_scroll_strategy(ScrollStrategy::KeepRow);
return Some(EventResult::consumed());
}
Event::Key(Key::PageDown) => {
if scroller.can_scroll_down() {
log::trace!("scrolling down to: {}", size.y);
scroller.scroll_down(size.y);
}
scroller.set_scroll_strategy(ScrollStrategy::KeepRow);
return Some(EventResult::consumed());
}
_ => {
return None;
}
}
return Some(scroll::on_event(
&mut *v.get_mut(),
e.clone(),
|s: &mut ScrollView<LogViewBase>, e| s.on_event(e),
|s, si| s.important_area(si),
));
};

let reset_search =
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
Loading