Compare commits

...

10 Commits
main ... dev

Author SHA1 Message Date
18168cf0e1 shorten pwd 2024-11-26 13:43:39 +05:30
a9a7dc13b8 path 2024-11-26 12:34:31 +05:30
5a1fca980d Delete src/old/error.rs 2024-11-25 07:55:17 +00:00
7257faf85d error indicator 2024-11-25 13:23:37 +05:30
24f3f6772e migrated user, git, & ssh indicators to ansi_term 2024-11-24 19:53:15 +05:30
b823586d03 changed dependancy to ansi_term 2024-11-23 16:50:15 +05:30
7ed36ce471 modularity 2024-11-21 20:04:16 +05:30
4f90edceef sort out modules 2024-11-21 16:28:26 +05:30
d1fecadb51 unwanted files 2024-11-21 14:11:13 +05:30
7b27e81c89 introduce modularity 2024-11-21 14:09:00 +05:30
15 changed files with 298 additions and 250 deletions

5
.idea/.gitignore vendored
View File

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

View File

@ -1,11 +0,0 @@
<?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>

View File

@ -1,8 +0,0 @@
<?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>

View File

@ -1,6 +0,0 @@
<?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,90 +3,39 @@
version = 3
[[package]]
name = "colored"
version = "2.1.0"
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"lazy_static",
"windows-sys",
"winapi",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "prettyprompt"
version = "0.1.0"
dependencies = [
"colored",
"ansi_term",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"windows-targets",
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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",
]
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

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

17
src/indicators/error.rs Normal file
View File

@ -0,0 +1,17 @@
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),
}
}

45
src/indicators/git.rs Normal file
View File

@ -0,0 +1,45 @@
use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
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,
}
}
//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),
}
}

33
src/indicators/pwd.rs Normal file
View File

@ -0,0 +1,33 @@
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);
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 replace_home(path: String) -> String {
let homedir = home_dir();
path.replace(&homedir, "~")
}
pub fn pwd(abbrev_home:bool) -> ANSIGenericString<'static, str> {
let mut path = full_path();
if abbrev_home {
path = replace_home(path);
}
PATH_COL.italic().paint(path)
}

21
src/indicators/ssh.rs Normal file
View File

@ -0,0 +1,21 @@
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)
}
}

22
src/indicators/user.rs Normal file
View File

@ -0,0 +1,22 @@
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,161 +1,46 @@
use colored::{ColoredString, Colorize};
use std::env::{args, current_dir, var_os};
use std::process::Command;
mod indicators {
pub mod error;
pub mod git;
pub mod pwd;
pub mod ssh;
pub mod user;
}
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" => "󱢇 ",
_ => "󱆃 ",
use crate::indicators::error;
use crate::indicators::git;
use crate::indicators::pwd;
use crate::indicators::ssh;
use crate::indicators::user;
fn main() {
let cmd_args: Vec<String> = std::env::args().collect();
let mut prompt: String = String::new();
let indicate_user: bool = true;
let indicate_ssh: bool = true;
let indicate_err: bool = true;
let indicate_git_branch: bool = true;
let pwd: bool = true;
let abbrev_home:bool = true;
if pwd {
prompt += &pwd::pwd(abbrev_home).to_string();
}
if indicate_ssh {
prompt += &ssh::indicator().to_string();
}
if indicate_git_branch {
let git_info = git::info();
match git_info {
Some((_repo, branch)) => prompt += &git::indicator(Some(branch)).to_string(),
None => prompt += &git::indicator(None).to_string(),
};
shell_char.to_string()
}
fn get_git_branch() -> String {
let git_status_cmd = Command::new("git")
.arg("status")
.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()
if indicate_user {
prompt += &user::indicator().to_string();
}
if indicate_err {
prompt += &error::indicator(cmd_args).to_string();
}
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(())
print!("{prompt} ");
}

55
src/old/main.bkp.rs Normal file
View File

@ -0,0 +1,55 @@
//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(())
}

31
src/old/path.rs Normal file
View File

@ -0,0 +1,31 @@
use std::env::{args, current_dir, var_os};
pub 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
}
//pwd
pub fn homedir() -> String {
var_os("HOME")
.expect("UnknownDir")
.to_str()
.expect("UnknownDir")
.to_string()
}

20
src/old/shell.rs Normal file
View File

@ -0,0 +1,20 @@
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()
}
}