Compare commits

..

7 Commits
master ... main

2 changed files with 23 additions and 14 deletions

View File

@ -12,3 +12,4 @@ path = "src/make_prompt.rs"
[dependencies] [dependencies]
git2="*" git2="*"
gethostname="*"

View File

@ -2,6 +2,8 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use std::io::BufReader; use std::io::BufReader;
use std::io::BufRead; use std::io::BufRead;
use gethostname::gethostname;
use std::ffi::OsString;
fn fixed_width(input: &String, len: usize) -> String fn fixed_width(input: &String, len: usize) -> String
{ {
@ -10,9 +12,9 @@ fn fixed_width(input: &String, len: usize) -> String
if clen > len if clen > len
{ {
let cut_to = len - 2_usize; let cut_to = len - 1_usize;
let start_from = clen - cut_to; let start_from = clen - cut_to;
let cutted_result = &input[start_from..]; let cutted_result: String = input.chars().skip(start_from).collect();
let mut result: String = String::from(PLACE_HOLDER); let mut result: String = String::from(PLACE_HOLDER);
result.push_str(&cutted_result); result.push_str(&cutted_result);
@ -52,6 +54,11 @@ fn battery_info(width_to: usize) -> String
let mut percentage = String::new(); let mut percentage = String::new();
let _ = file.read_to_string(&mut percentage); let _ = file.read_to_string(&mut percentage);
if percentage.ends_with('\n')
{
percentage.pop();
}
let status_path = bat_path.join("status"); let status_path = bat_path.join("status");
let mut file = match File::open(&status_path) { let mut file = match File::open(&status_path) {
@ -63,11 +70,11 @@ fn battery_info(width_to: usize) -> String
if status.contains("Charging") || status.contains("Full") if status.contains("Charging") || status.contains("Full")
{ {
fixed_width(&format!("{SYMB_POWER} {percentage}%"), width_to + 1) fixed_width(&format!("{SYMB_POWER} {percentage}%"), width_to)
} }
else else
{ {
fixed_width(&format!("{SYMB_BATT} {percentage}%"), width_to) fixed_width(&format!("{SYMB_BATT} {percentage}%"), width_to - 1)
} }
} }
@ -161,19 +168,19 @@ fn get_editor() -> String
fn shell_color_str<T: std::fmt::Display>(string_in: &T, color: &str) -> String fn shell_color_str<T: std::fmt::Display>(string_in: &T, color: &str) -> String
{ {
format!("\x1b[{color}m{string_in}\x1b[00m") format!("\x01\x1b[{color}m\x02{string_in}\x01\x1b[00m\x02")
} }
fn getenv(varname: &str) -> String fn getenv(varname: &str) -> String
{ {
match std::env::var(varname) match std::env::var(varname)
{ {
Ok(hostname) => hostname, Ok(var) => var,
Err(_) => "".to_string() Err(_err) => {"".to_string()}
} }
} }
fn main() fn main() ->Result<(), OsString>
{ {
const SH_GREEN: &str = "1;32"; const SH_GREEN: &str = "1;32";
const SH_RED: &str = "01;31"; const SH_RED: &str = "01;31";
@ -230,22 +237,22 @@ fn main()
print!("\n"); print!("\n");
{ {
print!("{}{}", shell_color_str(&"┌[", SH_GREEN) print!("{}{}", shell_color_str(&"┌[", SH_GREEN)
, battery_info(12)); , fixed_width(&get_git_branch(& std::path::Path::new(&raw_cwd)), 13));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, fixed_width(&get_git_branch(& std::path::Path::new(&raw_cwd)), 10)); , fixed_width(&get_editor(), 13));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, fixed_width(&get_editor(), 12)); , battery_info(8));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, fixed_width(lastexit, 3)); , fixed_width(lastexit, 3));
println!("{}", shell_color_str(&"]┐", SH_GREEN)); println!("{}", shell_color_str(&"]┐", SH_GREEN));
} }
{ {
print!("{}{}", shell_color_str(&"╞[", SH_GREEN) print!("{}{}", shell_color_str(&"╞[", SH_GREEN)
, shell_color_str(&fixed_width(&getenv("HOSTNAME"), 12), SH_CYAN)); , shell_color_str(&fixed_width(&gethostname().into_string()?, 13), SH_CYAN));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, shell_color_str(&fixed_width(&getenv("USER"), 10), SH_RED)); , shell_color_str(&fixed_width(&getenv("USER"), 13), SH_RED));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, fixed_width(&cpu_arch, 12)); , fixed_width(&cpu_arch, 8));
print!("{}{}", shell_color_str(&"|", SH_GREEN) print!("{}{}", shell_color_str(&"|", SH_GREEN)
, fixed_width(&bgjobs, 3)); , fixed_width(&bgjobs, 3));
@ -260,4 +267,5 @@ fn main()
{ {
print!("{} {} ", shell_color_str(&"", SH_GREEN), shell_color_str(&"$", SH_PURPLE)); print!("{} {} ", shell_color_str(&"", SH_GREEN), shell_color_str(&"$", SH_PURPLE));
} }
Ok(())
} }