introduce modularity
This commit is contained in:
parent
89d909b512
commit
7b27e81c89
55
src/git.rs
Normal file
55
src/git.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use colored::{ColoredString, Colorize};
|
||||
use std::process::Command;
|
||||
|
||||
pub 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()
|
||||
}
|
||||
}
|
||||
|
||||
pub 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
|
||||
}
|
||||
|
||||
pub 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()
|
||||
}
|
||||
|
||||
pub 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),
|
||||
}
|
||||
}
|
79
src/main.rs
79
src/main.rs
@ -1,6 +1,11 @@
|
||||
use colored::{ColoredString, Colorize};
|
||||
use std::env::{args, current_dir, var_os};
|
||||
use std::process::Command;
|
||||
mod git;
|
||||
mod ssh;
|
||||
mod user;
|
||||
use crate::git::*;
|
||||
use crate::ssh::*;
|
||||
use crate::user::*;
|
||||
|
||||
fn get_shell_char(shell: &str) -> String {
|
||||
let shell_char = match shell {
|
||||
@ -15,59 +20,6 @@ fn get_shell_char(shell: &str) -> 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()
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@ -92,13 +44,6 @@ fn abrev_path(path: &str) -> String {
|
||||
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();
|
||||
@ -112,21 +57,15 @@ fn main() -> std::io::Result<()> {
|
||||
shell = "none".to_string();
|
||||
}
|
||||
|
||||
let root_indicator = match user.as_str() {
|
||||
"root" => angle.truecolor(255, 53, 94),
|
||||
_ => angle.truecolor(0, 255, 180),
|
||||
};
|
||||
let root_indicator = root_indicator();
|
||||
|
||||
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(),
|
||||
};
|
||||
// SSH status
|
||||
let ssh_char = ssh_char();
|
||||
|
||||
//Git status
|
||||
let git_branch = get_git_branch();
|
||||
|
10
src/ssh.rs
Normal file
10
src/ssh.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use colored::{ColoredString, Colorize};
|
||||
use std::env::var_os;
|
||||
|
||||
//SSH shell indicator
|
||||
pub fn ssh_char() -> ColoredString {
|
||||
match var_os("SSH_TTY") {
|
||||
Some(_val) => " ".truecolor(0, 150, 180),
|
||||
None => "".clear(),
|
||||
}
|
||||
}
|
20
src/user.rs
Normal file
20
src/user.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use colored::{ColoredString, Colorize};
|
||||
use std::env::var_os;
|
||||
|
||||
//Root user indicator
|
||||
|
||||
pub fn user() -> String {
|
||||
var_os("USER")
|
||||
.expect("UnknownUser")
|
||||
.to_str()
|
||||
.expect("UnknownUser")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn root_indicator() -> ColoredString {
|
||||
let angle = "❯";
|
||||
match user().as_str() {
|
||||
"root" => angle.truecolor(255, 53, 94),
|
||||
_ => angle.truecolor(0, 255, 180),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user