some initial work

This commit is contained in:
Daniel Knuettel 2024-04-15 16:18:26 +02:00
commit f8a029545b
4 changed files with 156 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "make_prompt_rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "make_prompt"
path = "src/make_prompt.rs"

8
source.sh Normal file
View File

@ -0,0 +1,8 @@
bin_path=$(pwd)/target/debug/make_prompt
PROMPT_COMMAND='make_prompt'
make_prompt () {
local prompt_pref=$($bin_path $?)
PS1="$prompt_pref"
}

136
src/make_prompt.rs Normal file
View File

@ -0,0 +1,136 @@
use std::fs::File;
use std::io::Read;
fn fixed_width(input: &String, len: usize) -> String
{
let clen = input.len();
const PLACE_HOLDER: &str = "🤄";
if clen > len
{
let cut_to = len - 2_usize;
let start_from = clen - cut_to;
let cutted_result = &input[start_from..];
let mut result: String = cutted_result.to_owned();
result.push_str(&PLACE_HOLDER);
return result;
}
else
{
let n_pad = len - clen;
let n_pad_left = n_pad / 2;
let n_pad_right = n_pad_left + { if n_pad % 2 == 0 {0} else {1}};
let mut result: String = str::repeat(" ", n_pad_left).to_owned();
result.push_str(input);
result.push_str(&str::repeat(" ", n_pad_right));
return result;
}
}
fn battery_info(width_to: usize) -> String
{
const BATTERY_DIR: &str = "/sys/class/power_supply/BAT0/";
const SYMB_POWER: &str = "";
const SYMB_BATT: &str = "🔋";
let bat_path = std::path::Path::new(BATTERY_DIR);
if !bat_path.exists()
{
return fixed_width(&String::from(SYMB_POWER), width_to);
}
let capacity_path = bat_path.join("capacity");
let mut file = match File::open(&capacity_path) {
Err(why) => panic!("couldn't open {}: {}", capacity_path.display(), why),
Ok(file) => file,
};
let mut percentage = String::new();
let _ = file.read_to_string(&mut percentage);
let status_path = bat_path.join("status");
let mut file = match File::open(&status_path) {
Err(why) => panic!("couldn't open {}: {}", status_path.display(), why),
Ok(file) => file,
};
let mut status = String::new();
let _ = file.read_to_string(&mut status);
if status.contains("Charging") || status.contains("Full")
{
fixed_width(&format!("{SYMB_POWER} {percentage}%"), width_to + 1)
}
else
{
fixed_width(&format!("{SYMB_BATT} {percentage}%"), width_to)
}
}
fn shell_color_str<T: std::fmt::Display>(string_in: &T, color: &str) -> String
{
format!("\x1b[{color}m{string_in}\x1b[00m")
}
fn main()
{
const SH_GREEN: &str = "1;32";
const SH_RED: &str = "01;31";
const SH_YELLOW: &str = "01;33";
const SH_PURPLE: &str = "01;35";
const SH_CYAN: &str = "01;36";
let args: Vec<String> = std::env::args().collect();
let default_lastexit = String::from("0");
let cpu_arch = String::from(std::env::consts::ARCH);
let lastexit = {
if args.len() <= 1
{
&default_lastexit
}
else {
&args[1]
}
};
let cwd = {
if args.len() <= 2
{
let promise = std::env::current_dir();
match promise
{
Ok(v) => v.display().to_string(),
Err(_reason) => panic!("failed to get cwd")
}
}
else
{
args[2].to_string()
}
};
print!("\n");
{
print!("{}{}", shell_color_str(&"┌[", SH_GREEN), battery_info(12));
println!("{}", shell_color_str(&"]", SH_GREEN));
}
{
print!("{}", shell_color_str(&"╞[", SH_GREEN));
println!("{}", shell_color_str(&"]", SH_GREEN));
}
{
print!("{} {}", shell_color_str(&"╞>", SH_GREEN)
, shell_color_str(&cwd, SH_YELLOW));
println!("");
}
{
print!("{} {}", shell_color_str(&"", SH_GREEN), shell_color_str(&"$", SH_PURPLE));
}
}