From f8a029545b5066e4bf4005ad4cc4d9e4acd39395 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Mon, 15 Apr 2024 16:18:26 +0200 Subject: [PATCH] some initial work --- .gitignore | 1 + Cargo.toml | 11 ++++ source.sh | 8 +++ src/make_prompt.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 source.sh create mode 100644 src/make_prompt.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2539d10 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/source.sh b/source.sh new file mode 100644 index 0000000..433d25f --- /dev/null +++ b/source.sh @@ -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" +} diff --git a/src/make_prompt.rs b/src/make_prompt.rs new file mode 100644 index 0000000..785a69e --- /dev/null +++ b/src/make_prompt.rs @@ -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(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 = 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)); + } +}