some work
This commit is contained in:
parent
f8a029545b
commit
8fac4e5aff
|
@ -9,3 +9,6 @@ edition = "2021"
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "make_prompt"
|
name = "make_prompt"
|
||||||
path = "src/make_prompt.rs"
|
path = "src/make_prompt.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
git2="*"
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::io::BufReader;
|
||||||
|
use std::io::BufRead;
|
||||||
|
|
||||||
fn fixed_width(input: &String, len: usize) -> String
|
fn fixed_width(input: &String, len: usize) -> String
|
||||||
{
|
{
|
||||||
|
@ -11,9 +13,9 @@ fn fixed_width(input: &String, len: usize) -> String
|
||||||
let cut_to = len - 2_usize;
|
let cut_to = len - 2_usize;
|
||||||
let start_from = clen - cut_to;
|
let start_from = clen - cut_to;
|
||||||
let cutted_result = &input[start_from..];
|
let cutted_result = &input[start_from..];
|
||||||
let mut result: String = cutted_result.to_owned();
|
let mut result: String = String::from(PLACE_HOLDER);
|
||||||
|
|
||||||
result.push_str(&PLACE_HOLDER);
|
result.push_str(&cutted_result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -69,11 +71,107 @@ fn battery_info(width_to: usize) -> String
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn do_get_git_branch(cwd: & std::path::Path) -> String
|
||||||
|
{
|
||||||
|
let default = String::from("");
|
||||||
|
match git2::Repository::discover(cwd)
|
||||||
|
{
|
||||||
|
Ok(repo) => {
|
||||||
|
match repo.head()
|
||||||
|
{
|
||||||
|
Ok(head) => {
|
||||||
|
match head.shorthand()
|
||||||
|
{
|
||||||
|
Some(branch) => String::from(branch),
|
||||||
|
None => default
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => default
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn get_git_branch(cwd: & std::path::Path) -> String
|
||||||
|
{
|
||||||
|
format!("✶ {}", do_get_git_branch(cwd))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn head<T: Read>(from: T, nlines: u64) -> String
|
||||||
|
{
|
||||||
|
let mut res = String::new();
|
||||||
|
let mut i = 0;
|
||||||
|
let mut reader = BufReader::new(from);
|
||||||
|
loop
|
||||||
|
{
|
||||||
|
i += 1;
|
||||||
|
if i > nlines
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let mut line = String::new();
|
||||||
|
let _len = reader.read_line(&mut line);
|
||||||
|
res.push_str(& line);
|
||||||
|
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cut_f(input: &String, delimiter: &str, field: u32) -> String
|
||||||
|
{
|
||||||
|
let mut i = 1;
|
||||||
|
for fld in input.split(delimiter)
|
||||||
|
{
|
||||||
|
if i < field
|
||||||
|
{
|
||||||
|
i += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
if fld.ends_with('\n')
|
||||||
|
{
|
||||||
|
fld[..fld.len() - 1].to_string()
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fld.to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
String::from("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_editor() -> String
|
||||||
|
{
|
||||||
|
match std::env::var("EDITOR")
|
||||||
|
{
|
||||||
|
Ok(editor) => match std::process::Command::new(editor).arg("--version").stdout(std::process::Stdio::piped()).spawn()
|
||||||
|
{
|
||||||
|
Ok(child) => match child.stdout
|
||||||
|
{
|
||||||
|
Some(out) => cut_f(&head(out, 1), "-", 1),
|
||||||
|
None => {eprintln!("editor --version did not give stdout"); String::from("")}
|
||||||
|
},
|
||||||
|
Err(_) => {eprintln!("failed to spawn editor"); String::from("")}
|
||||||
|
},
|
||||||
|
Err(_) => {eprintln!("failed to get $EDITOR"); String::from("")}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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!("\x1b[{color}m{string_in}\x1b[00m")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn getenv(varname: &str) -> String
|
||||||
|
{
|
||||||
|
match std::env::var(varname)
|
||||||
|
{
|
||||||
|
Ok(hostname) => hostname,
|
||||||
|
Err(_) => "".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main()
|
fn main()
|
||||||
{
|
{
|
||||||
|
@ -114,15 +212,26 @@ fn main()
|
||||||
args[2].to_string()
|
args[2].to_string()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
print!("\n");
|
print!("\n");
|
||||||
{
|
{
|
||||||
print!("{}{}", shell_color_str(&"┌[", SH_GREEN), battery_info(12));
|
print!("{}{}", shell_color_str(&"┌[", SH_GREEN)
|
||||||
println!("{}", shell_color_str(&"]", SH_GREEN));
|
, battery_info(12));
|
||||||
|
print!("{}{}", shell_color_str(&"|", SH_GREEN)
|
||||||
|
, fixed_width(&get_git_branch(& std::path::Path::new(&cwd)), 12));
|
||||||
|
print!("{}{}", shell_color_str(&"|", SH_GREEN)
|
||||||
|
, fixed_width(&get_editor(), 10));
|
||||||
|
print!("{}{}", shell_color_str(&"|", SH_GREEN)
|
||||||
|
, fixed_width(lastexit, 4));
|
||||||
|
println!("{}", shell_color_str(&"]┐", SH_GREEN));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
print!("{}", shell_color_str(&"╞[", SH_GREEN));
|
print!("{}{}", shell_color_str(&"╞[", SH_GREEN)
|
||||||
println!("{}", shell_color_str(&"]", SH_GREEN));
|
, fixed_width(&getenv("HOSTNAME"), 12));
|
||||||
|
print!("{}{}", shell_color_str(&"|", SH_GREEN)
|
||||||
|
, fixed_width(&getenv("USER"), 10));
|
||||||
|
println!("{}", shell_color_str(&"]┘", SH_GREEN));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
print!("{} {}", shell_color_str(&"╞>", SH_GREEN)
|
print!("{} {}", shell_color_str(&"╞>", SH_GREEN)
|
||||||
|
@ -131,6 +240,6 @@ fn main()
|
||||||
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
print!("{} {}", shell_color_str(&"└", SH_GREEN), shell_color_str(&"$", SH_PURPLE));
|
print!("{} {} ", shell_color_str(&"└", SH_GREEN), shell_color_str(&"$", SH_PURPLE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user