summaryrefslogtreecommitdiff
path: root/bash/bashrc.sync
blob: f191b3fe40a22bcc2b7c75939cb554a3b7527ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# --- environment variables -----------------------------------------------------------------------

export VISUAL=vim
export EDITOR="$VISUAL"
export LEDGER_FILE="~/ledger/main.ledger"

# --- aliases -------------------------------------------------------------------------------------

alias ledger='ledger --date-format "%Y-%m-%d"'

alias ledger-cash='ledger register cash --tail 15'

alias ledger-checking='ledger register checking --tail 15'

alias ledger-worth='ledger bal ^assets ^liabilities'

alias enscript='enscript -Bc'


# --- prompt command ------------------------------------------------------------------------------


_bash_prompt_config() {

  local USER_SYMBOL="\u"
  local HOST_SYMBOL="\h"
  local ESC_OPEN="\["
  local ESC_CLOSE="\]"

  if tput setaf >/dev/null 2>&1 ; then
    _setaf () { tput setaf "$1" ; }
    local RESET="${ESC_OPEN}$( { tput sgr0 || tput me ; } 2>/dev/null )${ESC_CLOSE}"
    local BOLD="$( { tput bold || tput md ; } 2>/dev/null )"
  else
    # Fallback
    _setaf () { echo "\033[0;$(($1+30))m" ; }
    local RESET="\033[m"
    local BOLD=""
    ESC_OPEN=""
    ESC_CLOSE=""
  fi

  # Normal colors
  local BLACK="${ESC_OPEN}$(_setaf 0)${ESC_CLOSE}"
  local RED="${ESC_OPEN}$(_setaf 1)${ESC_CLOSE}"
  local GREEN="${ESC_OPEN}$(_setaf 2)${ESC_CLOSE}"
  local YELLOW="${ESC_OPEN}$(_setaf 3)${ESC_CLOSE}"
  local BLUE="${ESC_OPEN}$(_setaf 4)${ESC_CLOSE}"
  local VIOLET="${ESC_OPEN}$(_setaf 5)${ESC_CLOSE}"
  local CYAN="${ESC_OPEN}$(_setaf 6)${ESC_CLOSE}"
  local WHITE="${ESC_OPEN}$(_setaf 7)${ESC_CLOSE}"

  # Bright colors
  local BRIGHT_GREEN="${ESC_OPEN}$(_setaf 10)${ESC_CLOSE}"
  local BRIGHT_YELLOW="${ESC_OPEN}$(_setaf 11)${ESC_CLOSE}"
  local BRIGHT_BLUE="${ESC_OPEN}$(_setaf 12)${ESC_CLOSE}"
  local BRIGHT_VIOLET="${ESC_OPEN}$(_setaf 13)${ESC_CLOSE}"
  local BRIGHT_CYAN="${ESC_OPEN}$(_setaf 14)${ESC_CLOSE}"
  local BRIGHT_WHITE="${ESC_OPEN}$(_setaf 15)${ESC_CLOSE}"

  # Bold colors
  local BLACK_BOLD="${ESC_OPEN}${BOLD}$(_setaf 0)${ESC_CLOSE}"
  local RED_BOLD="${ESC_OPEN}${BOLD}$(_setaf 1)${ESC_CLOSE}"
  local GREEN_BOLD="${ESC_OPEN}${BOLD}$(_setaf 2)${ESC_CLOSE}"
  local YELLOW_BOLD="${ESC_OPEN}${BOLD}$(_setaf 3)${ESC_CLOSE}"
  local BLUE_BOLD="${ESC_OPEN}${BOLD}$(_setaf 4)${ESC_CLOSE}"
  local VIOLET_BOLD="${ESC_OPEN}${BOLD}$(_setaf 5)${ESC_CLOSE}"
  local CYAN_BOLD="${ESC_OPEN}${BOLD}$(_setaf 6)${ESC_CLOSE}"
  local WHITE_BOLD="${ESC_OPEN}${BOLD}$(_setaf 7)${ESC_CLOSE}"

  # Expose the variables we need in prompt command
  P_USER=${BRIGHT_GREEN}${USER_SYMBOL}
  P_HOST=${CYAN}${HOST_SYMBOL}
  P_WHITE=${WHITE_BOLD}
  P_BLUE=${BLUE_BOLD}
  P_GREEN=${YELLOW_BOLD}
  P_YELLOW=${GREEN_BOLD}
  P_RED=${RED}
  P_RESET=${RESET}

}

_bash_prompt_config
unset _bash_prompt_config

parse_git_branch() {
  local OUT=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
  if [ "$OUT" != "" ]; then echo "$OUT"; fi
}

bash_prompt_command() {
  # conda
  if [ "$CONDA_DEFAULT_ENV" != "" ];
  then
    P_CONDA="conda@$CONDA_DEFAULT_ENV "
  else
    P_CONDA=""
  fi
  # git
  if [ "$(parse_git_branch)" != "" ];
  then
    P_GIT="git@$(parse_git_branch) "
  else
    P_GIT=""
  fi
  # construct
  export PS1="${P_YELLOW}\u@\h ${P_GREEN}${P_CONDA}${P_RED}${P_GIT}${P_BLUE}\W ${P_YELLOW}\\$ ${P_RESET}"
}
PROMPT_COMMAND=bash_prompt_command

# --- functions -----------------------------------------------------------------------------------

function countdown(){
  secs=$(($1 * 60));
  date1=$((`date +%s` + "$secs"));
  while [ "$date1" -ge `date +%s` ]; do
    echo -ne "$(date -u --date @$(($date1 - `date +%s`)) +%H:%M:%S)\r";
    sleep 0.1
  done
}

function git-commit-with-timestamp(){
  print-green $(pwd)
  git remote update
  git pull
  if ! git diff --quiet
  then
    echo "COMMITING"
    git add --all
    git commit -m "$(date +"%Y-%m-%d %H:%M")"
    git push
  fi
}

function git-sync-all(){
  while read -r line
  do
    print-line
    cd "$line"
    git-commit-with-timestamp
  done < ~/.git-synced
  cd ~
}

function print-cyan {
  CYAN='\033[0;36m'
  NC='\033[0m' # No Color
  printf "${CYAN}$*${NC}\n"
}


function print-green {
  GREEN='\033[0;32m'
  NC='\033[0m' # No Color
  printf "${GREEN}$*${NC}\n"
}

function print-line {
  print-green "%`tput cols`s"|tr ' ' '#'
}

function print-yellow {
  YELLOW='\033[0;33m'
  NC='\033[0m' # No Color
  printf "${YELLOW}$*${NC}\n"
}

function pip3-update-all {
  pip3 freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip3 install -U
}