Initial rewrite commit

This commit is contained in:
Joshua Barretto 2024-02-09 11:03:33 +00:00
commit f7943884cb
12 changed files with 1417 additions and 0 deletions

512
Cargo.lock generated Normal file
View file

@ -0,0 +1,512 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2faccea4cc4ab4a667ce676a30e8ec13922a692c99bb8f5b11f1502c72e04220"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "crossterm"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [
"bitflags 2.4.2",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
dependencies = [
"winapi",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "mio"
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets 0.48.5",
]
[[package]]
name = "proc-macro2"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "slotmap"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a"
dependencies = [
"version_check",
]
[[package]]
name = "smallvec"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
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 = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "zte2"
version = "0.2.0"
dependencies = [
"clap",
"crossterm",
"slotmap",
"thiserror",
]

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "zte2"
version = "0.2.0"
edition = "2021"
[dependencies]
clap = { version = "4.4", features = ["derive"] }
slotmap = "1.0"
crossterm = "0.27"
thiserror = "1.0"
[profile.dev]
opt-level = 2

114
src/action.rs Normal file
View file

@ -0,0 +1,114 @@
use crate::{
state::State,
terminal::TerminalEvent,
};
use crossterm::event::{KeyEvent, KeyCode, KeyEventKind, KeyModifiers};
#[derive(Clone, Debug)]
pub enum Dir {
Left,
Right,
Up,
Down,
}
#[derive(Clone, Debug)]
pub enum Action {
Char(char), // Insert a character
Backspace, // Backspace a character
Move(Dir), // Move the cursor
Cancel, // Cancels the current context
Go, // Search, accept, or select the current option
Quit, // Quit the application
OpenPrompt, // Open the command prompt
Show(String), // Display some arbitrary text to the user
}
pub enum Event {
// The incoming event is an action generated by some other internal component.
Action(Action),
// The incoming event is a raw user input.
Raw(RawEvent),
}
impl Event {
pub fn from_raw(e: TerminalEvent) -> Self {
Self::Raw(RawEvent(e))
}
/// Turn the event into an action (if possible).
///
/// The translation function allows elements to translate raw events into their own context-specific actions.
pub fn to_action(&self, translate: impl FnOnce(&RawEvent) -> Option<Action>) -> Option<Action> {
match self {
Self::Action(a) => Some(a.clone()),
Self::Raw(te) => translate(te),
}
}
}
pub struct RawEvent(TerminalEvent);
impl RawEvent {
pub fn to_char(&self) -> Option<char> {
match &self.0 {
TerminalEvent::Key(KeyEvent {
code,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) => match code {
KeyCode::Char(c) => Some(*c),
KeyCode::Backspace => Some('\x08'),
KeyCode::Enter => Some('\n'),
_ => None,
},
_ => None,
}
}
pub fn to_move(&self) -> Option<Dir> {
match &self.0 {
TerminalEvent::Key(KeyEvent {
code,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press | KeyEventKind::Repeat,
..
}) => match code {
KeyCode::Left => Some(Dir::Left),
KeyCode::Right => Some(Dir::Right),
KeyCode::Up => Some(Dir::Up),
KeyCode::Down => Some(Dir::Down),
_ => None,
},
_ => None,
}
}
pub fn is_go(&self) -> bool {
matches!(&self.0, TerminalEvent::Key(KeyEvent {
code: KeyCode::Enter,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
..
}))
}
pub fn is_prompt(&self) -> bool {
matches!(&self.0, TerminalEvent::Key(KeyEvent {
code: KeyCode::Enter,
modifiers: KeyModifiers::ALT,
kind: KeyEventKind::Press,
..
}))
}
pub fn is_cancel(&self) -> bool {
matches!(&self.0, TerminalEvent::Key(KeyEvent {
code: KeyCode::Esc,
modifiers: KeyModifiers::NONE,
kind: KeyEventKind::Press,
..
}))
}
}

58
src/main.rs Normal file
View file

@ -0,0 +1,58 @@
mod action;
mod ui;
mod terminal;
mod state;
use crate::{
terminal::{Terminal, TerminalEvent, Color},
action::{Event, Action, Dir},
state::State,
ui::{Element as _, Visual as _},
};
use clap::Parser;
use std::{
path::PathBuf,
time::Duration,
io,
};
#[derive(Parser, Debug)]
struct Args {
paths: Vec<PathBuf>,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("io: {0}")]
Io(#[from] io::Error),
}
fn main() -> Result<(), Error> {
let args = Args::parse();
println!("{args:?}");
let mut state = State::try_from(args)?;
let mut ui = ui::Root::new(&state);
Terminal::with(move |term| {
loop {
// Render the state to the screen
term.update(|fb| ui.render(&state, fb));
// Wait for a while
term.wait_at_least(Duration::from_millis(250));
state.tick();
while let Some(ev) = term.get_event() {
// Resize events are special and need handling by the terminal
if let TerminalEvent::Resize(cols, rows) = ev { term.set_size([cols, rows]); }
// Have the UI handle events
if ui
.handle(Event::from_raw(ev))
.map_or(false, |r| r.should_end())
{ return Ok(()); }
}
}
})
}

79
src/state.rs Normal file
View file

@ -0,0 +1,79 @@
use crate::{
ui::{self, Resp, Element as _},
Action, Event, Args, Error, Color,
};
use crossterm::event::{KeyCode, KeyModifiers, KeyEvent, KeyEventKind};
use slotmap::{HopSlotMap, new_key_type};
use std::collections::HashMap;
new_key_type! {
// Per-activity
pub struct ViewId;
pub struct ActivityId;
}
pub struct Cursor {
base: (usize, usize),
pos: (usize, usize),
}
pub struct FileView {
line: usize,
cursor: Cursor,
// For searches
// view_cursor: Option<Cursor>,
}
pub struct File {
views: HopSlotMap<ViewId, FileView>,
}
pub struct ConsoleView {
line: usize,
}
pub struct Console {
views: HopSlotMap<ViewId, ConsoleView>,
}
pub enum Activity {
File(File),
Console(Console),
}
pub struct Theme {
pub ui_bg: Color,
pub status_bg: Color,
}
impl Default for Theme {
fn default() -> Self {
Self {
ui_bg: Color::AnsiValue(235),
status_bg: Color::AnsiValue(23),
}
}
}
pub struct State {
pub activities: HopSlotMap<ActivityId, Activity>,
pub tick: u64,
pub theme: Theme,
}
impl TryFrom<Args> for State {
type Error = Error;
fn try_from(args: Args) -> Result<Self, Self::Error> {
Ok(Self {
activities: HopSlotMap::default(),
tick: 0,
theme: Theme::default(),
})
}
}
impl State {
pub fn tick(&mut self) {
self.tick += 1;
}
}

265
src/terminal.rs Normal file
View file

@ -0,0 +1,265 @@
use super::Error;
pub use crossterm::{
cursor::SetCursorStyle as CursorStyle,
event::Event as TerminalEvent,
style::Color,
};
use std::{
io::{self, StdoutLock, Write as _},
panic,
time::Duration,
borrow::Borrow,
};
use crossterm::{
event,
style,
cursor,
terminal,
ExecutableCommand,
QueueableCommand,
SynchronizedUpdate,
};
#[derive(Copy, Clone, PartialEq)]
struct Cell {
c: char,
fg: Color,
bg: Color,
}
impl Default for Cell {
fn default() -> Self {
Self {
c: ' ',
fg: Color::Reset,
bg: Color::Reset,
}
}
}
pub struct Rect<'a> {
fg: Color,
bg: Color,
origin: [u16; 2],
size: [u16; 2],
fb: &'a mut Framebuffer,
}
impl<'a> Rect<'a> {
fn get_mut(&mut self, pos: [usize; 2]) -> Option<&mut Cell> {
if pos[0] < self.size()[0] && pos[1] < self.size()[1] {
let offs = [self.origin[0] as usize + pos[0], self.origin[1] as usize + pos[1]];
Some(&mut self.fb.cells[offs[1] * self.fb.size[0] as usize + offs[0]])
} else {
None
}
}
pub fn with<R>(&mut self, f: impl FnOnce(&mut Rect) -> R) -> R { f(self) }
pub fn rect(&mut self, origin: [usize; 2], size: [usize; 2]) -> Rect {
Rect {
origin: [self.origin[0] + origin[0] as u16, self.origin[1] + origin[1] as u16],
size: [
size[0].min((self.size[0] as usize).saturating_sub(origin[0])) as u16,
size[1].min((self.size[1] as usize).saturating_sub(origin[1])) as u16,
],
fg: self.fg,
bg: self.bg,
fb: self.fb,
}
}
pub fn with_fg(&mut self, fg: Color) -> Rect {
Rect { fg, bg: self.bg, origin: self.origin, size: self.size, fb: self.fb }
}
pub fn with_bg(&mut self, bg: Color) -> Rect {
Rect { fg: self.fg, bg, origin: self.origin, size: self.size, fb: self.fb }
}
pub fn size(&self) -> [usize; 2] { self.size.map(|e| e as usize) }
pub fn fill(&mut self, c: char) -> &mut Self {
for row in 0..self.size()[1] {
for col in 0..self.size()[0] {
let cell = Cell { c, fg: self.fg, bg: self.bg };
if let Some(c) = self.get_mut([col, row]) { *c = cell; }
}
}
self
}
pub fn text<C: Borrow<char>>(&mut self, origin: [usize; 2], text: impl IntoIterator<Item = C>) -> &mut Self {
for (idx, c) in text.into_iter().enumerate() {
if origin[0] + idx >= self.size()[0] {
break;
} else {
let cell = Cell { c: *c.borrow(), fg: self.fg, bg: self.bg };
if let Some(c) = self.get_mut([origin[0] + idx, origin[1]]) { *c = cell; }
}
}
self
}
pub fn set_cursor(&mut self, cursor: [usize; 2], style: CursorStyle) -> &mut Self {
self.fb.cursor = Some((
[self.origin[0] + cursor[0] as u16, self.origin[1] + cursor[1] as u16],
style,
));
self
}
}
#[derive(Default)]
pub struct Framebuffer {
size: [u16; 2],
cells: Vec<Cell>,
cursor: Option<([u16; 2], CursorStyle)>,
}
impl Framebuffer {
pub fn rect(&mut self) -> Rect {
Rect {
fg: Color::Reset,
bg: Color::Reset,
origin: [0, 0],
size: self.size,
fb: self,
}
}
}
pub struct Terminal<'a> {
stdout: StdoutLock<'a>,
size: [u16; 2],
fb: [Framebuffer; 2],
}
impl<'a> Terminal<'a> {
fn enter(mut stdout: impl io::Write) {
let _ = terminal::enable_raw_mode();
let _ = stdout.execute(terminal::EnterAlternateScreen);
}
fn leave(mut stdout: impl io::Write) {
let _ = terminal::disable_raw_mode();
let _ = stdout.execute(terminal::LeaveAlternateScreen);
let _ = stdout.execute(cursor::Show);
}
pub fn with<T>(f: impl FnOnce(&mut Self) -> Result<T, Error> + panic::UnwindSafe) -> Result<T, Error> {
let size = terminal::window_size()?;
Self::enter(io::stdout().lock());
let mut this = Self {
stdout: io::stdout().lock(),
size: [size.columns, size.rows],
fb: [Framebuffer::default(), Framebuffer::default()],
};
let hook = panic::take_hook();
panic::set_hook(Box::new(move |panic| { Self::leave(io::stdout().lock()); hook(panic); }));
let res = f(&mut this);
Self::leave(io::stdout().lock());
res
}
pub fn set_size(&mut self, size: [u16; 2]) {
self.size = size;
}
pub fn update(&mut self, render: impl FnOnce(&mut Rect)) {
// Reset framebuffer
if self.fb[0].size != self.size {
self.fb[0].size = self.size;
self.fb[0].cells.resize(self.size[0] as usize * self.size[1] as usize, Cell::default());
}
self.fb[0].cursor = None;
render(&mut self.fb[0].rect());
self.stdout.sync_update(|stdout| {
let mut cursor_pos = [0, 0];
let mut fg = Color::Reset;
let mut bg = Color::Reset;
stdout
.queue(cursor::MoveTo(cursor_pos[0], cursor_pos[1])).unwrap()
.queue(style::SetForegroundColor(fg)).unwrap()
.queue(style::SetBackgroundColor(bg)).unwrap()
.queue(cursor::Hide).unwrap();
// Write out changes
for row in 0..self.size[1] {
for col in 0..self.size[0] {
let pos = row as usize * self.size[0] as usize + col as usize;
let cell = self.fb[0].cells[pos];
let changed = self.fb[0].size != self.fb[1].size
|| cell != self.fb[1].cells[pos];
if changed {
if cursor_pos != [col, row] {
// Minimise the work done to move the cursor around
if cursor_pos[1] == row {
stdout.queue(cursor::MoveToColumn(col)).unwrap();
} else if cursor_pos[0] == col {
stdout.queue(cursor::MoveToRow(row)).unwrap();
} else {
stdout.queue(cursor::MoveTo(col, row)).unwrap();
}
cursor_pos = [col, row];
}
if fg != cell.fg {
fg = cell.fg;
stdout.queue(style::SetForegroundColor(fg)).unwrap();
}
if bg != cell.bg {
bg = cell.bg;
stdout.queue(style::SetBackgroundColor(bg)).unwrap();
}
stdout.queue(style::Print(self.fb[0].cells[pos].c)).unwrap();
// Move cursor
cursor_pos[0] += 1;
if cursor_pos[0] >= self.size[0] { cursor_pos = [0, cursor_pos[1] + 1]; }
}
}
}
if let Some(([col, row], style)) = self.fb[0].cursor {
stdout
.queue(cursor::MoveTo(col, row)).unwrap()
.queue(style).unwrap()
.queue(cursor::Show).unwrap();
} else {
stdout.queue(cursor::Hide).unwrap();
}
}).unwrap();
self.stdout.flush().unwrap();
// Switch front and back buffers
self.fb.swap(0, 1);
}
// Get the next pending event, if one is available.
pub fn get_event(&mut self) -> Option<TerminalEvent> {
if event::poll(Duration::ZERO).ok()? {
event::read().ok()
} else {
None
}
}
// Wait for the given duration or until an event arrives.
pub fn wait_at_least(&mut self, dur: Duration) {
event::poll(dur).unwrap();
}
}

