Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
portaloffreedom committed Mar 29, 2023
2 parents 47622ea + 2ba7091 commit 4249e23
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
42 changes: 41 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
authors = ["Matteo De Carlo <[email protected]>"]
name = "rustline"
version = "0.1.2"
version = "0.1.5"
edition = "2018"

[dependencies]
git2 = "0.13.6"
termion = "1.5.5"
hostname = "0.3.1"
17 changes: 13 additions & 4 deletions rustline.zsh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
DIR="$( dirname "$0" )"
RUSTLINE_COMMAND=$DIR/target/release/rustline

if command -v "$DIR/target/release/rustline" &>/dev/null
then
RUSTLINE_COMMAND="$DIR/target/release/rustline"
elif command -v rustline &>/dev/null
then
RUSTLINE_COMMAND=$( command -v rustline )
else
echo ERROR: rustline command not found
fi

_rustline_append_precmd_function() {
if test -z "${precmd_functions[(re)$1]}" ; then
precmd_functions+=( $1 )
fi
if test -z "${precmd_functions[(re)$1]}" ; then
precmd_functions+=( $1 )
fi
}

integer _POWERLINE_JOBNUM
Expand Down
47 changes: 44 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::process::exit;
use std::io::{Write, stdout};
use termion::{color, style};
use git2::{Repository, RepositoryState};
use termion::color::Color;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");

Expand All @@ -32,6 +33,8 @@ const FG_NAME: color::LightWhite = color::LightWhite;
const BG_NAME: color::Cyan = color::Cyan;
const FG_PATH: color::White = color::White;
const BG_PATH: color::LightBlack = color::LightBlack;
const FG_WARN: color::LightYellow = color::LightYellow;
const BG_WARN: color::Yellow = color::Yellow;

struct Config {
flag_shortened_path: String,
Expand All @@ -47,13 +50,51 @@ fn print_usage_and_exit(exit_code: i32) {
exit(exit_code);
}

fn is_session_ssh() -> bool {
let ssh_tty_env = env::var("SSH_TTY").is_ok();
if ssh_tty_env {
return ssh_tty_env;
}
//TODO it's also possible to detect if the parent of the shell is `sshd`

return false;
}

fn write_left(cout: &mut std::io::Stdout, conf: &Config) -> Result<(), std::io::Error> {
if is_session_ssh() {
let hostname = hostname::get()
.ok()
.map(|oss| oss.into_string()
// .map(|hostname| format!("@{}", hostname))
.ok())
.flatten()
.unwrap_or_default();

write!(cout, "%{{{}{}{}%}} 🔒{} %{{{}{}{}%}}%{{{}%}}",
color::Fg(FG_WARN),
color::Bg(BG_WARN),
style::Bold,
hostname,
style::Reset,
color::Fg(BG_WARN),
color::Bg(BG_NAME),
color::Fg(FG_NAME),
)?;
}

let user = env::var("USER");
let (fg_name, bg_name): (Box<dyn Color>, Box<dyn Color>) = if user.is_ok() && user.unwrap() == "root" {
(Box::new(color::White), Box::new(color::Red))
} else {
(Box::new(FG_NAME), Box::new(BG_NAME))
};

write!(cout, "%{{{}{}{}%}} %n %{{{}{}{}%}}%{{{}%}} ",
color::Fg(FG_NAME),
color::Bg(BG_NAME),
color::Fg(fg_name.as_ref()),
color::Bg(bg_name.as_ref()),
style::Bold,
style::Reset,
color::Fg(BG_NAME),
color::Fg(bg_name.as_ref()),
color::Bg(BG_PATH),
color::Fg(FG_PATH),
)?;
Expand Down

0 comments on commit 4249e23

Please sign in to comment.