~tommoa/dots

ref: 55ef81233d6785d97d8abc101d73fe0952807dd0 dots/.env -rw-r--r-- 2.8 KiB
55ef8123Tom Almeida zsh: Add vi-mode maps 6 months ago
                                                                                
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
#!/bin/sh

# This script runs on the startup of every POSIX compliant shell.

# set the umask
umask 022

# Exec out to tmux if possible
{
    # Technically not strictly POSIX because "local", but `dash` supports it
    if [ -z "${SSH_CLIENT}" ] && [ -z "${VIMRUNTIME}" ] && [ -z "${TMUX}" ]; then
        # Only launch TMUX if we're not in an SSH session
        # and we're not in a VIM session
        # and we're not already in a TMUX session
        local tmux=$(/usr/bin/env which tmux)
        [ -z "${tmux}" ] && return
        local ns=$("${tmux}" list-sessions | wc -l)
        if [ "${ns}" -gt 0 ]; then
            exec "${tmux}" attach
        else
            exec "${tmux}" new-session
        fi
    fi
}

# Colours for various things
blue="\033[38;5;4m"
green="\033[38;5;85m"
red="\033[38;5;1m"
cyan="\033[38;5;6m"
underline_cyan="\033[4;38;5;6m"
reset="\033[m"
bold="\033[1m"

# A prompt for git
prompt_git() {
    if [ "${GIT_PROMPT}" = "1" ] && git rev-parse --is-inside-work-tree -q >/dev/null 2>&1; then
        eval $(git diff-files --numstat -r 2>/dev/null | awk '{add+=$1; remove+=$2} END {printf "num_added='%d';num_removed='%d';total='%d';", add, remove, NR}')
        local totals="";
        if [ "${total}" -gt 0 ]; then
            totals="${bold}:${blue}${total}"
            [ "${num_added}" -gt 0 ] && totals="${totals}${green}+${num_added}"
            [ "${num_removed}" -gt 0 ] && totals="${totals}${red}-${num_removed}"
        fi
        printf "${bold}[${red}$(git symbolic-ref --short -q HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)${reset}${totals}${reset}${bold}] ${reset}"
    fi
}

PS1='$(printf "\033[1;38;5;85m${USER}${SSH_CLIENT:+${red}@${underline_cyan}$(uname -n)}${reset}${bold}:\033[38;5;75m${PWD#${HOME}/}${reset}${bold}#${reset}") $(prompt_git)'

{
    if which exa >/dev/null 2>/dev/null; then
        # Set ls to being exa if possible
        alias ls="exa --color=auto"
    elif ls --version 2>&1 | grep -i gnu >/dev/null; then
        # gnu coreutils supports colours
        alias ls="ls --color=auto"
    elif ls --version 2>&1 | grep -i busybox >/dev/null; then
        # busybox also supports colours
        alias ls="ls --color=auto"
    fi
}

# A function to switch to a specific directory/tmux session
mux() {
    local tmux=$(env which tmux)
    local fd=$(env which find)
    [ -z "${tmux}" ] && return
    [ -z "${fd}" ] && return
    # Get XDG directories
    . "${HOME}/.config/user-dirs.dirs"
    local dir=$("${fd}" -L "${XDG_DOCUMENTS_DIR}" -type d -iname "${1}" | awk -F'/' 'NR==1{n=NF; m=$0} NF<n{ m=$0; n=NF } END { print m }')
    ${tmux} if-shell                  \
        "${tmux} has-session -t ${1}" \
        "switch-client -t ${1}"       \
        "new-session -ds ${1} -c ${dir}; switch-client -t ${1}"
}

alias tls="tmux list-sessions"

stty -ixon
export GIT_PROMPT=1