58
src/ui/input.rs Normal file
View file

@ -0,0 +1,58 @@
use super::*;
use crate::terminal::CursorStyle;
#[derive(Default)]
pub struct Input {
pub text: Vec<char>,
pub cursor: usize,
pub preamble: &'static str,
}
impl Input {
pub fn get_text(&self) -> String {
self.text.iter().copied().collect()
}
}
impl Element for Input {
fn handle(&mut self, event: Event) -> Result<Resp, Event> {
match event.to_action(|e| e.to_char().map(Action::Char)
.or_else(|| e.to_move().map(Action::Move))) {
Some(Action::Char('\x08')) => {
self.cursor = self.cursor.saturating_sub(1);
if self.text.len() > self.cursor {
self.text.remove(self.cursor);
}
Ok(Resp::handled(None))
},
Some(Action::Char(c)) => {
self.text.insert(self.cursor, c);
self.cursor += 1;
Ok(Resp::handled(None))
},
Some(Action::Move(Dir::Left)) => {
self.cursor = self.cursor.saturating_sub(1);
Ok(Resp::handled(None))
},
Some(Action::Move(Dir::Right)) => {
self.cursor = (self.cursor + 1).min(self.text.len());
Ok(Resp::handled(None))
},
_ => Err(event),
}
}
}
impl Visual for Input {
fn render(&self, state: &State, frame: &mut Rect) {
frame.with(|frame| {
frame.fill(' ');
frame.text([0, 0], self.preamble.chars());
frame.rect([self.preamble.chars().count(), 0], frame.size()).with(|frame| {
frame.text([0, 0], &self.text);
frame.set_cursor([self.cursor, 0], CursorStyle::BlinkingBar);
});
});
}
}

