Compare commits

..

No commits in common. "dev" and "main" have entirely different histories.
dev ... main

14 changed files with 252 additions and 350 deletions

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

11
.idea/PrettyPrompt.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PrettyPrompt.iml" filepath="$PROJECT_DIR$/.idea/PrettyPrompt.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

83
Cargo.lock generated
View File

@ -3,39 +3,90 @@
version = 3 version = 3
[[package]] [[package]]
name = "ansi_term" name = "colored"
version = "0.12.1" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [ dependencies = [
"winapi", "lazy_static",
"windows-sys",
] ]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "prettyprompt" name = "prettyprompt"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ansi_term", "colored",
] ]
[[package]] [[package]]
name = "winapi" name = "windows-sys"
version = "0.3.9" version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu", "windows-targets",
"winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "windows-targets"
version = "0.4.0" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "windows_aarch64_gnullvm"
version = "0.4.0" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
ansi_term = "0.12" colored = "2.1.0"

View File

@ -1,17 +0,0 @@
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
// Error indicator symbol
pub const ERR_SYMBOL: &str = "\u{276F}"; // ""
pub const ERR_COL: ansi_term::Colour = RGB(255, 53, 94); //
pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); //
// Error indicator
pub fn indicator(args: Vec<String>) -> ANSIGenericString<'static, str> {
let default_exit_code = "0".to_string();
let exit_code = args.get(1).unwrap_or(&default_exit_code); // Default to "0" if missing
match exit_code.as_str() {
"0" => NORMIE_COL.paint(ERR_SYMBOL),
_ => ERR_COL.paint(ERR_SYMBOL),
}
}

View File

@ -1,54 +0,0 @@
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::path::Path;
use std::process::Command;
// SSH indicator symbol
pub const GIT_SYMBOL: &str = "\u{276F}"; // ""
pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44); //
pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150); //
pub const OTHER_COL: ansi_term::Colour = RGB(82, 82, 82); //
pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
pub fn info() -> Option<(String, String)> {
let output = Command::new("git")
.arg("rev-parse")
.arg("--show-toplevel") // Repo root
.arg("--abbrev-ref") // Git branch
.arg("HEAD")
.output();
match output {
Ok(output) if output.status.success() => {
let stdout = String::from_utf8_lossy(&output.stdout); // Get cmd output
let output_string = stdout.trim();
let parts: Vec<&str> = output_string.split('\n').collect();
if parts.len() == 2 {
Some((parts[0].to_string(), parts[1].to_string()))
} else {
None
}
}
_ => None,
}
}
pub fn repo_name(path: &str) -> String {
Path::new(path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("") // Default value if None
.to_string() // Convert &str to String
}
//Git branch indicator
pub fn indicator(branch: Option<String>) -> ANSIGenericString<'static, str> {
match branch {
Some(b) => match b.as_str() {
"main" => MAIN_COL.paint(GIT_SYMBOL),
"dev" => DEV_COL.paint(GIT_SYMBOL),
_ => OTHER_COL.paint(GIT_SYMBOL),
},
None => NORMIE_COL.paint(GIT_SYMBOL),
}
}

View File

@ -1,94 +0,0 @@
use crate::indicators::git::{repo_name, MAIN_COL};
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::env::current_dir;
pub const UNKNOWN_PATH: &str = "\u{2248}"; // "≈"
pub const PATH_COL: ansi_term::Colour = RGB(82, 82, 82);
pub const REPO_COL: ansi_term::Colour = RGB(55, 120, 130);
fn home_dir() -> String {
std::env::var("HOME").map_or_else(|_| "".to_string(), |path| path.to_string())
}
fn full_path() -> String {
current_dir().map_or_else(
|_| UNKNOWN_PATH.to_string(),
|path| path.to_string_lossy().to_string(),
)
}
fn remove_repo(pwd_path: &str, repo_path: &str) -> String {
pwd_path.replacen(repo_path, "", 1)
}
fn replace_home(path: &str) -> String {
let homedir = home_dir();
path.replacen(&homedir, "~", 1)
}
fn short(path: &str, slash_limit: u8) -> String {
let slashes = path.matches('/').count();
if slashes <= slash_limit.into() {
path.to_string() // Short path, return without changes
} else {
// Long path, shorten it
let parts: Vec<&str> = path.split('/').collect(); // Split the path into parts
let first = if path.starts_with("~") { "~" } else { "" }; // Determine the first part correctly
let last = parts[parts.len() - 1]; // The last part
// Abbreviate middle parts (take the first character of each)
let abbreviated_middle: Vec<String> = parts[1..parts.len() - 1] // Skip the first and last part
.iter()
.filter(|&&part| !part.is_empty()) // Avoid empty parts (like after "/")
.map(|&part| part.chars().next().unwrap().to_string()) // Take the first letter
.collect();
// Join the parts back together with "/" and return the shortened path
let mut shortened_path = vec![first.to_string()];
shortened_path.extend(abbreviated_middle);
shortened_path.push(last.to_string());
shortened_path.join("/")
}
}
pub fn pwd(
abbrev_home: bool,
shorten_path: bool,
replace_repo: bool,
git_repo: Option<String>,
) -> ANSIGenericString<'static, str> {
let mut path = full_path();
let slash_limit: u8 = if replace_repo { 2 } else { 3 };
if replace_repo && git_repo.is_some() {
if let Some(repo_path) = git_repo {
path = remove_repo(&path, &repo_path);
let repo_name = repo_name(&repo_path);
if shorten_path {
path = short(&path, slash_limit);
}
match path.as_str() {
"" => REPO_COL.paint(repo_name),
_ => format!(
"{}{}{}",
REPO_COL.paint(repo_name),
MAIN_COL.paint(" \u{F02A2} "),
PATH_COL.italic().paint(path)
)
.into(),
}
} else {
PATH_COL.italic().paint(path)
}
} else if abbrev_home {
path = replace_home(&path);
if shorten_path {
path = short(&path, slash_limit);
}
PATH_COL.italic().paint(path)
} else {
PATH_COL.italic().paint(path)
}
}

