This commit is contained in:
Vivian Lim 2024-04-06 23:31:51 -07:00
commit 37e332e0fc
9 changed files with 1390 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1011
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "zandatsu"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.81"
clap = { version = "4.5.4", features = ["derive"] }
color-eyre = "0.6.3"
crossterm = "0.27.0"
fuzzy-select = "0.1.1"
ratatui = "0.26.1"

150
flake.lock Normal file
View File

@ -0,0 +1,150 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1710656467,
"narHash": "sha256-4Plj0vNP+ckWVNi6EtVojL9YV2dwSH7H4UMFCV40VE8=",
"owner": "nix-community",
"repo": "fenix",
"rev": "c53bb4a32f2fce7acf4e8e160a54779c4460ffdb",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixGL": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1685908677,
"narHash": "sha256-E4zUPEUFyVWjVm45zICaHRpfGepfkE9Z2OECV9HXfA4=",
"owner": "guibou",
"repo": "nixGL",
"rev": "489d6b095ab9d289fe11af0219a9ff00fe87c7c5",
"type": "github"
},
"original": {
"owner": "guibou",
"repo": "nixGL",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1660551188,
"narHash": "sha256-a1LARMMYQ8DPx1BgoI/UN4bXe12hhZkCNqdxNi6uS0g=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "441dc5d512153039f19ef198e662e4f3dbb9fd65",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1706367331,
"narHash": "sha256-AqgkGHRrI6h/8FWuVbnkfFmXr4Bqsr4fV23aISqj/xg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "160b762eda6d139ac10ae081f8f78d640dd523eb",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"nixGL": "nixGL",
"nixpkgs": "nixpkgs_2",
"utils": "utils"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1710610549,
"narHash": "sha256-xFIGLn5u+msUazlLbdjZ3gQgXrt7Lrlhq+XXUH0XU/0=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "5ecace48f693afaa6adf8cb23086b651db3aec96",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

37
flake.nix Normal file
View File

@ -0,0 +1,37 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
nixGL.url = "github:guibou/nixGL";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, utils, fenix, nixGL }:
utils.lib.eachDefaultSystem (system:
let
pkgs = (import nixpkgs) {
inherit system;
overlays = [ nixGL.overlay fenix.overlays.default ];
};
toolchain = fenix.packages.x86_64-linux.minimal.toolchain;
in {
devShell = with pkgs;
mkShell rec {
nativeBuildInputs = [
(pkgs.fenix.complete.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
])
pkgs.rust-analyzer
];
};
});
}

7
shell.nix Normal file
View File

@ -0,0 +1,7 @@
(import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).shellNix

74
src/main.rs Normal file
View File

@ -0,0 +1,74 @@
use std::io;
use anyhow::Result;
use clap::{command, Parser, Subcommand};
use fuzzy_select::FuzzySelect;
use crate::vertical::Line;
pub mod vertical;
pub mod refinement;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
source: Source,
}
#[derive(Subcommand, Debug)]
enum Source {
/// Adds files to myapp
Stdin,
Clipboard,
}
fn main() -> Result<()>{
let cli = Cli::parse();
let input = match &cli.source {
Source::Stdin => io::stdin().lines().filter_map(|l| {
match l {
Ok(s) => Some(s),
_ => None
}
}) ,
Source::Clipboard => todo!(),
};
let input: Vec<Line> = input.enumerate().map(|(index, line)| Line {
index,
line
}).collect();
let v = vertical::vertical(&input)?;
let result = refinement::refine(v, &input)?;
println!("result: {}", result);
// let initial = match &cli.source {
// &Source::Stdin => {
// let lines_iter = io::stdin().lines().filter_map(|l| {
// match l {
// Ok(s) => Some(s),
// _ => None
// }
// });
// FuzzySelect::new()
// .with_prompt("Choose line")
// .with_options_from_iter(lines_iter)
// .select()
// },
// Commands::Clipboard => todo!()
// };
// let item = FuzzySelect::new()
// .with_prompt("Choose:")
// .with_
// .with_options(items)
// .select()?;
Ok(())
}

53
src/refinement.rs Normal file
View File

@ -0,0 +1,53 @@
use std::{fmt::Display, io::stdout};
use crossterm::{event::{read, Event, KeyCode, KeyEvent, KeyModifiers}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand};
use fuzzy_select::{FuzzySelect, Select};
use anyhow::Result;
use ratatui::{backend::CrosstermBackend, widgets::{Block, Borders, Paragraph}, Terminal};
use crate::vertical::Line;
struct RefinementState<'a> {
text: String,
selection: &'a Line,
input: &'a[Line],
previous: Option<&'a RefinementState<'a>>,
}
pub fn refine(selected: Line, input: &[Line]) -> Result<String> {
let mut state = RefinementState {
text: selected.line.clone(),
selection: &selected,
input,
previous: None
};
enable_raw_mode()?;
stdout().execute(EnterAlternateScreen);
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
loop {
terminal.draw(|frame| {
frame.render_widget(Paragraph::new(state.text.clone()).block(Block::default().title("Content").borders(Borders::ALL)), frame.size() )
})?;
match read() {
Ok(Event::Key(ke)) => {
match (ke.code, ke.modifiers) {
(KeyCode::Enter, KeyModifiers::NONE) => {
break;
}
,
_ => {}
}
},
Ok(_) => (),
Err(_) => todo!(),
}
}
disable_raw_mode()?;
stdout().execute(LeaveAlternateScreen)?;
Ok(state.text)
}

43
src/vertical.rs Normal file
View File

@ -0,0 +1,43 @@
use std::fmt::Display;
use crossterm::event::{read, Event};
use fuzzy_select::{FuzzySelect, Select};
use anyhow::Result;
#[derive(Clone)]
pub struct Line {
pub index: usize,
pub line: String
}
impl Display for Line {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.line);
Ok(())
}
}
impl Select for Line {
fn search_content(&self) -> &str {
&self.line
}
fn render_before_content(&self) -> Option<impl std::fmt::Display + '_> {
Some(format!("{} ", self.index + 1))
}
fn render_after_content(&self) -> Option<impl std::fmt::Display + '_> {
None::<Self>
}
}
pub fn vertical(input: &[Line]) -> Result<Line> {
let selected = FuzzySelect::new()
.with_prompt("Choose line")
.with_options_from_slice(input)
.select()?;
Ok(selected)
}