Skip to content

Instantly share code, notes, and snippets.

@sabinM1
Created September 18, 2021 15:05
Show Gist options
  • Save sabinM1/a6631e3dafde0791901b2ae4dc6ce412 to your computer and use it in GitHub Desktop.
Save sabinM1/a6631e3dafde0791901b2ae4dc6ce412 to your computer and use it in GitHub Desktop.
.bashrc
#
# ~/.bashrc
#
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo " (${BRANCH}${STAT}) "
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
function nonzero_return() {
RETVAL=$?
[ $RETVAL -ne 0 ] && echo "$RETVAL"
}
# fara culori:
# export PS1="[\u@\h \W]\`nonzero_return\`\`parse_git_branch\`\\$ "
# cu culori:
#export PS1="[\[\e[32m\]\u\[\e[m\]@\[\e[31m\]\h\[\e[m\] \W]\[\e[31;43m\]\`nonzero_return\`\[\e[m\]\[\e[33m\]\`parse_git_branch\`\[\e[m\]\\$ "
export PS1="[\e[38;5;$((RANDOM%257))m\]\u\[\e[m\]@\[\e[31m\]\h\[\e[m\] \W]\[\e[31;43m\]\`nonzero_return\`\[\e[m\]\[\e[33m\]\`parse_git_branch\`\[\e[m\]\\$ "
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
[[ -f ~/.welcome_screen ]] && . ~/.welcome_screen
_set_my_PS1() {
PS1='[\u@\h \W]\$ '
if [ "$(whoami)" = "liveuser" ] ; then
local iso_version="$(grep ^VERSION= /usr/lib/endeavouros-release 2>/dev/null | cut -d '=' -f 2)"
if [ -n "$iso_version" ] ; then
local prefix="eos-"
local iso_info="$prefix$iso_version"
PS1="[\u@$iso_info \W]\$ "
fi
fi
}
#_set_my_PS1
unset -f _set_my_PS1
ShowInstallerIsoInfo() {
local file=/usr/lib/endeavouros-release
if [ -r $file ] ; then
cat $file
else
echo "Sorry, installer ISO info is not available." >&2
fi
}
alias ls='ls --color=auto'
alias ll='ls -lav --ignore=..' # show long listing of all except ".."
alias l='ls -lav --ignore=.?*' # show long listing but no hidden dotfiles except "."
[[ "$(whoami)" = "root" ]] && return
[[ -z "$FUNCNEST" ]] && export FUNCNEST=100 # limits recursive functions, see 'man bash'
## Use the up and down arrow keys for finding a command in history
## (you can write some initial letters of the command first).
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
################################################################################
## Some generally useful functions.
## Consider uncommenting aliases below to start using these functions.
_GeneralCmdCheck() {
# A helper for functions UpdateArchPackages and UpdateAURPackages.
echo "$@" >&2
"$@" || {
echo "Error: '$*' failed." >&2
exit 1
}
}
_CheckInternetConnection() {
# curl --silent --connect-timeout 8 https://8.8.8.8 >/dev/null
eos-connection-checker
local result=$?
test $result -eq 0 || echo "No internet connection!" >&2
return $result
}
_CheckArchNews() {
local conf=/etc/eos-update-notifier.conf
if [ -z "$CheckArchNewsForYou" ] && [ -r $conf ] ; then
source $conf
fi
if [ "$CheckArchNewsForYou" = "yes" ] ; then
local news="$(yay -Pw)"
if [ -n "$news" ] ; then
echo "Arch news:" >&2
echo "$news" >&2
echo "" >&2
# read -p "Press ENTER to continue (or Ctrl-C to stop): "
else
echo "No Arch news." >&2
fi
fi
}
UpdateArchPackages() {
# Updates Arch packages.
_CheckInternetConnection || return 1
_CheckArchNews
#local updates="$(yay -Qu --repo)"
local updates="$(checkupdates)"
if [ -n "$updates" ] ; then
echo "Updates from upstream:" >&2
echo "$updates" | sed 's|^| |' >&2
_GeneralCmdCheck sudo pacman -Syu "$@"
return 0
else
echo "No upstream updates." >&2
return 1
fi
}
UpdateAURPackages() {
# Updates AUR packages.
_CheckInternetConnection || return 1
local updates
if [ -x /usr/bin/yay ] ; then
updates="$(yay -Qua)"
if [ -n "$updates" ] ; then
echo "Updates from AUR:" >&2
echo "$updates" | sed 's|^| |' >&2
_GeneralCmdCheck yay -Syua "$@"
else
echo "No AUR updates." >&2
fi
else
echo "Warning: /usr/bin/yay does not exist." >&2
fi
}
UpdateAllPackages() {
# Updates all packages in the system.
# Upstream (i.e. Arch) packages are updated first.
# If there are Arch updates, you should run
# this function a second time to update
# the AUR packages too.
UpdateArchPackages || UpdateAURPackages
}
_open_files_for_editing() {
# Open any given document file(s) for editing (or just viewing).
# Note1: Do not use for executable files!
# Note2: uses mime bindings, so you may need to use
# e.g. a file manager to make some file bindings.
if [ -x /usr/bin/exo-open ] ; then
echo "exo-open $*" >&2
/usr/bin/exo-open "$@" >& /dev/null &
return
fi
if [ -x /usr/bin/xdg-open ] ; then
for file in "$@" ; do
echo "xdg-open $file" >&2
/usr/bin/xdg-open "$file" >& /dev/null &
done
return
fi
echo "Sorry, none of programs [$progs] is found." >&2
echo "Tip: install one of packages" >&2
for prog in $progs ; do
echo " $(pacman -Qqo "$prog")" >&2
done
}
_Pacdiff() {
local differ pacdiff=/usr/bin/pacdiff
if [ -n "$(echo q | DIFFPROG=diff $pacdiff)" ] ; then
for differ in kdiff3 meld diffuse ; do
if [ -x /usr/bin/$differ ] ; then
DIFFPROG=$differ su-c_wrapper $pacdiff
break
fi
done
fi
}
#------------------------------------------------------------
## Aliases for the functions above.
## Uncomment an alias if you want to use it.
##
# alias ef='_open_files_for_editing' # 'ef' opens given file(s) for editing
# alias pacdiff=_Pacdiff
################################################################################
#aliasuri:
alias cls="clear"
alias lh="ls -lha"
alias lr="ls -ltrha"
alias swjava="sudo update-alternatives --config java"
alias dws="cd /data/Downloads"
alias timp="/etc/init.d/ntp restart"
alias untar="tar -xvf"
alias placa="gpustat -cp --watch"
alias pubip="curl curlmyip.net"
alias doamne_ajuta="rm -rf ~/.cache/ && reboot"
alias tb="nc termbin.com 9999"
#transfer.sh:
transfer(){ if [ $# -eq 0 ];then echo -e "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>">&2;return 1;fi;if tty -s;then file="$1";file_name=$(basename "$file");if [ ! -e "$file" ];then echo "$file: No such file or directory">&2;return 1;fi;if [ -d "$file" ];then file_name="$file_name.zip" ,;(cd "$file"&&zip -r -q - .)|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null,;else cat "$file"|curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;else file_name=$1;curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name"|tee /dev/null;fi;}
#ix.io:
ix() {
local opts
local OPTIND
[ -f "$HOME/.netrc" ] && opts='-n'
while getopts ":hd:i:n:" x; do
case $x in
h) echo "ix [-d ID] [-i ID] [-n N] [opts]"; return;;
d) $echo curl $opts -X DELETE ix.io/$OPTARG; return;;
i) opts="$opts -X PUT"; local id="$OPTARG";;
n) opts="$opts -F read:1=$OPTARG";;
esac
done
shift $(($OPTIND - 1))
[ -t 0 ] && {
local filename="$1"
shift
[ "$filename" ] && {
curl $opts -F f:1=@"$filename" $* ix.io/$id
return
}
echo "^C to cancel, ^D to send."
}
curl $opts -F f:1='<-' $* ix.io/$id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment