-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SUPER PRE ALPHA BETA Not ready to use at ALL (even if 2-3 things works)
- Loading branch information
Wyene
committed
Feb 26, 2024
1 parent
362925d
commit 7b79203
Showing
9 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "shell" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
serde = { version = "1.0.152", features = ["derive"] } | ||
toml = "0.8.10" | ||
termion = "3.0.0" | ||
rustyline = {version= "13.0.0", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[style] | ||
prompt=">" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::collections::HashMap; | ||
|
||
pub fn translate_to_color(mut cli: String) -> String { | ||
let mut colors_map = HashMap::new(); | ||
colors_map.insert("(black)", "\x1b[30m"); | ||
colors_map.insert("(red)", "\x1b[31m"); | ||
colors_map.insert("(green)", "\x1b[32m"); | ||
colors_map.insert("(yellow)", "\x1b[33m"); | ||
colors_map.insert("(blue)", "\x1b[34m"); | ||
colors_map.insert("(magenta)", "\x1b[35m"); | ||
colors_map.insert("(cyan)", "\x1b[36m"); | ||
colors_map.insert("(white)", "\x1b[37m"); | ||
colors_map.insert("(default)", "\x1b[39m"); | ||
colors_map.insert("(bold)", "\x1b[1m"); | ||
colors_map.insert("(underline)", "\x1b[4m"); | ||
colors_map.insert("(blink)", "\x1b[5m"); | ||
colors_map.insert("(reverse)", "\x1b[7m"); | ||
colors_map.insert("(hidden)", "\x1b[8m"); | ||
colors_map.insert("(black_bg)", "\x1b[40m"); | ||
colors_map.insert("(red_bg)", "\x1b[41m"); | ||
colors_map.insert("(green_bg)", "\x1b[42m"); | ||
colors_map.insert("(yellow_bg)", "\x1b[43m"); | ||
colors_map.insert("(blue_bg)", "\x1b[44m"); | ||
colors_map.insert("(magenta_bg)", "\x1b[45m"); | ||
colors_map.insert("(cyan_bg)", "\x1b[46m"); | ||
colors_map.insert("(white_bg)", "\x1b[47m"); | ||
colors_map.insert("(default_bg)", "\x1b[49m"); | ||
colors_map.insert("(reset)", "\x1b[0m"); | ||
|
||
cli.push_str("(reset)"); | ||
|
||
for key in colors_map.keys() { | ||
cli = cli.replace(key, colors_map.get(key).unwrap()); | ||
} | ||
|
||
cli | ||
} | ||
|
||
pub fn prompt_var(mut cli: String, env: &HashMap<String, String>) -> String { | ||
for (key, value) in env { | ||
cli = cli.replace(&format!("({})", key.to_lowercase()), &value); | ||
} | ||
|
||
cli | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::fs; | ||
use std::fmt; | ||
use std::collections::HashMap; | ||
|
||
use crate::r#struct::programming_language; | ||
|
||
fn detect_rust() -> programming_language { | ||
if fs::metadata("Cargo.toml").is_ok() { | ||
return programming_language::Rust; | ||
} | ||
programming_language::Unknown | ||
} | ||
|
||
fn detect_go() -> programming_language { | ||
if fs::metadata("go.mod").is_ok() { | ||
return programming_language::Go; | ||
} | ||
programming_language::Unknown | ||
} | ||
|
||
fn detect_node() -> programming_language { | ||
if fs::metadata("package.json").is_ok() { | ||
return programming_language::Node; | ||
} | ||
programming_language::Unknown | ||
} | ||
|
||
fn detect_python() -> programming_language { | ||
if fs::metadata("requirements.txt").is_ok() || fs::metadata("pyproject.toml").is_ok() || fs::metadata("main.py").is_ok() { | ||
return programming_language::Python; | ||
} | ||
programming_language::Unknown | ||
} | ||
|
||
pub fn detect_work_env() -> String { | ||
|
||
let mut envs = HashMap::new(); | ||
envs.insert(programming_language::Rust.to_string(), detect_rust()); | ||
envs.insert(programming_language::Go.to_string(), detect_go()); | ||
envs.insert(programming_language::Python.to_string(), detect_python()); | ||
envs.insert(programming_language::Node.to_string(), detect_node()); | ||
|
||
for (env, language) in envs { | ||
if language != programming_language::Unknown { | ||
return env.to_string(); | ||
} | ||
} | ||
"Unknown".to_string() | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::collections::HashSet; | ||
|
||
use rustyline::hint::{Hint, Hinter}; | ||
use rustyline::history::DefaultHistory; | ||
use rustyline::Context; | ||
use rustyline::{Completer, Helper, Highlighter, Validator}; | ||
use rustyline::{Editor, Result}; | ||
|
||
#[derive(Completer, Helper, Validator, Highlighter, Clone)] | ||
pub struct DIYHinter { | ||
// It's simple example of rustyline, for more efficient, please use ** radix trie ** | ||
pub hints: HashSet<CommandHint>, | ||
} | ||
|
||
#[derive(Hash, Debug, PartialEq, Eq, Clone)] | ||
pub struct CommandHint { | ||
display: String, | ||
complete_up_to: usize, | ||
} | ||
|
||
impl Hint for CommandHint { | ||
fn display(&self) -> &str { | ||
&self.display | ||
} | ||
|
||
fn completion(&self) -> Option<&str> { | ||
if self.complete_up_to > 0 { | ||
Some(&self.display[..self.complete_up_to]) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl DIYHinter { | ||
pub fn new(hints: HashSet<CommandHint>) -> DIYHinter { | ||
DIYHinter { hints } | ||
} | ||
|
||
pub fn add_hint(&mut self, hint: CommandHint) { | ||
self.hints.insert(hint); | ||
} | ||
} | ||
|
||
impl CommandHint { | ||
pub fn new(text: &str, complete_up_to: &str) -> CommandHint { | ||
assert!(text.starts_with(complete_up_to)); | ||
CommandHint { | ||
display: text.into(), | ||
complete_up_to: complete_up_to.len(), | ||
} | ||
} | ||
|
||
fn suffix(&self, strip_chars: usize) -> CommandHint { | ||
CommandHint { | ||
display: self.display[strip_chars..].to_owned(), | ||
complete_up_to: self.complete_up_to.saturating_sub(strip_chars), | ||
} | ||
} | ||
} | ||
|
||
impl Hinter for DIYHinter { | ||
type Hint = CommandHint; | ||
|
||
fn hint(&self, line: &str, pos: usize, _ctx: &Context<'_>) -> Option<CommandHint> { | ||
if line.is_empty() || pos < line.len() { | ||
return None; | ||
} | ||
|
||
self.hints | ||
.iter() | ||
.filter_map(|hint| { | ||
// expect hint after word complete, like redis cli, add condition: | ||
// line.ends_with(" ") | ||
if hint.display.starts_with(line) { | ||
Some(hint.suffix(pos)) | ||
} else { | ||
None | ||
} | ||
}) | ||
.next() | ||
} | ||
} | ||
|
||
pub fn diy_hints() -> HashSet<CommandHint> { | ||
let mut set = HashSet::new(); | ||
set.insert(CommandHint::new("help", "help")); | ||
set.insert(CommandHint::new("get key", "get ")); | ||
set.insert(CommandHint::new("set key value", "set ")); | ||
set.insert(CommandHint::new("hget key field", "hget ")); | ||
set.insert(CommandHint::new("hset key field value", "hset ")); | ||
set | ||
} | ||
|
Oops, something went wrong.