Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ thiserror = "^2"
regex = { version = "^1", optional = true }
crossbeam-channel = "^0.5"
parking_lot = "0.12.1"
arboard = { version = "3", optional = true, default-features = false, features = ["wayland-data-control"] }

[features]
search = [ "regex" ]
static_output = []
dynamic_output = []
clipboard = ["arboard"]

[dev-dependencies]
tokio = { version = "^1.0", features = ["rt", "macros", "rt-multi-thread", "time"] }
Expand Down
2 changes: 1 addition & 1 deletion src/core/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Debug for Command {
Self::SetRunNoOverflow(val) => write!(f, "SetRunNoOverflow({val:?})"),
Self::UserInput(input) => write!(f, "UserInput({input:?})"),
Self::FollowOutput(follow_output) => write!(f, "FollowOutput({follow_output:?})"),
Self::Io(c) => write!(f, "Internal({c:?})"),
Self::Io(c) => write!(f, "Io({c:?})"),
}
}
}
229 changes: 225 additions & 4 deletions src/core/ev_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use crate::{PagerState, error::MinusError, hooks::Hook, input::InputEvent};
/// - Call search related functions
#[cfg_attr(not(feature = "search"), allow(unused_mut))]
#[allow(clippy::too_many_lines)]
// TODO: Remove it in next major release
#[allow(deprecated)]
pub fn handle_event(
ev: Command,
p: &mut PagerState,
Expand Down Expand Up @@ -59,6 +61,85 @@ pub fn handle_event(
p.left_mark = lm;
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
Command::UserInput(InputEvent::StartSelection { x, y }) => {
#[cfg(feature = "search")]
if p.search_state.search_term.is_some() {
return;
}

if let Some(selection) = p.selection_from_coordinates(x, y) {
p.selection_anchor = Some(selection);
p.selection = Some(selection);
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
}
Command::UserInput(InputEvent::UpdateSelection { x, y }) => {
#[cfg(feature = "search")]
if p.search_state.search_term.is_some() {
return;
}

if p.selection_anchor.is_none() {
return;
}

let writable_rows = p.rows.saturating_sub(1);
if writable_rows == 0 {
return;
}

let row_count = p.screen.formatted_lines_count();
let max_upper_mark = row_count.saturating_sub(writable_rows);
let mut should_redraw = false;
let mut selection_y = usize::from(y);

if y == 0 {
let next_upper_mark = p.upper_mark.saturating_sub(1);
if next_upper_mark != p.upper_mark {
p.upper_mark = next_upper_mark;
should_redraw = true;
}
selection_y = 0;
} else if selection_y >= writable_rows {
let next_upper_mark = p.upper_mark.saturating_add(1).min(max_upper_mark);
if next_upper_mark != p.upper_mark {
p.upper_mark = next_upper_mark;
should_redraw = true;
}
selection_y = writable_rows.saturating_sub(1);
}

#[allow(clippy::cast_possible_truncation)]
if let Some(selection) = p.selection_from_coordinates(x, selection_y as u16)
&& p.selection != Some(selection)
{
p.selection = Some(selection);
should_redraw = true;
}

if should_redraw {
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
}
Command::UserInput(InputEvent::ClearSelection) => {
if p.selection.is_some() || p.selection_anchor.is_some() {
p.clear_selection();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
}

#[cfg(feature = "clipboard")]
Command::UserInput(InputEvent::CopySelection) => {
if let Some(text) = p.extract_selection()
&& let Ok(mut clipboard) = arboard::Clipboard::new()
{
let _ = clipboard.set_text(text);
}
if p.selection.is_some() || p.selection_anchor.is_some() {
p.clear_selection();
command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
}
}
Command::UserInput(InputEvent::RestorePrompt) => {
// Set the message to None and new messages to false as all messages have been shown
p.message = None;
Expand Down Expand Up @@ -316,13 +397,13 @@ pub fn handle_io_command(
let AppendStyle::PartialUpdate(bounds) = append_style else {
unreachable!();
};
let fmt_lines = p.screen.get_formatted_lines_with_bounds(bounds.0, bounds.1);
let fmt_lines = p.render_rows_for_display(bounds.0, bounds.1);
display::draw_append_text(
out,
p.rows,
prev_unterminated,
prev_fmt_lines_count,
fmt_lines,
&fmt_lines,
)?;
}
#[cfg(feature = "search")]
Expand Down Expand Up @@ -376,9 +457,10 @@ pub fn handle_io_command(

#[cfg(test)]
mod tests {
use super::super::commands::Command;
use super::super::commands::{Command, IoCommand};
use super::handle_event;
use crate::{PagerState, minus_core::CommandQueue};
use crate::{PagerState, input::InputEvent, minus_core::CommandQueue, state::Selection};
use std::fmt::Write;
use std::sync::{Arc, atomic::AtomicBool};

const TEST_STR: &str = "This is some sample text";
Expand Down Expand Up @@ -488,4 +570,143 @@ mod tests {
);
assert_eq!(ps.exit_callbacks.len(), 1);
}

#[test]
fn update_selection_scrolls_up_at_top_edge() {
let mut ps = PagerState::new().unwrap();
ps.rows = 5;
ps.screen.orig_text = (0..10).fold(String::new(), |mut t, idx| {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.upper_mark = 3;
ps.selection_anchor = Some(Selection {
absolute_row: 3,
col: 0,
});
ps.selection = ps.selection_anchor;
let mut command_queue = CommandQueue::new_zero();

handle_event(
Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 0 }),
&mut ps,
&mut command_queue,
&Arc::new(AtomicBool::new(false)),
);

assert_eq!(ps.upper_mark, 2);
assert_eq!(
ps.selection,
Some(Selection {
absolute_row: 2,
col: 0,
})
);
assert_eq!(
command_queue.pop_front(),
Some(Command::Io(IoCommand::RedrawDisplay))
);
}

#[test]
fn update_selection_scrolls_down_at_bottom_edge() {
let mut ps = PagerState::new().unwrap();
ps.rows = 5;
ps.screen.orig_text = (0..10).fold(String::new(), |mut t, idx| {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.upper_mark = 3;
ps.selection_anchor = Some(Selection {
absolute_row: 3,
col: 0,
});
ps.selection = ps.selection_anchor;
let mut command_queue = CommandQueue::new_zero();

handle_event(
Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 4 }),
&mut ps,
&mut command_queue,
&Arc::new(AtomicBool::new(false)),
);

assert_eq!(ps.upper_mark, 4);
assert_eq!(
ps.selection,
Some(Selection {
absolute_row: 7,
col: 0,
})
);
assert_eq!(
command_queue.pop_front(),
Some(Command::Io(IoCommand::RedrawDisplay))
);
}

#[test]
fn update_selection_clamps_scroll_at_bottom_bound() {
let mut ps = PagerState::new().unwrap();
ps.rows = 5;
ps.screen.orig_text = (0..6).fold(String::new(), |mut t, idx| {
let _ = writeln!(t, "line {idx}");
t
});
ps.format_lines();
ps.upper_mark = 2;
ps.selection_anchor = Some(Selection {
absolute_row: 2,
col: 0,
});
ps.selection = ps.selection_anchor;
let mut command_queue = CommandQueue::new_zero();

handle_event(
Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 10 }),
&mut ps,
&mut command_queue,
&Arc::new(AtomicBool::new(false)),
);

assert_eq!(ps.upper_mark, 2);
assert_eq!(
ps.selection,
Some(Selection {
absolute_row: 5,
col: 0,
})
);
assert_eq!(
command_queue.pop_front(),
Some(Command::Io(IoCommand::RedrawDisplay))
);
}

#[test]
#[cfg(feature = "clipboard")]
fn copy_selection_clears_selection_and_redraws() {
let mut ps = PagerState::new().unwrap();
ps.selection = Some(Selection {
absolute_row: 0,
col: 0,
});
let mut command_queue = CommandQueue::new_zero();

handle_event(
Command::UserInput(InputEvent::CopySelection),
&mut ps,
&mut command_queue,
&Arc::new(AtomicBool::new(false)),
);

assert_eq!(ps.selection, None);
assert_eq!(ps.selection_anchor, None);
assert_eq!(
command_queue.pop_front(),
Some(Command::Io(IoCommand::RedrawDisplay))
);
}
}
36 changes: 8 additions & 28 deletions src/core/utils/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn draw_for_change(
// need this value whatever the value of delta be.
let normalized_delta = delta.min(writable_rows);

let lines = match (*new_upper_mark).cmp(&ps.upper_mark) {
let (start, end) = match (*new_upper_mark).cmp(&ps.upper_mark) {
Ordering::Greater => {
// Scroll down `normalized_delta` lines, and put the cursor one line above, where the old prompt would present.
// Clear it off and start displaying new dta.
Expand All @@ -87,10 +87,9 @@ pub fn draw_for_change(
queue!(out, Clear(ClearType::CurrentLine))?;

if delta < writable_rows {
ps.screen
.get_formatted_lines_with_bounds(lower_bound, new_lower_bound)
(lower_bound, new_lower_bound)
} else {
ps.screen.get_formatted_lines_with_bounds(
(
*new_upper_mark,
new_upper_mark.saturating_add(normalized_delta),
)
Expand All @@ -103,23 +102,16 @@ pub fn draw_for_change(
)?;
term::move_cursor(out, 0, 0, false)?;

ps.screen.get_formatted_lines_with_bounds(
(
*new_upper_mark,
new_upper_mark.saturating_add(normalized_delta),
)
}
Ordering::Equal => return Ok(()),
};

write_lines(
out,
lines,
ps.cols,
ps.screen.line_wrapping,
ps.left_mark,
ps.line_numbers.is_on(),
ps.screen.line_count(),
)?;
let lines = ps.render_rows_for_display(start, end);
write_raw_lines(out, &lines, Some("\r"))?;

ps.upper_mark = *new_upper_mark;

Expand Down Expand Up @@ -284,20 +276,8 @@ pub fn write_from_pagerstate(out: &mut impl Write, ps: &mut PagerState) -> Resul
ps.upper_mark = line_count.saturating_sub(writable_rows);
}

// Add \r to ensure cursor is placed at the beginning of each row
let display_lines: &[String] = ps
.screen
.get_formatted_lines_with_bounds(ps.upper_mark, lower_mark);

write_lines(
out,
display_lines,
ps.cols,
ps.screen.line_wrapping,
ps.left_mark,
ps.line_numbers.is_on(),
ps.screen.line_count(),
)
let display_lines = ps.render_rows_for_display(ps.upper_mark, lower_mark);
write_raw_lines(out, &display_lines, Some("\r"))
}

pub fn write_lines(
Expand Down
9 changes: 9 additions & 0 deletions src/core/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ impl LinesRowMap {
pub fn get(&self, ln: usize) -> Option<&usize> {
self.0.get(ln)
}

#[allow(dead_code)]
pub fn row_to_line(&self, row: usize) -> Option<usize> {
match self.0.binary_search(&row) {
Ok(line) => Some(line),
Err(0) => None,
Err(next_line) => Some(next_line - 1),
}
}
}
9 changes: 9 additions & 0 deletions src/input/definitions/mousedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ mod tests {
column: 0,
}
);
assert_eq!(
parse_mouse_event("left:up"),
MouseEvent {
kind: MouseEventKind::Up(MouseButton::Left),
modifiers: KeyModifiers::NONE,
row: 0,
column: 0,
}
);

assert_eq!(
parse_mouse_event("mid:up"),
Expand Down
Loading
Loading