From bb7053baa9b89632550ba9f5d405b361376e637a Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Wed, 17 Jan 2024 12:48:45 +0530 Subject: [PATCH] Better preview --- src/init.lua | 10 +++------ src/lua/util.rs | 57 +++++++++++++++++++++++++++++++++++-------------- src/ui.rs | 8 +++---- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/init.lua b/src/init.lua index 197111e..651c8f0 100644 --- a/src/init.lua +++ b/src/init.lua @@ -264,7 +264,7 @@ xplr.config.general.preview.renderer.format = "builtin.fmt_general_preview_rende -- Style for preview panel. -- -- Type: [Style](https://xplr.dev/en/style) -xplr.config.general.preview.renderer.style = {} +xplr.config.general.preview.renderer.style = { add_modifiers = { "Dim" } } -- The default search algorithm -- @@ -348,9 +348,7 @@ xplr.config.general.sort_and_filter_ui.separator.format = " › " -- The style of the separator for the Sort & filter panel. -- -- Type: [Style](https://xplr.dev/en/style) -xplr.config.general.sort_and_filter_ui.separator.style = { - add_modifiers = { "Dim" }, -} +xplr.config.general.sort_and_filter_ui.separator.style = { add_modifiers = { "Dim" } } -- The content of the default identifier in Sort & filter panel. -- @@ -649,9 +647,7 @@ xplr.config.general.panel_ui.selection.border_type = "Double" -- Style of the selection panel borders. -- -- Type: [Style](https://xplr.dev/en/style) -xplr.config.general.panel_ui.selection.border_style = { - fg = "Red", -} +xplr.config.general.panel_ui.selection.border_style = { fg = "Red" } -- The content for the preview panel title. -- diff --git a/src/lua/util.rs b/src/lua/util.rs index 30e6861..7e0d1c6 100644 --- a/src/lua/util.rs +++ b/src/lua/util.rs @@ -848,15 +848,12 @@ pub fn permissions_octal<'a>(util: Table<'a>, lua: &Lua) -> Result> { pub fn preview<'a>(util: Table<'a>, lua: &Lua) -> Result> { fn format_node(node: &Node) -> String { format!( - "{} -{} -{} -{}:{}", + "• T: {}\n• P: {}\n• O: {}:{}\n• S: {}", node.mime_essence, node.permissions.to_string(), - node.human_size, node.uid, - node.gid + node.gid, + node.human_size, ) } @@ -868,8 +865,19 @@ pub fn preview<'a>(util: Table<'a>, lua: &Lua) -> Result> { let size = args.layout_size; - let preview = if node.is_file { - let file = fs::File::open(&node.absolute_path)?; + let preview = if node + .canonical + .as_ref() + .map(|c| c.is_file) + .unwrap_or(node.is_file) + { + let path = node + .canonical + .as_ref() + .map(|c| &c.absolute_path) + .unwrap_or(&node.absolute_path); + + let file = fs::File::open(path)?; let reader = io::BufReader::new(file); let mut lines = vec![]; for line in reader.lines() { @@ -883,16 +891,33 @@ pub fn preview<'a>(util: Table<'a>, lua: &Lua) -> Result> { } } lines.join("\n") - } else if node.is_dir { - match fs::read_dir(node.absolute_path) { - Ok(nodes) => iter::once(node.relative_path) + } else if node + .canonical + .as_ref() + .map(|c| c.is_dir) + .unwrap_or(node.is_dir) + { + let path = node + .symlink + .as_ref() + .map(|c| &c.absolute_path) + .unwrap_or(&node.relative_path); + + match fs::read_dir(path) { + Ok(nodes) => iter::once(format!("▼ {}/", path)) .chain( nodes + .filter_map(|d| d.ok()) .map(|d| { - d.ok() - .map(|d| d.file_name().to_string_lossy().to_string()) - .map(|n| format!(" {n}")) - .unwrap_or_else(|| "???".into()) + if d.file_type() + .ok() + .map(|t| t.is_dir()) + .unwrap_or(false) + { + format!(" ▷ {}/", d.file_name().to_string_lossy()) + } else { + format!(" {}", d.file_name().to_string_lossy()) + } }) .take(size.height.into()), ) @@ -904,7 +929,7 @@ pub fn preview<'a>(util: Table<'a>, lua: &Lua) -> Result> { "-> ×".into() } else if node.is_symlink { node.symlink - .map(|s| format!(" -> {}", s.absolute_path)) + .map(|s| format!("-> {}", s.absolute_path)) .unwrap_or_default() } else { format_node(&node) diff --git a/src/ui.rs b/src/ui.rs index 6ad28d8..dbd0294 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -894,10 +894,10 @@ fn draw_preview( String::new() }; - let preview = - Paragraph::new(string_to_text(preview)).block(block(config, " Preview ".into())); - - f.render_widget(preview, layout_size); + let mut text = string_to_text(preview); + text.patch_style(app.config.general.preview.renderer.style.to_owned().into()); + let block = Paragraph::new(text).block(block(config, " Preview ".into())); + f.render_widget(block, layout_size); } fn draw_selection(