From 2ecf8bd5db43e02f7b9b2d1e99721b9f1dd54a28 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 22:10:51 +0000 Subject: [PATCH 1/4] Initial Commit --- .cargo/config.toml | 2 ++ .gitignore | 2 ++ Cargo.toml | 13 +++++++ LICENSE.md | 21 ++++++++++++ src/main.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++ src/state.rs | 55 ++++++++++++++++++++++++++++++ 6 files changed, 178 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE.md create mode 100644 src/main.rs create mode 100644 src/state.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..bc255e30 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-wasi" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..44709884 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..3aa24a7d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "module" +version = "0.1.0" +authors = ["Brooks J Rady "] +edition = "2018" +description = "A small client-side library for writing mosaic plugins (tiles)" +repository = "https://github.com/mosaic-org/strider" +license = "MIT" + +[dependencies] +colored = "2" +mosaic-tile = "0.1" +pretty-bytes = "0.2" \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..0b264c31 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Mosaic contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 00000000..f48d07fc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,85 @@ +mod state; + +use colored::*; +use mosaic_tile::register_tile; +use state::{FsEntry, State}; +use std::{cmp::min, fs::read_dir}; + +register_tile!(State); + +impl MosaicTile for State { + fn init(&mut self) { + refresh_directory(self); + } + + fn draw(&mut self, rows: usize, cols: usize) { + for i in 0..rows - 1 { + if self.selected() < self.scroll() { + *self.scroll_mut() = self.selected(); + } + if self.selected() - self.scroll() + 2 > rows { + *self.scroll_mut() = self.selected() + 2 - rows; + } + let i = self.scroll() + i; + if let Some(entry) = self.files.get(i) { + let mut path = entry.as_line(cols).normal(); + + if let FsEntry::Dir(..) = entry { + path = path.dimmed().bold(); + } + + if i == self.selected() { + println!("{}", path.reversed()); + } else { + println!("{}", path); + } + } else { + println!(); + } + } + } + + fn handle_key(&mut self, key: KeyEvent) { + match key.code { + KeyCode::Up => { + *self.selected_mut() = self.selected().saturating_sub(1); + } + KeyCode::Down => { + let next = self.selected().saturating_add(1); + *self.selected_mut() = min(self.files.len() - 1, next); + } + KeyCode::Right | KeyCode::Enter => match self.files[self.selected()].clone() { + FsEntry::Dir(p, _) => { + self.path = p; + refresh_directory(self); + } + FsEntry::File(p, _) => open_file(&p), + }, + KeyCode::Left => { + self.path.pop(); + refresh_directory(self); + } + _ => (), + }; + } +} + +fn refresh_directory(state: &mut State) { + state.files = read_dir(&state.path) + .unwrap() + .filter_map(|res| { + res.and_then(|d| { + if d.metadata()?.is_dir() { + let children = read_dir(d.path())?.count(); + Ok(FsEntry::Dir(d.path(), children)) + } else { + let size = d.metadata()?.len(); + Ok(FsEntry::File(d.path(), size)) + } + }) + .ok() + }) + .collect(); + + state.files.sort_unstable(); +} diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 00000000..cbb44dcd --- /dev/null +++ b/src/state.rs @@ -0,0 +1,55 @@ +use pretty_bytes::converter as pb; +use std::{collections::HashMap, path::PathBuf}; + +#[derive(Default)] +pub struct State { + pub path: PathBuf, + pub files: Vec, + pub cursor_hist: HashMap, +} + +impl State { + pub fn selected_mut(&mut self) -> &mut usize { + &mut self.cursor_hist.entry(self.path.clone()).or_default().0 + } + pub fn selected(&self) -> usize { + self.cursor_hist.get(&self.path).unwrap_or(&(0, 0)).0 + } + pub fn scroll_mut(&mut self) -> &mut usize { + &mut self.cursor_hist.entry(self.path.clone()).or_default().1 + } + pub fn scroll(&self) -> usize { + self.cursor_hist.get(&self.path).unwrap_or(&(0, 0)).1 + } +} + +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)] +pub enum FsEntry { + Dir(PathBuf, usize), + File(PathBuf, u64), +} + +impl FsEntry { + pub fn name(&self) -> String { + let path = match self { + FsEntry::Dir(p, _) => p, + FsEntry::File(p, _) => p, + }; + path.file_name().unwrap().to_string_lossy().into_owned() + } + + pub fn as_line(&self, width: usize) -> String { + let info = match self { + FsEntry::Dir(_, s) => s.to_string(), + FsEntry::File(_, s) => pb::convert(*s as f64), + }; + let space = width - info.len(); + let name = self.name(); + if space - 1 < name.len() { + [&name[..space - 2], &info].join("~ ") + } else { + let padding = " ".repeat(space - name.len()); + [name, padding, info].concat() + } + } +} From a57eadee8c87c60411a9ce93dd340d2b99a9e436 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 22:12:07 +0000 Subject: [PATCH 2/4] Update description --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3aa24a7d..7eab6a9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "module" version = "0.1.0" authors = ["Brooks J Rady "] edition = "2018" -description = "A small client-side library for writing mosaic plugins (tiles)" +description = "A simplified ranger clone written as a mosaic tile" repository = "https://github.com/mosaic-org/strider" license = "MIT" From 75136a613d8bdfc94e0c6f4e071cf050a7e2a231 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Thu, 7 Jan 2021 11:10:39 +0000 Subject: [PATCH 3/4] Update deps and add a build script --- .gitignore | 3 ++- Cargo.toml | 9 ++++++--- build-optimised.sh | 6 ++++++ src/main.rs | 14 +++++++------- 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100755 build-optimised.sh diff --git a/.gitignore b/.gitignore index 44709884..b7a9cefc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/ -Cargo.lock \ No newline at end of file +Cargo.lock +*.yaml \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 7eab6a9a..0a5d298d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "module" -version = "0.1.0" +version = "0.2.0" authors = ["Brooks J Rady "] edition = "2018" description = "A simplified ranger clone written as a mosaic tile" @@ -9,5 +9,8 @@ license = "MIT" [dependencies] colored = "2" -mosaic-tile = "0.1" -pretty-bytes = "0.2" \ No newline at end of file +mosaic-tile = "0.2" +pretty-bytes = "0.2" + +[profile.release] +lto = true \ No newline at end of file diff --git a/build-optimised.sh b/build-optimised.sh new file mode 100755 index 00000000..e0654d89 --- /dev/null +++ b/build-optimised.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Build a release WASM from Rust with lto on +cargo build --release +# Further optimise for speed (and size) +wasm-opt -O target/wasm32-wasi/release/module.wasm -o target/strider.wasm \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f48d07fc..beb57e39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ impl MosaicTile for State { } fn draw(&mut self, rows: usize, cols: usize) { - for i in 0..rows - 1 { + for i in 0..rows { if self.selected() < self.scroll() { *self.scroll_mut() = self.selected(); } @@ -39,23 +39,23 @@ impl MosaicTile for State { } } - fn handle_key(&mut self, key: KeyEvent) { - match key.code { - KeyCode::Up => { + fn handle_key(&mut self, key: Key) { + match key { + Key::Up => { *self.selected_mut() = self.selected().saturating_sub(1); } - KeyCode::Down => { + Key::Down => { let next = self.selected().saturating_add(1); *self.selected_mut() = min(self.files.len() - 1, next); } - KeyCode::Right | KeyCode::Enter => match self.files[self.selected()].clone() { + Key::Right | Key::Char('\n') => match self.files[self.selected()].clone() { FsEntry::Dir(p, _) => { self.path = p; refresh_directory(self); } FsEntry::File(p, _) => open_file(&p), }, - KeyCode::Left => { + Key::Left => { self.path.pop(); refresh_directory(self); } From 8c4db83dfbb186fb95227a02dba67fc542205a35 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 21:50:50 +0000 Subject: [PATCH 4/4] Bump dependency version --- Cargo.toml | 2 +- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a5d298d..405ae4d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" [dependencies] colored = "2" -mosaic-tile = "0.2" +mosaic-tile = "0.4" pretty-bytes = "0.2" [profile.release] diff --git a/src/main.rs b/src/main.rs index beb57e39..e63cd822 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod state; use colored::*; -use mosaic_tile::register_tile; +use mosaic_tile::*; use state::{FsEntry, State}; use std::{cmp::min, fs::read_dir};