diff options
author | quentin konieczko <konieczko@gmail.com> | 2022-10-06 08:57:11 +0200 |
---|---|---|
committer | quentin konieczko <konieczko@gmail.com> | 2022-10-06 08:57:11 +0200 |
commit | 3885bbe38dc84a7941aff470c5930da2cafcf530 (patch) | |
tree | fee1f783b9c8b041b39a29ce1fe85881cf5150e6 | |
parent | 04d0ae387eae40aa2727b55792ee7ead93285394 (diff) |
moved preview to own mode
-rw-r--r-- | Cargo.lock | 227 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/display.rs | 7 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/preview.rs | 39 | ||||
-rw-r--r-- | src/status.rs | 40 |
6 files changed, 282 insertions, 35 deletions
@@ -3,6 +3,12 @@ version = 3 [[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] name = "aho-corasick" version = "0.7.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -38,6 +44,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -50,6 +71,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" [[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + +[[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -65,7 +92,7 @@ dependencies = [ "js-sys", "num-integer", "num-traits", - "time", + "time 0.1.44", "wasm-bindgen", "winapi", ] @@ -114,6 +141,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" [[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] name = "dirs" version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -155,6 +191,16 @@ dependencies = [ ] [[package]] +name = "flate2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] name = "fm" version = "0.1.0" dependencies = [ @@ -163,11 +209,18 @@ dependencies = [ "regex", "serde_yaml", "shellexpand", + "syntect", "tuikit", "users", ] [[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] name = "getrandom" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -251,6 +304,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966" [[package]] +name = "line-wrap" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9" +dependencies = [ + "safemem", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + +[[package]] name = "log" version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -275,6 +343,15 @@ dependencies = [ ] [[package]] +name = "miniz_oxide" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" +dependencies = [ + "adler", +] + +[[package]] name = "nix" version = "0.24.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -306,18 +383,69 @@ dependencies = [ ] [[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + +[[package]] name = "once_cell" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" [[package]] +name = "onig" +version = "6.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c4b31c8722ad9171c6d77d3557db078cab2bd50afcc9d09c8b315c59df8ca4f" +dependencies = [ + "bitflags", + "libc", + "once_cell", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b829e3d7e9cc74c7e315ee8edb185bf4190da5acde74afd7fc59c35b1f086e7" +dependencies = [ + "cc", + "pkg-config", +] + +[[package]] name = "os_str_bytes" version = "6.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" [[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + +[[package]] +name = "plist" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd39bc6cdc9355ad1dc5eeedefee696bb35c34caf21768741e81826c0bbd7225" +dependencies = [ + "base64", + "indexmap", + "line-wrap", + "serde", + "time 0.3.15", + "xml-rs", +] + +[[package]] name = "proc-macro-error" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -409,12 +537,49 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] name = "serde" version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" [[package]] +name = "serde_derive" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] name = "serde_yaml" version = "0.9.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -454,6 +619,29 @@ dependencies = [ ] [[package]] +name = "syntect" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6c454c27d9d7d9a84c7803aaa3c50cd088d2906fe3c6e42da3209aa623576a8" +dependencies = [ + "bincode", + "bitflags", + "flate2", + "fnv", + "lazy_static", + "once_cell", + "onig", + "plist", + "regex-syntax", + "serde", + "serde_derive", + "serde_json", + "thiserror", + "walkdir", + "yaml-rust", +] + +[[package]] name = "term" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -505,6 +693,17 @@ dependencies = [ ] [[package]] +name = "time" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d634a985c4d4238ec39cacaed2e7ae552fbd3c476b552c1deac3021b7d7eaf0c" +dependencies = [ + "itoa", + "libc", + "num_threads", +] + +[[package]] name = "tuikit" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -553,6 +752,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + +[[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -648,3 +858,18 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "xml-rs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] @@ -14,9 +14,10 @@ path = "src/lib.rs" [dependencies] chrono = "*" +clap = { version = "4.0", features = ["derive"] } regex = "1" serde_yaml = "0.9.13" shellexpand = "2.1.2" +syntect = "5.0" tuikit = "*" users = "0.11.0" -clap = { version = "4.0", features = ["derive"] } diff --git a/src/display.rs b/src/display.rs index 44a15a13..d7fe8ff3 100644 --- a/src/display.rs +++ b/src/display.rs @@ -191,12 +191,13 @@ impl Display { let _ = self.term.clear(); self.first_line(status); let content_attr = Attr::default(); - let line_number_offset = status.preview_lines.len().to_string().len() + 2; + let line_number_offset = status.preview.content.len().to_string().len() + 2; for (i, line) in status - .preview_lines + .preview + .content .iter() .enumerate() - .take(min(status.preview_lines.len(), status.window.bottom + 1)) + .take(min(status.preview.content.len(), status.window.bottom + 1)) .skip(status.window.top) { let row = i + ContentWindow::WINDOW_MARGIN_TOP - status.window.top; @@ -10,4 +10,5 @@ pub mod help; pub mod input; pub mod last_edition; pub mod mode; +pub mod preview; pub mod status; diff --git a/src/preview.rs b/src/preview.rs new file mode 100644 index 00000000..14392f71 --- /dev/null +++ b/src/preview.rs @@ -0,0 +1,39 @@ +use std::io::BufRead; + +use syntect; + +use crate::fileinfo::PathContent; + +pub struct Preview { + pub content: Box<Vec<String>>, +} + +impl Default for Preview { + fn default() -> Self { + Self { + content: Box::new(vec![]), + } + } +} + +impl Preview { + pub fn fill_preview_lines(&mut self, path_content: &PathContent) { + self.content = match path_content.selected_file() { + Some(file) => { + let reader = + std::io::BufReader::new(std::fs::File::open(file.path.clone()).unwrap()); + Box::new( + reader + .lines() + .map(|line| line.unwrap_or_else(|_| "".to_owned())) + .collect(), + ) + } + None => Box::new(vec![]), + }; + } + + pub fn empty_preview_lines(&mut self) { + self.content = Box::new(vec![]) + } +} diff --git a/src/status.rs b/src/status.rs index 5673cd8a..74d94f46 100644 --- a/src/status.rs +++ b/src/status.rs @@ -2,7 +2,6 @@ use std::borrow::Borrow; use std::cmp::min; use std::collections::HashSet; use std::fs; -use std::io::BufRead; use std::os::unix::fs::PermissionsExt; use std::path; use std::process::Command; @@ -17,6 +16,7 @@ use crate::fileinfo::{FileKind, PathContent, SortBy}; use crate::input::Input; use crate::last_edition::LastEdition; use crate::mode::Mode; +use crate::preview::Preview; /// Holds every thing about the current status of the application. /// Is responsible to execute commands depending on received events, mutating @@ -52,7 +52,7 @@ pub struct Status { must_quit: bool, /// Lines of the previewed files. /// Empty if not in preview mode. - pub preview_lines: Box<Vec<String>>, + pub preview: Preview, } impl Status { @@ -75,7 +75,7 @@ impl Status { let completion = Completion::default(); let last_edition = LastEdition::Nothing; let must_quit = false; - let preview_lines = Box::new(vec![]); + let preview = Preview::default(); Self { mode, line_index, @@ -91,7 +91,7 @@ impl Status { completion, last_edition, must_quit, - preview_lines, + preview, } } @@ -102,7 +102,7 @@ impl Status { self.path_content.reset_files(); self.window.reset(self.path_content.files.len()); self.mode = Mode::Normal; - self.empty_preview_lines() + self.preview.empty_preview_lines() } pub fn event_up_one_row(&mut self) { @@ -120,7 +120,7 @@ impl Status { self.path_content.select_next(); self.path_content.files.len() } else { - self.preview_lines.len() + self.preview.content.len() }; if self.line_index < max_line - ContentWindow::WINDOW_MARGIN_TOP { self.line_index += 1; @@ -160,7 +160,7 @@ impl Status { last_index = self.path_content.files.len() - 1; self.path_content.select_index(last_index); } else { - last_index = self.preview_lines.len() - 1; + last_index = self.preview.content.len() - 1; } self.line_index = last_index; self.window.scroll_to(last_index); @@ -178,7 +178,7 @@ impl Status { let down_index = if let Mode::Normal = self.mode { min(self.path_content.files.len() - 1, self.line_index + 10) } else { - min(self.preview_lines.len() - 1, self.line_index + 30) + min(self.preview.content.len() - 1, self.line_index + 30) }; self.path_content.select_index(down_index); self.line_index = down_index; @@ -276,8 +276,8 @@ impl Status { pub fn event_preview(&mut self) { if !self.path_content.files.is_empty() { self.mode = Mode::Preview; - self.fill_preview_lines(); - self.window.reset(self.preview_lines.len()) + self.preview.fill_preview_lines(&self.path_content); + self.window.reset(self.preview.content.len()) } } @@ -703,26 +703,6 @@ impl Status { pub fn must_quit(&self) -> bool { self.must_quit } - - fn fill_preview_lines(&mut self) { - self.preview_lines = match self.path_content.selected_file() { - Some(file) => { - let reader = - std::io::BufReader::new(std::fs::File::open(file.path.clone()).unwrap()); - Box::new( - reader - .lines() - .map(|line| line.unwrap_or_else(|_| "".to_owned())) - .collect(), - ) - } - None => Box::new(vec![]), - }; - } - - fn empty_preview_lines(&mut self) { - self.preview_lines = Box::new(vec![]) - } } // TODO: use [wait with output](https://doc.rust-lang.org/std/process/struct.Child.html#method.wait_with_output) |