updated comments

This commit is contained in:
Candifloss 2024-12-01 21:15:46 +05:30
parent d9fdd8fa9e
commit dbd272eb48
4 changed files with 16 additions and 15 deletions

View File

@ -1,6 +1,6 @@
# PrettyPrompt # PrettyPrompt
A pretty shell prompt, written in rust A pretty shell prompt, written in rust.
## Current Features ## Current Features
@ -126,5 +126,5 @@ The current default (and only) theme draws inspiration from [s1ck94](https://git
## Why this project? ## Why this project?
- **Efficiency**: Avoids repeated invocation of multiple binaries like `tr`, `grep`, `echo`, `git`, `sed`, etc., which would otherwise be used dozens of times in shell scripts just to generate a colored string. - **Efficiency**: Avoids repeated invocation of multiple binaries like `tr`, `grep`, `echo`, `git`, `sed`, etc., which would otherwise be used dozens of times in shell scripts just to generate a colored string.
- **Universality**: Eliminates the need to write separate scripts in different shell languages for various shells. - **Portability**: Eliminates the need to write separate scripts in different shell languages for various shells.
- **Learning Rust**: Serves as a fun and practical project to learn and apply Rust programming skills. - **Learning Rust**: Serves as a fun and practical project to learn and apply Rust programming skills.

View File

@ -3,13 +3,13 @@ use ansi_term::Colour::RGB;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
// SSH indicator symbol pub const GIT_SYMBOL: &str = "\u{276F}"; // Git indicator symbol: ""
pub const GIT_SYMBOL: &str = "\u{276F}"; // "" pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44);
pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44); // pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150);
pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150); // pub const DEFAULT_BRANCH_COL: ansi_term::Colour = RGB(255, 255, 255);
pub const OTHER_COL: ansi_term::Colour = RGB(82, 82, 82); // pub const NORMIE_COL: ansi_term::Colour = RGB(82, 82, 82);
pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
/// Returns the repo's root and branch, if present
pub fn info() -> Option<(String, String)> { pub fn info() -> Option<(String, String)> {
let output = Command::new("git") // Program/Command to execute let output = Command::new("git") // Program/Command to execute
.args(["rev-parse", "--show-toplevel", "--abbrev-ref", "HEAD"]) // Arguments .args(["rev-parse", "--show-toplevel", "--abbrev-ref", "HEAD"]) // Arguments
@ -20,12 +20,13 @@ pub fn info() -> Option<(String, String)> {
let output_str = String::from_utf8_lossy(&output.stdout).trim().to_string(); let output_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
let parts: Vec<&str> = output_str.split('\n').collect(); let parts: Vec<&str> = output_str.split('\n').collect();
if parts.len() == 2 { if parts.len() == 2 {
return Some((parts[0].to_string(), parts[1].to_string())); return Some((parts[0].to_string(), parts[1].to_string())); // If the `git`` command returns a repo and branch
} }
} }
None None // If the current directory is not in a git repo
} }
/// The name of the repo
pub fn repo_name(path: &str) -> String { pub fn repo_name(path: &str) -> String {
Path::new(path) Path::new(path)
.file_name() // Extracts the last component of the path. .file_name() // Extracts the last component of the path.
@ -34,13 +35,13 @@ pub fn repo_name(path: &str) -> String {
.to_string() // Converts &str to String .to_string() // Converts &str to String
} }
//Git branch indicator /// Git branch indicator
pub fn indicator(branch: Option<String>) -> ANSIGenericString<'static, str> { pub fn indicator(branch: Option<String>) -> ANSIGenericString<'static, str> {
match branch { match branch {
Some(b) => match b.as_str() { Some(b) => match b.as_str() {
"main" => MAIN_COL.paint(GIT_SYMBOL), "main" => MAIN_COL.paint(GIT_SYMBOL),
"dev" => DEV_COL.paint(GIT_SYMBOL), "dev" => DEV_COL.paint(GIT_SYMBOL),
_ => OTHER_COL.paint(GIT_SYMBOL), _ => DEFAULT_BRANCH_COL.paint(GIT_SYMBOL),
}, },
None => NORMIE_COL.paint(GIT_SYMBOL), None => NORMIE_COL.paint(GIT_SYMBOL),
} }

View File

@ -2,10 +2,9 @@ use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB; use ansi_term::Colour::RGB;
use std::env::var; // For environment variables use std::env::var; // For environment variables
/// Constants
pub const SSH_SYMBOL: &str = "\u{276F}"; // SSH indicator symbol: "" pub const SSH_SYMBOL: &str = "\u{276F}"; // SSH indicator symbol: ""
pub const SSH_COL: ansi_term::Colour = RGB(255, 149, 0); // In SSH session pub const SSH_COL: ansi_term::Colour = RGB(255, 149, 0); // In SSH session
pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // Non-SSH session pub const NORMIE_COL: ansi_term::Colour = RGB(82, 82, 82); // Non-SSH session
const SSH_ENV_VARS: [&str; 2] = ["SSH_TTY", "SSH_CONNECTION"]; // Environment variables normally present in SSH sessions const SSH_ENV_VARS: [&str; 2] = ["SSH_TTY", "SSH_CONNECTION"]; // Environment variables normally present in SSH sessions
/// Checks if current session is an SSH session /// Checks if current session is an SSH session

View File

@ -7,11 +7,12 @@ pub const ROOT_COL: ansi_term::Colour = RGB(255, 53, 94); // If the user is root
pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // Regular user pub const NORMIE_COL: ansi_term::Colour = RGB(0, 255, 180); // Regular user
const ROOT_USER: &str = "root"; // Root username constant const ROOT_USER: &str = "root"; // Root username constant
//Root user indicator /// Username of current user
pub fn username() -> String { pub fn username() -> String {
var("USER").unwrap_or_else(|_| "UnknownUser".to_string()) var("USER").unwrap_or_else(|_| "UnknownUser".to_string())
} }
/// Root user indicator
pub fn indicator() -> ANSIGenericString<'static, str> { pub fn indicator() -> ANSIGenericString<'static, str> {
let user = username(); let user = username();
if user == ROOT_USER { if user == ROOT_USER {