85
src/ui/mod.rs Normal file
View file

@ -0,0 +1,85 @@
mod prompt;
mod root;
mod panes;
mod status;
mod input;
pub use self::{
prompt::{Prompt, Confirm, Show},
panes::Panes,
status::Status,
root::Root,
input::Input,
};
use crate::{
terminal::{Rect, Color},
State, Action, Event, Dir,
};
pub enum CannotEnd {}
pub struct CanEnd;
pub struct Resp<CanEnd = CannotEnd> {
should_end: Option<CanEnd>,
pub action: Option<Action>,
}
impl Resp<CanEnd> {
pub fn end(action: impl Into<Option<Action>>) -> Self {
Self {
should_end: Some(CanEnd),
action: action.into(),
}
}
pub fn should_end(&self) -> bool { self.should_end.is_some() }
}
impl<T> Resp<T> {
pub fn handled(action: impl Into<Option<Action>>) -> Self {
Self {
should_end: None,
action: action.into(),
}
}
pub fn into_can_end(self) -> Resp<CanEnd> {
Resp {
should_end: None,
action: self.action,
}
}
}
pub trait Element<CanEnd = CannotEnd> {
/// Attempt to handle an event.
///
/// If handled, convert into a series of secondary actions.
/// If unhandled, return the original event to be handled by a lower element.
fn handle(&mut self, event: Event) -> Result<Resp<CanEnd>, Event>;
}
pub trait Visual {
fn render(&self, state: &State, frame: &mut Rect);
}
pub struct Label(String);
impl std::ops::Deref for Label {
type Target = String;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl Visual for Label {
fn render(&self, state: &State, frame: &mut Rect) {
frame
.with_bg(state.theme.ui_bg)
.fill(' ')
.with(|frame| {
for (idx, line) in self.lines().enumerate() {
frame.text([0, idx], line.chars());
}
});
}
}

3
src/ui/panes.rs Normal file
View file

@ -0,0 +1,3 @@
use super::*;
pub struct Panes;

124
src/ui/prompt.rs Normal file
View file

@ -0,0 +1,124 @@
use super::*;
use std::str::FromStr;
pub enum Command {
Quit,
Help,
Version,
}
impl FromStr for Command {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"q" | "quit" => Ok(Command::Quit),
"version" => Ok(Command::Version),
"?" | "help" => Ok(Command::Help),
_ => Err(()),
}
}
}
pub struct Prompt {
pub input: Input,
}
impl Prompt {
pub fn get_action(&self) -> Option<Action> {
match self.input.get_text().as_str() {
"quit" => Some(Action::Quit),
"version" => Some(Action::Show(format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")))),
"help" => Some(Action::Show(format!("Temporary help info:\n- quit\n- version\n- help"))),
_ => None,
}
}
}
impl Element<CanEnd> for Prompt {
fn handle(&mut self, event: Event) -> Result<Resp<CanEnd>, Event> {
match event.to_action(|e| if e.is_cancel() {
Some(Action::Cancel)
} else if e.is_go() {
Some(Action::Go)
} else if e.is_prompt() {
Some(Action::OpenPrompt)
} else {
None
}) {
Some(Action::Cancel /*| Action::Prompt*/) => Ok(Resp::end(None)),
Some(Action::Go) => if let Some(action) = self.get_action() {
Ok(Resp::end(action))
} else {
Ok(Resp::end(Action::Show(format!("unknown command `{}`", self.input.get_text()))))
},
_ => self.input.handle(event).map(Resp::into_can_end),
}
}
}
impl Visual for Prompt {
fn render(&self, state: &State, frame: &mut Rect) {
frame
.rect([0, frame.size()[1].saturating_sub(1)], [frame.size()[0], 1])
.with_bg(state.theme.status_bg)
.with(|f| self.input.render(state, f));
}
}
pub struct Show {
pub label: Label,
}
impl Element<CanEnd> for Show {
fn handle(&mut self, event: Event) -> Result<Resp<CanEnd>, Event> {
match event.to_action(|e| if e.is_cancel() {
Some(Action::Cancel)
} else {
None
}) {
Some(Action::Cancel) => Ok(Resp::end(None)),
_ => Err(event),
}
}
}
impl Visual for Show {
fn render(&self, state: &State, frame: &mut Rect) {
let lines = self.label.lines().count();
self.label.render(
state,
&mut frame.rect([0, frame.size()[1].saturating_sub(1 + lines)], [frame.size()[0], lines]),
);
}
}
pub struct Confirm {
pub label: Label,
pub action: Action,
}
impl Element<CanEnd> for Confirm {
fn handle(&mut self, event: Event) -> Result<Resp<CanEnd>, Event> {
match event.to_action(|e| if e.is_cancel() || e.to_char() == Some('n') {
Some(Action::Cancel)
} else if e.to_char() == Some('y') {
Some(Action::Go)
} else {
None
}) {
Some(Action::Go) => Ok(Resp::end(Some(self.action.clone()))),
Some(Action::Cancel) => Ok(Resp::end(None)),
_ => Err(event),
}
}
}
impl Visual for Confirm {
fn render(&self, state: &State, frame: &mut Rect) {
let lines = self.label.lines().count();
self.label.render(
state,
&mut frame.rect([0, frame.size()[1].saturating_sub(1 + lines)], [frame.size()[0], lines]),
);
}
}

103
src/ui/root.rs Normal file
View file

@ -0,0 +1,103 @@
use super::*;
pub struct Root {
panes: Panes,
status: Status,
tasks: Vec<Task>,
}
pub enum Task {
Prompt(Prompt),
Show(Show),
Confirm(Confirm),
}
impl Root {
pub fn new(state: &State) -> Self {
Self {
panes: Panes,
status: Status,
tasks: Vec::new(),
}
}
}
impl Element<CanEnd> for Root {
fn handle(&mut self, mut event: Event) -> Result<Resp<CanEnd>, Event> {
// Pass the event down through the list of tasks until we meet one that can handle it
let mut task_idx = self.tasks.len();
let action = loop {
task_idx = match task_idx.checked_sub(1) {
Some(task_idx) => task_idx,
None => break event.to_action(|e| if e.is_prompt() {
Some(Action::OpenPrompt)
} else if e.is_cancel() {
Some(Action::Cancel)
} else {
None
}),
};
let res = match &mut self.tasks[task_idx] {
Task::Prompt(p) => p.handle(event),
Task::Show(s) => s.handle(event),
Task::Confirm(c) => c.handle(event),
};
match res {
Ok(resp) => {
// If the task has requested that it should end, kill it and all of its children
if resp.should_end() {
self.tasks.truncate(task_idx);
}
break resp.action;
}
Err(e) => event = e,
}
};
// Handle 'top-level' actions
if let Some(action) = action {
match action {
Action::OpenPrompt => {
self.tasks.clear(); // Prompt overrides all
self.tasks.push(Task::Prompt(Prompt {
input: Input { preamble: "> ", ..Input::default() },
}));
},
Action::Cancel => self.tasks.push(Task::Confirm(Confirm {
label: Label("Are you sure you wish to quit? (y/n)".to_string()),
action: Action::Quit,
})),
Action::Show(text) => self.tasks.push(Task::Show(Show { label: Label(text) })),
Action::Quit => return Ok(Resp::end(None)),
action => todo!("Unhandled action {action:?}"),
}
}
// Root element swallows all other events
Ok(Resp::handled(None))
}
}
impl Visual for Root {
fn render(&self, state: &State, frame: &mut Rect) {
frame
.with_bg(Color::Black)
.fill(' ');
// Display status bar
frame
.rect([0, frame.size()[1].saturating_sub(1)], [frame.size()[0], 1])
.with_bg(state.theme.status_bg)
.fill(' ');
if let Some(task) = self.tasks.last() {
match task {
Task::Prompt(p) => p.render(state, frame),
Task::Show(s) => s.render(state, frame),
Task::Confirm(c) => c.render(state, frame),
}
}
}
}

3
src/ui/status.rs Normal file
View file

@ -0,0 +1,3 @@
use super::*;
pub struct Status;