Skip to content

Commit

Permalink
feat: prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Mar 26, 2021
1 parent 2571367 commit 2a61ec4
Show file tree
Hide file tree
Showing 11 changed files with 575 additions and 1 deletion.
214 changes: 214 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
crossterm = "0.18.2"
36 changes: 36 additions & 0 deletions src/loader/loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::loader::Script;

pub fn load_scripts() -> Vec<Script> {
vec![
Script {
id: 1,
script: "build",
command: "tsc",
},
Script {
id: 2,
script: "build:watch",
command: "tsc -w",
},
Script {
id: 3,
script: "style",
command: "npm run style:format && npm run style:lint",
},
Script {
id: 4,
script: "style:format",
command: "prettier --write \"src/**/*.ts\"",
},
Script {
id: 5,
script: "style:lint",
command: "eslint src --ext .js,.ts --fix",
},
Script {
id: 6,
script: "install:g",
command: "npm run build && npm i -g",
},
]
}
7 changes: 7 additions & 0 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Re-export public API.
pub use script::Script;
pub use loader::load_scripts;

// Private modules.
mod loader;
mod script;
14 changes: 14 additions & 0 deletions src/loader/script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::fmt::{Display, Formatter, Result};

#[derive(Clone, Debug)]
pub struct Script {
pub id: usize,
pub script: &'static str,
pub command: &'static str,
}

impl Display for Script {
fn fmt(&self, writer: &mut Formatter<'_>) -> Result {
write!(writer, "{} {}", self.script, self.command)
}
}
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
mod loader;
mod prompt;

use prompt::{Prompt, SelectPrompt};

fn main() {
println!("Hello, world!");
let options = loader::load_scripts();
let mut prompt = SelectPrompt::new("Pick a script to execute", options, 64);

match prompt.run() {
| Ok(Some(item)) => println!("Executing: {} {}", item.script, item.command),
| Ok(None) => println!("Nothing was picked. Bye!"),
| Err(error) => println!("Oof, something happened: {:?}", error),
}
}
25 changes: 25 additions & 0 deletions src/prompt/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub trait Prompt<T> {
fn run(&mut self) -> std::result::Result<Option<T>, crossterm::ErrorKind>;
fn render(&mut self) -> crossterm::Result<()>;
fn handle(&mut self, event: crossterm::event::KeyEvent);
}

#[derive(Eq, PartialEq, Debug)]
pub enum State {
Created,
Running,
Aborted,
Completed,
}

impl State {
pub fn is_completed(&self) -> bool {
*self == State::Aborted || *self == State::Completed
}
}

impl Default for State {
fn default() -> State {
State::Created
}
}
12 changes: 12 additions & 0 deletions src/prompt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Re-export module public API.
pub use base::{Prompt, State};
pub use select::SelectPrompt;
pub use symbols::Symbols;

// Private modules.
mod base;
mod select;
mod symbols;

// Public modules.
pub mod utils;
Loading

0 comments on commit 2a61ec4

Please sign in to comment.