View File

@ -1,21 +0,0 @@
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::env::var;
// SSH indicator symbol
pub const SSH_SYMBOL: &str = "\u{276F}"; // ""
pub const SSH_COL: ansi_term::Colour = RGB(255, 149, 0); // A ind of orange
pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
fn is_ssh_session() -> bool {
var("SSH_TTY").is_ok() || var("SSH_CONNECTION").is_ok()
}
//SSH shell indicator
pub fn indicator() -> ANSIGenericString<'static, str> {
if is_ssh_session() {
SSH_COL.paint(SSH_SYMBOL)
} else {
NORMIE_COL.paint(SSH_SYMBOL)
}
}

View File

@ -1,21 +0,0 @@
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::env::var;
// User indicator symbol
pub const USER_SYMBOL: &str = "\u{276F}"; // ""
pub const ROOT_COL: ansi_term::Colour = RGB(255, 53, 94); // Riot red or something
pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // A kind of green
//Root user indicator
pub fn username() -> String {
var("USER").unwrap_or_else(|_| "UnknownUser".to_string())
}
pub fn indicator() -> ANSIGenericString<'static, str> {
if username() == "root" {
ROOT_COL.paint(USER_SYMBOL)
} else {
NORMIE_COL.paint(USER_SYMBOL)
}
}

View File

