Revert vimlike scrolling

Use stateful ui widget.
pull/709/head
Arijit Basu 2 months ago committed by Arijit Basu
parent 6fb0781fe4
commit ce52bcdf94

@ -335,12 +335,12 @@ impl App {
&config &config
.general .general
.initial_mode .initial_mode
.to_owned() .clone()
.unwrap_or_else(|| "default".into()), .unwrap_or_else(|| "default".into()),
) { ) {
Some(m) => m.clone().sanitized( Some(m) => m.clone().sanitized(
config.general.read_only, config.general.read_only,
config.general.global_key_bindings.to_owned(), config.general.global_key_bindings.clone(),
), ),
None => { None => {
bail!("'default' mode is missing") bail!("'default' mode is missing")
@ -351,7 +351,7 @@ impl App {
&config &config
.general .general
.initial_layout .initial_layout
.to_owned() .clone()
.unwrap_or_else(|| "default".into()), .unwrap_or_else(|| "default".into()),
) { ) {
Some(l) => l.clone(), Some(l) => l.clone(),
@ -752,10 +752,8 @@ impl App {
self.explorer_config.clone(), self.explorer_config.clone(),
self.pwd.clone().into(), self.pwd.clone().into(),
focus.as_ref().map(PathBuf::from), focus.as_ref().map(PathBuf::from),
self.directory_buffer self.directory_buffer.as_ref().map(|d| d.focus).unwrap_or(0),
.as_ref() self.config.general.vimlike_scrolling,
.map(|d| d.scroll_state.get_focus())
.unwrap_or(0),
) { ) {
Ok(dir) => self.set_directory(dir), Ok(dir) => self.set_directory(dir),
Err(e) => { Err(e) => {
@ -794,7 +792,7 @@ impl App {
} }
} }
dir.scroll_state.set_focus(0); dir.focus = 0;
if save_history { if save_history {
if let Some(n) = self.focused_node() { if let Some(n) = self.focused_node() {
@ -812,7 +810,7 @@ impl App {
history = history.push(n.absolute_path.clone()); history = history.push(n.absolute_path.clone());
} }
dir.scroll_state.set_focus(dir.total.saturating_sub(1)); dir.focus = dir.total.saturating_sub(1);
if let Some(n) = dir.focused_node() { if let Some(n) = dir.focused_node() {
self.history = history.push(n.absolute_path.clone()); self.history = history.push(n.absolute_path.clone());
@ -823,15 +821,15 @@ impl App {
fn focus_previous(mut self) -> Result<Self> { fn focus_previous(mut self) -> Result<Self> {
let bounded = self.config.general.enforce_bounded_index_navigation; let bounded = self.config.general.enforce_bounded_index_navigation;
if let Some(dir) = self.directory_buffer_mut() { if let Some(dir) = self.directory_buffer_mut() {
if dir.scroll_state.get_focus() == 0 { dir.focus = if dir.focus == 0 {
if !bounded { if bounded {
dir.scroll_state.set_focus(dir.total.saturating_sub(1)); dir.focus
} else {
dir.total.saturating_sub(1)
} }
} else { } else {
dir.scroll_state dir.focus.saturating_sub(1)
.set_focus(dir.scroll_state.get_focus().saturating_sub(1));
}; };
}; };
Ok(self) Ok(self)
@ -884,8 +882,7 @@ impl App {
history = history.push(n.absolute_path.clone()); history = history.push(n.absolute_path.clone());
} }
dir.scroll_state dir.focus = dir.focus.saturating_sub(index);
.set_focus(dir.scroll_state.get_focus().saturating_sub(index));
if let Some(n) = self.focused_node() { if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone()); self.history = history.push(n.absolute_path.clone());
} }
@ -908,16 +905,18 @@ impl App {
fn focus_next(mut self) -> Result<Self> { fn focus_next(mut self) -> Result<Self> {
let bounded = self.config.general.enforce_bounded_index_navigation; let bounded = self.config.general.enforce_bounded_index_navigation;
if let Some(dir) = self.directory_buffer_mut() { if let Some(dir) = self.directory_buffer_mut() {
if (dir.scroll_state.get_focus() + 1) == dir.total { dir.focus = if (dir.focus + 1) == dir.total {
if !bounded { if bounded {
dir.scroll_state.set_focus(0); dir.focus
} else {
0
} }
} else { } else {
dir.scroll_state.set_focus(dir.scroll_state.get_focus() + 1); dir.focus + 1
} }
}; };
Ok(self) Ok(self)
} }
@ -968,12 +967,10 @@ impl App {
history = history.push(n.absolute_path.clone()); history = history.push(n.absolute_path.clone());
} }
dir.scroll_state.set_focus( dir.focus = dir
dir.scroll_state .focus
.get_focus() .saturating_add(index)
.saturating_add(index) .min(dir.total.saturating_sub(1));
.min(dir.total.saturating_sub(1)),
);
if let Some(n) = self.focused_node() { if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone()); self.history = history.push(n.absolute_path.clone());
@ -998,7 +995,7 @@ impl App {
fn follow_symlink(self) -> Result<Self> { fn follow_symlink(self) -> Result<Self> {
if let Some(pth) = self if let Some(pth) = self
.focused_node() .focused_node()
.and_then(|n| n.symlink.to_owned().map(|s| s.absolute_path)) .and_then(|n| n.symlink.clone().map(|s| s.absolute_path))
{ {
self.focus_path(&pth, true) self.focus_path(&pth, true)
} else { } else {
@ -1241,8 +1238,7 @@ impl App {
fn focus_by_index(mut self, index: usize) -> Result<Self> { fn focus_by_index(mut self, index: usize) -> Result<Self> {
let history = self.history.clone(); let history = self.history.clone();
if let Some(dir) = self.directory_buffer_mut() { if let Some(dir) = self.directory_buffer_mut() {
dir.scroll_state dir.focus = index.min(dir.total.saturating_sub(1));
.set_focus(index.min(dir.total.saturating_sub(1)));
if let Some(n) = self.focused_node() { if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone()); self.history = history.push(n.absolute_path.clone());
} }
@ -1279,7 +1275,7 @@ impl App {
history = history.push(n.absolute_path.clone()); history = history.push(n.absolute_path.clone());
} }
} }
dir_buf.scroll_state.set_focus(focus); dir_buf.focus = focus;
if save_history { if save_history {
if let Some(n) = dir_buf.focused_node() { if let Some(n) = dir_buf.focused_node() {
self.history = history.push(n.absolute_path.clone()); self.history = history.push(n.absolute_path.clone());
@ -1386,7 +1382,7 @@ impl App {
self = self.push_mode(); self = self.push_mode();
self.mode = mode.sanitized( self.mode = mode.sanitized(
self.config.general.read_only, self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(), self.config.general.global_key_bindings.clone(),
); );
// Hooks // Hooks
@ -1411,7 +1407,7 @@ impl App {
self = self.push_mode(); self = self.push_mode();
self.mode = mode.sanitized( self.mode = mode.sanitized(
self.config.general.read_only, self.config.general.read_only,
self.config.general.global_key_bindings.to_owned(), self.config.general.global_key_bindings.clone(),
); );
// Hooks // Hooks
@ -1438,7 +1434,7 @@ impl App {
fn switch_layout_builtin(mut self, layout: &str) -> Result<Self> { fn switch_layout_builtin(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.builtin.get(layout) { if let Some(l) = self.config.layouts.builtin.get(layout) {
self.layout = l.to_owned(); self.layout = l.clone();
// Hooks // Hooks
if !self.hooks.on_layout_switch.is_empty() { if !self.hooks.on_layout_switch.is_empty() {
@ -1454,7 +1450,7 @@ impl App {
fn switch_layout_custom(mut self, layout: &str) -> Result<Self> { fn switch_layout_custom(mut self, layout: &str) -> Result<Self> {
if let Some(l) = self.config.layouts.get_custom(layout) { if let Some(l) = self.config.layouts.get_custom(layout) {
self.layout = l.to_owned(); self.layout = l.clone();
// Hooks // Hooks
if !self.hooks.on_layout_switch.is_empty() { if !self.hooks.on_layout_switch.is_empty() {
@ -1579,7 +1575,7 @@ impl App {
pub fn select(mut self) -> Result<Self> { pub fn select(mut self) -> Result<Self> {
let count = self.selection.len(); let count = self.selection.len();
if let Some(n) = self.focused_node().map(|n| n.to_owned()) { if let Some(n) = self.focused_node().cloned() {
self.selection.insert(n); self.selection.insert(n);
} }
@ -1634,7 +1630,7 @@ impl App {
pub fn un_select(mut self) -> Result<Self> { pub fn un_select(mut self) -> Result<Self> {
let count = self.selection.len(); let count = self.selection.len();
if let Some(n) = self.focused_node().map(|n| n.to_owned()) { if let Some(n) = self.focused_node().cloned() {
self.selection self.selection
.retain(|s| s.absolute_path != n.absolute_path); .retain(|s| s.absolute_path != n.absolute_path);
} }
@ -1808,7 +1804,7 @@ impl App {
.config .config
.general .general
.initial_sorting .initial_sorting
.to_owned() .clone()
.unwrap_or_default(); .unwrap_or_default();
Ok(self) Ok(self)
} }

@ -7,7 +7,7 @@ use crate::ui::block;
use crate::ui::string_to_text; use crate::ui::string_to_text;
use crate::ui::Constraint; use crate::ui::Constraint;
use crate::ui::ContentRendererArg; use crate::ui::ContentRendererArg;
use mlua::Lua; use crate::ui::UI;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tui::layout::Constraint as TuiConstraint; use tui::layout::Constraint as TuiConstraint;
use tui::layout::Rect as TuiRect; use tui::layout::Rect as TuiRect;
@ -60,12 +60,11 @@ pub struct CustomContent {
/// A cursed function from crate::ui. /// A cursed function from crate::ui.
pub fn draw_custom_content( pub fn draw_custom_content(
ui: &mut UI,
f: &mut Frame, f: &mut Frame,
screen_size: TuiRect,
layout_size: TuiRect, layout_size: TuiRect,
app: &app::App, app: &app::App,
content: CustomContent, content: CustomContent,
lua: &Lua,
) { ) {
let config = app.config.general.panel_ui.default.clone(); let config = app.config.general.panel_ui.default.clone();
let title = content.title; let title = content.title;
@ -85,12 +84,12 @@ pub fn draw_custom_content(
let ctx = ContentRendererArg { let ctx = ContentRendererArg {
app: app.to_lua_ctx_light(), app: app.to_lua_ctx_light(),
layout_size: layout_size.into(), layout_size: layout_size.into(),
screen_size: screen_size.into(), screen_size: ui.screen_size.into(),
}; };
let render = lua::serialize(lua, &ctx) let render = lua::serialize(ui.lua, &ctx)
.map(|arg| { .map(|arg| {
lua::call(lua, &render, arg).unwrap_or_else(|e| format!("{e:?}")) lua::call(ui.lua, &render, arg).unwrap_or_else(|e| format!("{e:?}"))
}) })
.unwrap_or_else(|e| e.to_string()); .unwrap_or_else(|e| e.to_string());
@ -121,12 +120,12 @@ pub fn draw_custom_content(
let ctx = ContentRendererArg { let ctx = ContentRendererArg {
app: app.to_lua_ctx_light(), app: app.to_lua_ctx_light(),
layout_size: layout_size.into(), layout_size: layout_size.into(),
screen_size: screen_size.into(), screen_size: ui.screen_size.into(),
}; };
let items = lua::serialize(lua, &ctx) let items = lua::serialize(ui.lua, &ctx)
.map(|arg| { .map(|arg| {
lua::call(lua, &render, arg) lua::call(ui.lua, &render, arg)
.unwrap_or_else(|e| vec![format!("{e:?}")]) .unwrap_or_else(|e| vec![format!("{e:?}")])
}) })
.unwrap_or_else(|e| vec![e.to_string()]) .unwrap_or_else(|e| vec![e.to_string()])
@ -161,7 +160,7 @@ pub fn draw_custom_content(
let widths = widths let widths = widths
.into_iter() .into_iter()
.map(|w| w.to_tui(screen_size, layout_size)) .map(|w| w.to_tui(ui.screen_size, layout_size))
.collect::<Vec<TuiConstraint>>(); .collect::<Vec<TuiConstraint>>();
let content = Table::new(rows, widths) let content = Table::new(rows, widths)
@ -182,12 +181,12 @@ pub fn draw_custom_content(
let ctx = ContentRendererArg { let ctx = ContentRendererArg {
app: app.to_lua_ctx_light(), app: app.to_lua_ctx_light(),
layout_size: layout_size.into(), layout_size: layout_size.into(),
screen_size: screen_size.into(), screen_size: ui.screen_size.into(),
}; };
let rows = lua::serialize(lua, &ctx) let rows = lua::serialize(ui.lua, &ctx)
.map(|arg| { .map(|arg| {
lua::call(lua, &render, arg) lua::call(ui.lua, &render, arg)
.unwrap_or_else(|e| vec![vec![format!("{e:?}")]]) .unwrap_or_else(|e| vec![vec![format!("{e:?}")]])
}) })
.unwrap_or_else(|e| vec![vec![e.to_string()]]) .unwrap_or_else(|e| vec![vec![e.to_string()]])
@ -204,7 +203,7 @@ pub fn draw_custom_content(
let widths = widths let widths = widths
.into_iter() .into_iter()
.map(|w| w.to_tui(screen_size, layout_size)) .map(|w| w.to_tui(ui.screen_size, layout_size))
.collect::<Vec<TuiConstraint>>(); .collect::<Vec<TuiConstraint>>();
let mut content = Table::new(rows, &widths).block(block( let mut content = Table::new(rows, &widths).block(block(

@ -55,7 +55,7 @@ pub struct NodeTypeConfig {
impl NodeTypeConfig { impl NodeTypeConfig {
pub fn extend(mut self, other: &Self) -> Self { pub fn extend(mut self, other: &Self) -> Self {
self.style = self.style.extend(&other.style); self.style = self.style.extend(&other.style);
self.meta.extend(other.meta.to_owned()); self.meta.extend(other.meta.clone());
self self
} }
} }
@ -85,11 +85,11 @@ pub struct NodeTypesConfig {
impl NodeTypesConfig { impl NodeTypesConfig {
pub fn get(&self, node: &Node) -> NodeTypeConfig { pub fn get(&self, node: &Node) -> NodeTypeConfig {
let mut node_type = if node.is_symlink { let mut node_type = if node.is_symlink {
self.symlink.to_owned() self.symlink.clone()
} else if node.is_dir { } else if node.is_dir {
self.directory.to_owned() self.directory.clone()
} else { } else {
self.file.to_owned() self.file.clone()
}; };
let mut me = node.mime_essence.splitn(2, '/'); let mut me = node.mime_essence.splitn(2, '/');
@ -141,7 +141,7 @@ pub struct UiElement {
impl UiElement { impl UiElement {
pub fn extend(mut self, other: &Self) -> Self { pub fn extend(mut self, other: &Self) -> Self {
self.format = other.format.to_owned().or(self.format); self.format = other.format.clone().or(self.format);
self.style = self.style.extend(&other.style); self.style = self.style.extend(&other.style);
self self
} }
@ -641,8 +641,8 @@ impl PanelUiConfig {
pub fn extend(mut self, other: &Self) -> Self { pub fn extend(mut self, other: &Self) -> Self {
self.title = self.title.extend(&other.title); self.title = self.title.extend(&other.title);
self.style = self.style.extend(&other.style); self.style = self.style.extend(&other.style);
self.borders = other.borders.to_owned().or(self.borders); self.borders = other.borders.clone().or(self.borders);
self.border_type = other.border_type.to_owned().or(self.border_type); self.border_type = other.border_type.or(self.border_type);
self.border_style = self.border_style.extend(&other.border_style); self.border_style = self.border_style.extend(&other.border_style);
self self
} }

@ -1,32 +1,52 @@
use crate::node::Node; use crate::node::Node;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use time::OffsetDateTime; use time::OffsetDateTime;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
pub struct ScrollState { pub struct ScrollState {
current_focus: usize, pub current_focus: usize,
pub last_focus: Option<usize>, pub last_focus: Option<usize>,
pub skipped_rows: usize, pub skipped_rows: usize,
/* The number of visible next lines when scrolling towards either ends of the view port */ /* The number of visible next lines when scrolling towards either ends of the view port */
pub initial_preview_cushion: usize, pub initial_preview_cushion: usize,
pub vimlike_scrolling: bool,
} }
impl ScrollState { impl ScrollState {
pub fn set_focus(&mut self, current_focus: usize) { pub fn new(current_focus: usize, total: usize, vimlike_scrolling: bool) -> Self {
let initial_preview_cushion = 5;
Self {
current_focus,
last_focus: None,
skipped_rows: 0,
initial_preview_cushion,
vimlike_scrolling,
}
.update_skipped_rows(initial_preview_cushion + 1, total)
}
pub fn set_focus(mut self, current_focus: usize) -> Self {
self.last_focus = Some(self.current_focus); self.last_focus = Some(self.current_focus);
self.current_focus = current_focus; self.current_focus = current_focus;
self
} }
pub fn get_focus(&self) -> usize { pub fn update_skipped_rows(self, height: usize, total: usize) -> Self {
self.current_focus if self.vimlike_scrolling {
self.update_skipped_rows_vimlike(height, total)
} else {
self.update_skipped_rows_paginated(height)
}
} }
pub fn calc_skipped_rows( pub fn update_skipped_rows_paginated(mut self, height: usize) -> Self {
&self, self.skipped_rows = height * (self.current_focus / height.max(1));
height: usize, self
total: usize, }
vimlike_scrolling: bool,
) -> usize { pub fn update_skipped_rows_vimlike(mut self, height: usize, total: usize) -> Self {
let preview_cushion = if height >= self.initial_preview_cushion * 3 { let preview_cushion = if height >= self.initial_preview_cushion * 3 {
self.initial_preview_cushion self.initial_preview_cushion
} else if height >= 9 { } else if height >= 9 {
@ -47,45 +67,52 @@ impl ScrollState {
.saturating_sub(preview_cushion + 1) .saturating_sub(preview_cushion + 1)
.min(total.saturating_sub(preview_cushion + 1)); .min(total.saturating_sub(preview_cushion + 1));
if !vimlike_scrolling { self.skipped_rows = if current_focus == 0 {
height * (self.current_focus / height.max(1))
} else if last_focus.is_none() {
// Just entered the directory
0
} else if current_focus == 0 {
// When focus goes to first node // When focus goes to first node
0 0
} else if current_focus == total.saturating_sub(1) { } else if current_focus == total.saturating_sub(1) {
// When focus goes to last node // When focus goes to last node
total.saturating_sub(height) total.saturating_sub(height)
} else if (start_cushion_row..=end_cushion_row).contains(&current_focus) { } else if current_focus > start_cushion_row && current_focus <= end_cushion_row {
// If within cushioned area; do nothing // If within cushioned area; do nothing
first_visible_row first_visible_row
} else if current_focus > last_focus.unwrap() { } else if let Some(last_focus) = last_focus {
// When scrolling down the cushioned area match current_focus.cmp(&last_focus) {
if current_focus > total.saturating_sub(preview_cushion + 1) { Ordering::Greater => {
// When focusing the last nodes; always view the full last page // When scrolling down the cushioned area
total.saturating_sub(height) if current_focus > total.saturating_sub(preview_cushion + 1) {
} else { // When focusing the last nodes; always view the full last page
// When scrolling down the cushioned area without reaching the last nodes total.saturating_sub(height)
current_focus.saturating_sub(height.saturating_sub(preview_cushion + 1)) } else {
} // When scrolling down the cushioned area without reaching the last nodes
} else if current_focus < last_focus.unwrap() { current_focus
// When scrolling up the cushioned area .saturating_sub(height.saturating_sub(preview_cushion + 1))
if current_focus < preview_cushion { }
// When focusing the first nodes; always view the full first page }
0
} else if current_focus > end_cushion_row { Ordering::Less => {
// When scrolling up from the last rows; do nothing // When scrolling up the cushioned area
first_visible_row if current_focus < preview_cushion {
} else { // When focusing the first nodes; always view the full first page
// When scrolling up the cushioned area without reaching the first nodes 0
current_focus.saturating_sub(preview_cushion) } else if current_focus > end_cushion_row {
// When scrolling up from the last rows; do nothing
first_visible_row
} else {
// When scrolling up the cushioned area without reaching the first nodes
current_focus.saturating_sub(preview_cushion)
}
}
Ordering::Equal => {
// Do nothing
first_visible_row
}
} }
} else { } else {
// If nothing matches; do nothing // Just entered dir
first_visible_row first_visible_row
} };
self
} }
} }
@ -94,31 +121,26 @@ pub struct DirectoryBuffer {
pub parent: String, pub parent: String,
pub nodes: Vec<Node>, pub nodes: Vec<Node>,
pub total: usize, pub total: usize,
pub scroll_state: ScrollState, pub focus: usize,
#[serde(skip, default = "now")] #[serde(skip, default = "now")]
pub explored_at: OffsetDateTime, pub explored_at: OffsetDateTime,
} }
impl DirectoryBuffer { impl DirectoryBuffer {
pub fn new(parent: String, nodes: Vec<Node>, current_focus: usize) -> Self { pub fn new(parent: String, nodes: Vec<Node>, focus: usize) -> Self {
let total = nodes.len(); let total = nodes.len();
Self { Self {
parent, parent,
nodes, nodes,
total, total,
scroll_state: ScrollState { focus,
current_focus,
last_focus: None,
skipped_rows: 0,
initial_preview_cushion: 5,
},
explored_at: now(), explored_at: now(),
} }
} }
pub fn focused_node(&self) -> Option<&Node> { pub fn focused_node(&self) -> Option<&Node> {
self.nodes.get(self.scroll_state.current_focus) self.nodes.get(self.focus)
} }
} }
@ -133,36 +155,39 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_calc_skipped_rows_non_vimlike_scrolling() { fn test_update_skipped_rows_paginated() {
let state = ScrollState { let state = ScrollState {
current_focus: 10, current_focus: 10,
last_focus: Some(8), last_focus: Some(8),
skipped_rows: 0, skipped_rows: 0,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: false,
}; };
let height = 5; let height = 5;
let total = 20; let total = 100;
let vimlike_scrolling = false;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let state = state.update_skipped_rows(height, total);
assert_eq!(result, height * (state.current_focus / height.max(1))); assert_eq!(
state.skipped_rows,
height * (state.current_focus / height.max(1))
);
} }
#[test] #[test]
fn test_calc_skipped_rows_entered_directory() { fn test_update_skipped_rows_entered_directory() {
let state = ScrollState { let state = ScrollState {
current_focus: 10, current_focus: 100,
last_focus: None, last_focus: None,
skipped_rows: 0, skipped_rows: 0,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: true,
}; };
let height = 5; let height = 5;
let total = 20; let total = 200;
let vimlike_scrolling = true;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let result = state.update_skipped_rows(height, total).skipped_rows;
assert_eq!(result, 0); assert_eq!(result, 0);
} }
@ -173,13 +198,13 @@ mod tests {
last_focus: Some(8), last_focus: Some(8),
skipped_rows: 5, skipped_rows: 5,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: true,
}; };
let height = 5; let height = 5;
let total = 20; let total = 20;
let vimlike_scrolling = true;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let result = state.update_skipped_rows(height, total).skipped_rows;
assert_eq!(result, 0); assert_eq!(result, 0);
} }
@ -190,13 +215,13 @@ mod tests {
last_focus: Some(18), last_focus: Some(18),
skipped_rows: 15, skipped_rows: 15,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: true,
}; };
let height = 5; let height = 5;
let total = 20; let total = 20;
let vimlike_scrolling = true;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let result = state.update_skipped_rows(height, total).skipped_rows;
assert_eq!(result, 15); assert_eq!(result, 15);
} }
@ -207,13 +232,13 @@ mod tests {
last_focus: Some(10), last_focus: Some(10),
skipped_rows: 10, skipped_rows: 10,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: true,
}; };
let height = 5; let height = 5;
let total = 20; let total = 20;
let vimlike_scrolling = true;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let result = state.update_skipped_rows(height, total).skipped_rows;
assert_eq!(result, 10); assert_eq!(result, 10);
} }
@ -224,13 +249,13 @@ mod tests {
last_focus: Some(10), last_focus: Some(10),
skipped_rows: 10, skipped_rows: 10,
initial_preview_cushion: 5, initial_preview_cushion: 5,
vimlike_scrolling: true,
}; };
let height = 5; let height = 5;
let total = 20; let total = 20;
let vimlike_scrolling = true;
let result = state.calc_skipped_rows(height, total, vimlike_scrolling); let result = state.update_skipped_rows(height, total).skipped_rows;
assert_eq!(result, 7); assert_eq!(result, 7);
} }

@ -22,5 +22,5 @@ pub fn runtime_dir() -> PathBuf {
else { else {
return env::temp_dir(); return env::temp_dir();
}; };
dir.to_owned() dir.clone()
} }

@ -45,6 +45,7 @@ pub(crate) fn explore_sync(
parent: PathBuf, parent: PathBuf,
focused_path: Option<PathBuf>, focused_path: Option<PathBuf>,
fallback_focus: usize, fallback_focus: usize,
vimlike_scrolling: bool,
) -> Result<DirectoryBuffer> { ) -> Result<DirectoryBuffer> {
let nodes = explore(&parent, &config)?; let nodes = explore(&parent, &config)?;
let focus_index = if config.searcher.is_some() { let focus_index = if config.searcher.is_some() {
@ -73,26 +74,33 @@ pub(crate) fn explore_async(
parent: PathBuf, parent: PathBuf,
focused_path: Option<PathBuf>, focused_path: Option<PathBuf>,
fallback_focus: usize, fallback_focus: usize,
vimlike_scrolling: bool,
tx_msg_in: Sender<Task>, tx_msg_in: Sender<Task>,
) { ) {
thread::spawn(move || { thread::spawn(move || {
explore_sync(config, parent.clone(), focused_path, fallback_focus) explore_sync(
.and_then(|buf| { config,
tx_msg_in parent.clone(),
.send(Task::new( focused_path,
MsgIn::Internal(InternalMsg::SetDirectory(buf)), fallback_focus,
None, vimlike_scrolling,
)) )
.map_err(Error::new) .and_then(|buf| {
}) tx_msg_in
.unwrap_or_else(|e| { .send(Task::new(
tx_msg_in MsgIn::Internal(InternalMsg::SetDirectory(buf)),
.send(Task::new( None,
MsgIn::External(ExternalMsg::LogError(e.to_string())), ))
None, .map_err(Error::new)
)) })
.unwrap_or_default(); // Let's not panic if xplr closes. .unwrap_or_else(|e| {
}) tx_msg_in
.send(Task::new(
MsgIn::External(ExternalMsg::LogError(e.to_string())),
None,
))
.unwrap_or_default(); // Let's not panic if xplr closes.
})
}); });
} }
@ -101,6 +109,7 @@ pub(crate) fn explore_recursive_async(
parent: PathBuf, parent: PathBuf,
focused_path: Option<PathBuf>, focused_path: Option<PathBuf>,
fallback_focus: usize, fallback_focus: usize,
vimlike_scrolling: bool,
tx_msg_in: Sender<Task>, tx_msg_in: Sender<Task>,
) { ) {
explore_async( explore_async(
@ -108,6 +117,7 @@ pub(crate) fn explore_recursive_async(
parent.clone(), parent.clone(),
focused_path, focused_path,
fallback_focus, fallback_focus,
vimlike_scrolling,
tx_msg_in.clone(), tx_msg_in.clone(),
); );
if let Some(grand_parent) = parent.parent() { if let Some(grand_parent) = parent.parent() {
@ -116,6 +126,7 @@ pub(crate) fn explore_recursive_async(
grand_parent.into(), grand_parent.into(),
parent.file_name().map(|p| p.into()), parent.file_name().map(|p| p.into()),
0, 0,
vimlike_scrolling,
tx_msg_in, tx_msg_in,
); );
} }
@ -130,7 +141,7 @@ mod tests {
let config = ExplorerConfig::default(); let config = ExplorerConfig::default();
let path = PathBuf::from("."); let path = PathBuf::from(".");
let r = explore_sync(config, path, None, 0); let r = explore_sync(config, path, None, 0, false);
assert!(r.is_ok()); assert!(r.is_ok());
} }
@ -140,7 +151,7 @@ mod tests {
let config = ExplorerConfig::default(); let config = ExplorerConfig::default();
let path = PathBuf::from("/there/is/no/path"); let path = PathBuf::from("/there/is/no/path");
let r = explore_sync(config, path, None, 0); let r = explore_sync(config, path, None, 0, false);
assert!(r.is_err()); assert!(r.is_err());
} }
@ -169,7 +180,7 @@ mod tests {
let path = PathBuf::from("."); let path = PathBuf::from(".");
let (tx_msg_in, rx_msg_in) = mpsc::channel(); let (tx_msg_in, rx_msg_in) = mpsc::channel();
explore_async(config, path, None, 0, tx_msg_in.clone()); explore_async(config, path, None, 0, false, tx_msg_in.clone());
let task = rx_msg_in.recv().unwrap(); let task = rx_msg_in.recv().unwrap();
let dbuf = extract_dirbuf_from_msg(task.msg); let dbuf = extract_dirbuf_from_msg(task.msg);

@ -647,7 +647,7 @@ impl Key {
Self::ShiftZ => Some('Z'), Self::ShiftZ => Some('Z'),
Self::Space => Some(' '), Self::Space => Some(' '),
Self::Special(c) => Some(c.to_owned()), Self::Special(c) => Some(*c),
_ => None, _ => None,
} }

@ -8,7 +8,8 @@ use crate::explorer;
use crate::lua; use crate::lua;
use crate::pipe; use crate::pipe;
use crate::pwd_watcher; use crate::pwd_watcher;
use crate::ui; use crate::ui::NO_COLOR;
use crate::ui::UI;
use crate::yaml; use crate::yaml;
use anyhow::{bail, Error, Result}; use anyhow::{bail, Error, Result};
use crossterm::event; use crossterm::event;
@ -89,7 +90,7 @@ fn call(
let focus_index = app let focus_index = app
.directory_buffer .directory_buffer
.as_ref() .as_ref()
.map(|d| d.scroll_state.get_focus()) .map(|d| d.focus)
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
@ -279,16 +280,14 @@ impl Runner {
app.explorer_config.clone(), app.explorer_config.clone(),
app.pwd.clone().into(), app.pwd.clone().into(),
self.focused_path, self.focused_path,
app.directory_buffer app.directory_buffer.as_ref().map(|d| d.focus).unwrap_or(0),
.as_ref() app.config.general.vimlike_scrolling,
.map(|d| d.scroll_state.get_focus())
.unwrap_or(0),
tx_msg_in.clone(), tx_msg_in.clone(),
); );
tx_pwd_watcher.send(app.pwd.clone())?; tx_pwd_watcher.send(app.pwd.clone())?;
let mut result = Ok(None); let mut result = Ok(None);
let session_path = app.session_path.to_owned(); let session_path = app.session_path.clone();
term::enable_raw_mode()?; term::enable_raw_mode()?;
@ -344,6 +343,9 @@ impl Runner {
None, None,
))?; ))?;
// UI
let mut ui = UI::new(&lua);
'outer: for task in rx_msg_in { 'outer: for task in rx_msg_in {
match app.handle_task(task) { match app.handle_task(task) {
Ok(a) => { Ok(a) => {
@ -433,8 +435,9 @@ impl Runner {
.map(|n| n.relative_path.clone().into()), .map(|n| n.relative_path.clone().into()),
app.directory_buffer app.directory_buffer
.as_ref() .as_ref()
.map(|d| d.scroll_state.get_focus()) .map(|d| d.focus)
.unwrap_or(0), .unwrap_or(0),
app.config.general.vimlike_scrolling,
tx_msg_in.clone(), tx_msg_in.clone(),
); );
tx_pwd_watcher.send(app.pwd.clone())?; tx_pwd_watcher.send(app.pwd.clone())?;
@ -448,8 +451,9 @@ impl Runner {
.map(|n| n.relative_path.clone().into()), .map(|n| n.relative_path.clone().into()),
app.directory_buffer app.directory_buffer
.as_ref() .as_ref()
.map(|d| d.scroll_state.get_focus()) .map(|d| d.focus)
.unwrap_or(0), .unwrap_or(0),
app.config.general.vimlike_scrolling,
tx_msg_in.clone(), tx_msg_in.clone(),
); );
tx_pwd_watcher.send(app.pwd.clone())?; tx_pwd_watcher.send(app.pwd.clone())?;
@ -479,7 +483,7 @@ impl Runner {
tx_pwd_watcher.send(app.pwd.clone())?; tx_pwd_watcher.send(app.pwd.clone())?;
// OSC 7: Change CWD // OSC 7: Change CWD
if !(*ui::NO_COLOR) { if !(*NO_COLOR) {
write!( write!(
terminal.backend_mut(), terminal.backend_mut(),
"\x1b]7;file://{}{}\x1b\\", "\x1b]7;file://{}{}\x1b\\",
@ -496,7 +500,7 @@ impl Runner {
} }
// UI // UI
terminal.draw(|f| ui::draw(f, &mut app, &lua))?; terminal.draw(|f| ui.draw(f, &app))?;
} }
EnableMouse => { EnableMouse => {

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save