sqishrc ok
This commit is contained in:
123
src/shell/config.rs
Normal file
123
src/shell/config.rs
Normal file
@ -0,0 +1,123 @@
|
||||
use users::{get_user_by_uid, get_current_uid};
|
||||
use termion::color;
|
||||
use std::{env, fs};
|
||||
use std::collections::HashMap;
|
||||
|
||||
extern crate dirs;
|
||||
|
||||
extern crate yaml_rust;
|
||||
use yaml_rust::{YamlLoader};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqishConf {
|
||||
pub promptline: String,
|
||||
pub promptline_base: String,
|
||||
pub aliases: HashMap<String, String>,
|
||||
pub hotkeys: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl SqishConf {
|
||||
pub fn from_sqishrc() -> Result<SqishConf, String> {
|
||||
let mut conf_path = match dirs::home_dir() {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return Err(String::from("Home dir could not be determined."));
|
||||
},
|
||||
};
|
||||
conf_path.push(".sqishrc.yaml");
|
||||
|
||||
let sqishrc_file = match fs::read_to_string(conf_path) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
return Err(String::from("Could not read ~/.sqishrc.yaml"));
|
||||
},
|
||||
};
|
||||
|
||||
let sqishrc = match YamlLoader::load_from_str(&sqishrc_file) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
return Err(String::from("sqishrc is not valid yaml"));
|
||||
},
|
||||
};
|
||||
|
||||
let sqishrc = &sqishrc[0];
|
||||
let out_str = String::from(sqishrc["prompt"].as_str().unwrap());
|
||||
|
||||
let mut out_conf = SqishConf {
|
||||
promptline: out_str.clone(),
|
||||
promptline_base: out_str,
|
||||
aliases: HashMap::new(),
|
||||
hotkeys: HashMap::new(),
|
||||
};
|
||||
|
||||
out_conf.handle_colors();
|
||||
|
||||
return Ok(out_conf);
|
||||
}
|
||||
|
||||
#[warn(unused_assignments)]
|
||||
pub fn update_prompt(&mut self, hist_nbr: i32) {
|
||||
|
||||
let user = String::from(
|
||||
get_user_by_uid(get_current_uid())
|
||||
.unwrap()
|
||||
.name()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let host = String::from(
|
||||
gethostname::gethostname()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let current_dir = String::from(
|
||||
env::current_dir()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let s = current_dir.split('/');
|
||||
let dir_last = String::from(
|
||||
s.last()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let hist_nbr = hist_nbr.to_string();
|
||||
|
||||
let mut prompt = self.promptline_base.replace("$USER_", &user);
|
||||
prompt = prompt.replace("$HOSTNAME_", &host);
|
||||
prompt = prompt.replace("$DIR_", &dir_last);
|
||||
prompt = prompt.replace("$HISTNUMBER_", &hist_nbr);
|
||||
prompt = prompt.to_owned();
|
||||
self.promptline = prompt;
|
||||
self.handle_colors();
|
||||
}
|
||||
|
||||
|
||||
fn handle_colors(&mut self) {
|
||||
let reset = format!("{}", color::Fg(color::Reset));
|
||||
let green = format!("{}", color::Fg(color::Green));
|
||||
let blue = format!("{}", color::Fg(color::Blue));
|
||||
let red = format!("{}", color::Fg(color::Red));
|
||||
let black = format!("{}", color::Fg(color::Black));
|
||||
let white = format!("{}", color::Fg(color::White));
|
||||
let cyan = format!("{}", color::Fg(color::Cyan));
|
||||
let lightblack = format!("{}", color::Fg(color::LightBlack));
|
||||
|
||||
let mut prompt = self.promptline.replace("$COLORGREEN_", &green);
|
||||
prompt = prompt.replace("$COLORBLUE_", &blue);
|
||||
prompt = prompt.replace("$COLORRED_", &red);
|
||||
prompt = prompt.replace("$COLORBLACK_", &black);
|
||||
prompt = prompt.replace("$COLORWHITE_", &white);
|
||||
prompt = prompt.replace("$COLORCYAN_", &cyan);
|
||||
prompt = prompt.replace("$COLORLBLACK_", &lightblack);
|
||||
prompt = prompt.replace("$COLORRESET_", &reset);
|
||||
let promptown = prompt.to_owned();
|
||||
self.promptline = promptown;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
use users::{get_user_by_uid, get_current_uid};
|
||||
use termion::color;
|
||||
use std::env;
|
||||
|
||||
pub fn build_prompt(curr_number: &i32) -> String {
|
||||
|
||||
let user = String::from(
|
||||
get_user_by_uid(get_current_uid())
|
||||
.unwrap()
|
||||
.name()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let host = String::from(
|
||||
gethostname::gethostname()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let current_dir = String::from(
|
||||
env::current_dir()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let s = current_dir.split('/');
|
||||
let dir_last = String::from(
|
||||
s.last()
|
||||
.unwrap()
|
||||
);
|
||||
|
||||
let prompt = String::from(format!("{}{}{}[{}@{} in {}]{} ",
|
||||
color::Fg(color::LightBlack),
|
||||
curr_number,
|
||||
color::Fg(color::LightCyan),
|
||||
user, host, dir_last,
|
||||
color::Fg(color::Reset)));
|
||||
return prompt;
|
||||
}
|
||||
|
Reference in New Issue
Block a user