@ -1,58 +1,161 @@
mod indicators { use colored::{ColoredString, Colorize};
pub mod error; use std::env::{args, current_dir, var_os};
pub mod git; use std::process::Command;
pub mod pwd;
pub mod ssh;
pub mod user;
}
use crate::indicators::error; fn get_shell_char(shell: &str) -> String {
use crate::indicators::git; let shell_char = match shell {
use crate::indicators::pwd; "bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" => "",
use crate::indicators::ssh; "zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" => "󰰶 ",
use crate::indicators::user; "fish" => "󰈺 ",
"nushell" => "",
fn main() { "ion" => "",
let cmd_args: Vec<String> = std::env::args().collect(); "oursh" => "󱢇 ",
_ => "󱆃 ",
let mut components: Vec<String> = Vec::new();
let indicate_user: bool = true;
let indicate_ssh: bool = true;
let indicate_err: bool = true;
let indicate_git_branch: bool = true;
let show_pwd: bool = true;
let abbrev_home: bool = true;
let shorten_path: bool = true;
let replace_repo: bool = true;
let mut git_info = None;
if indicate_ssh {
components.push(ssh::indicator().to_string());
}
if indicate_git_branch {
git_info = git::info();
match git_info {
Some(ref info) => components.push(git::indicator(Some(info.1.clone())).to_string()),
None => components.push(git::indicator(None).to_string()),
}
}
if indicate_user {
components.push(user::indicator().to_string());
}
if indicate_err {
components.push(error::indicator(cmd_args).to_string());
}
if show_pwd {
let repo_path = match git_info {
Some(info) => Some(info.0), // Clone to avoid ownership issues
None => None,
}; };
components.insert( shell_char.to_string()
0, }
pwd::pwd(abbrev_home, shorten_path, replace_repo, repo_path).to_string(),
); fn get_git_branch() -> String {
} let git_status_cmd = Command::new("git")
let prompt = components.join(""); .arg("status")
print!("{prompt} "); .output()
.expect("git_status_cmd_fail");
let git_status_output = String::from_utf8_lossy(&git_status_cmd.stdout);
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" {
git_status_output.split('\n').collect::<Vec<&str>>()[0]
.split(' ')
.collect::<Vec<&str>>()[2]
.to_string()
} else {
String::new()
}
}
fn get_git_root() -> String {
let git_repo_root_cmd = Command::new("git")
.arg("rev-parse")
.arg("--show-toplevel")
.output()
.expect("git_repo_root_cmd_fail");
let mut git_repo_path = String::from_utf8_lossy(&git_repo_root_cmd.stdout).to_string();
let git_repo_err = String::from_utf8_lossy(&git_repo_root_cmd.stderr);
if git_repo_err == "" {
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len();
git_repo_path.truncate(len);
} else {
git_repo_path = String::new();
}
git_repo_path
}
fn get_git_repo_name(git_repo_root: &str) -> String {
let repo_path_split: Vec<&str> = git_repo_root.split('/').collect();
let last_index = repo_path_split.len() - 1;
let git_repo_name = repo_path_split[last_index];
git_repo_name.to_string()
}
fn get_git_char(git_branch: &str) -> ColoredString {
match git_branch {
"" => "".clear(),
"main" => " 󰊢 ".truecolor(178, 98, 44),
"master" => " 󰊢 ".truecolor(196, 132, 29),
_ => " 󰊢 ".truecolor(82, 82, 82),
}
}
fn abrev_path(path: &str) -> String {
let mut short_dir = path.to_string();
let slashes = path.matches('/').count();
if slashes > 3 {
let parts: Vec<&str> = path.split('/').collect();
let len = parts.len() - 1;
let mut ch1: String;
for part in &parts[0..len] {
if part.to_string() != "" {
// to avoid the 1st "/"
ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part
short_dir = short_dir.replace(part, &ch1);
}
}
}
short_dir
}
fn main() -> std::io::Result<()> {
let angle = "";
//Root user indicator
let user = var_os("USER")
.expect("UnknownUser")
.to_str()
.expect("UnknownUser")
.to_string();
let mut err: String = String::new();
let args: Vec<String> = args().collect();
let shell: String;
if args.len() > 1 {
shell = args[1].clone(); // Shell symbol
if args.len() > 2 {
err.clone_from(&args[2]); // Error status
}
} else {
shell = "none".to_string();
}
let root_indicator = match user.as_str() {
"root" => angle.truecolor(255, 53, 94),
_ => angle.truecolor(0, 255, 180),
};
let err_indicator = match err.as_str() {
"0" => angle.truecolor(0, 255, 180),
_ => angle.truecolor(255, 53, 94),
};
//SSH shell indicator
let ssh_char: ColoredString = match var_os("SSH_TTY") {
Some(_val) => "".truecolor(0, 150, 180),
None => "".clear(),
};
//Git status
let git_branch = get_git_branch();
let git_repo_root = get_git_root();
let git_repo_name = get_git_repo_name(&git_repo_root.clone()).truecolor(122, 68, 24);
let git_char = get_git_char(&git_branch);
//pwd
let homedir = var_os("HOME")
.expect("UnknownDir")
.to_str()
.expect("UnknownDir")
.to_string();
let pwd = current_dir()?;
let mut cur_dir = pwd.display().to_string();
cur_dir = cur_dir.replace(&git_repo_root, ""); // Remove git repo root
cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~"
cur_dir = abrev_path(&cur_dir);
print!(
"{}{}{}{}{}{}{} ",
ssh_char,
get_shell_char(&shell).truecolor(75, 75, 75),
git_repo_name,
git_char,
cur_dir.italic().truecolor(82, 82, 82),
root_indicator,
err_indicator,
);
Ok(())
} }

View File

@ -1,55 +0,0 @@
//use colored::{ColoredString, Colorize};
use colored::Colorize;
use std::env::{args, current_dir, var_os};
mod indicators {
pub mod error;
pub mod git;
pub mod path;
pub mod shell;
pub mod ssh;
pub mod user;
}
use crate::indicators::{error::*, git::*, path::*, shell::*, ssh::*, user::*};
fn main() -> std::io::Result<()> {
//let angle = "";
let args: Vec<String> = args().collect();
let shell: String = shell(args.clone());
let root_indicator = root_indicator();
let err: String = err(args.clone());
let err_indicator = err_indicator(err);
// SSH status
let ssh_char = ssh_char();
//Git status
let git_branch = get_git_branch();
let git_repo_root = get_git_root();
let git_repo_name = get_git_repo_name(&git_repo_root.clone()).truecolor(122, 68, 24);
let git_char = get_git_char(&git_branch);
//pwd
let homedir = homedir();
let pwd = current_dir()?;
let mut cur_dir = pwd.display().to_string();
cur_dir = cur_dir.replace(&git_repo_root, ""); // Remove git repo root
cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~"
cur_dir = abrev_path(&cur_dir);
print!(
"{}{}{}{}{}{}{} ",
ssh_char,
get_shell_char(&shell).truecolor(75, 75, 75),
git_repo_name,
git_char,
cur_dir.italic().truecolor(82, 82, 82),
root_indicator,
err_indicator,
);
Ok(())
}

View File

@ -1,20 +0,0 @@
pub fn get_shell_char(shell: &str) -> String {
let shell_char = match shell {
"bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" => "",
"zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" => "󰰶 ",
"fish" => "󰈺 ",
"nushell" => "",
"ion" => "",
"oursh" => "󱢇 ",
_ => "󱆃 ",
};
shell_char.to_string()
}
pub fn shell(args: Vec<String>) -> String {
if args.len() > 1 {
args[1].clone() // Shell symbol
} else {
"none".to_string()
}
}