summaryrefslogtreecommitdiff
path: root/bashrc
blob: fcf0991d35aae178f652b3e3ffa4e14964ca6dd9 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
########################################################################
# Colour definitions for parts of $PS1.


# Do we even have a terminal we can do things to?
we_have_term()
{
    [ "$TERM" ] && [ "$TERM" != dumb ]
}


raw_termattr()
{
    if we_have_term
    then
        for attr in "$@"
        do
            case "$attr" in
                normal)
                    tput sgr0
                    ;;
                red)
                    tput setaf 1
                    ;;
                blue)
                    tput setaf 4
                    ;;
                bold)
                    tput bold
                    ;;
            esac
        done
    fi
}


# Output terminal attribute changing sequence, if there's a terminal.
# Also output output bash $PS1 sequences to tell bash it's
# non-printing characters.
termattr()
{
    printf '\['
    raw_termattr "$@"
    printf '\]'
}



########################################################################
# Collect info to show in $PS1


taskwarrior_context()
{
    if command -v task > /dev/null && [ -e "$HOME/.task" ]
    then
        task context list | awk '$NF == "yes" { print $1 }'
    fi
}


# Context for git and Debian packging: QvarnLabs?
QL()
{
    case "$DEBEMAIL" in
    *@qvarnlabs.com) printf "[QL:$OS_PROJECT_SHORTNAME] " ;;
    esac
}


find_git_root()
{
    (
        set -e
        while [ "$(pwd)" != / ]
        do
            if [ -d .git ]
            then
                pwd
                break
        else
            cd ..
            fi
        done
    )
}


is_git_repo()
{
    case "$(find_git_root)" in
        /*) return 0 ;;
        *) return 1 ;;
    esac
}


get_git_branch()
{
    git branch --no-color | 
    awk '
        BEGIN { max = 30 }
        /^\* / {
            name = $2
            if (length(name) > max)
                name = "..." substr(name, length(name) - max)
            print name
        }
    '
}


########################################################################
# Set window title for terminal emulator, screen.

set_window_title()
{
    case "$TERM" in
        xterm*)
	    printf '\033]1;%s\07' "$1" > /dev/tty
	    printf '\033]2;%s\07' "$1" > /dev/tty
	    ;;
        screen)
            printf '\ek%s\e\\' "$1" > /dev/tty
	    ;;
    esac
}



########################################################################
# Dynamically change $PS1 or output other things just before the prompt.


wrap_if_nonempty()
{
    local thing="$1"
    local before="$2"
    local after="$3"

    if [ -n "$thing" ]
    then
        printf '%s%s%s' "$before" "$thing" "$after"
    fi
}


define_ps1()
{
    wrap_if_nonempty "$(taskwarrior_context)" "$(termattr red)" "$(termattr normal) "
    wrap_if_nonempty "$(QL)" "$(termattr bold red)" "$(termattr normal)"

    termattr bold blue
    date +%H:%M | tr -d '\n'
    local hostname="$(hostname)"
    case "$hostname" in
        exolobe3)
            printf ' \w '
            ;;
        *)
            termattr red
            printf ' %s' "$hostname"
            termattr bold blue
            printf ':\w '
            ;;
    esac
    termattr normal

    if is_git_repo
    then
        wrap_if_nonempty "$(get_git_branch)" "$(termattr bold blue)[" "]$(termattr normal)"
    fi

    termattr bold blue
    printf '\$ '
    termattr normal
}


tell_about_exit_code()
{
    local exit_code="$1"
    if [ "$exit_code" != 0 ]
    then
        raw_termattr bold red
        printf '[PREVIOUS COMMAND EXIT: %s]\n' "$exit_code"
        raw_termattr normal
    fi
}


export PROMPT_COMMAND=prompt_command
prompt_command() { 
    tell_about_exit_code "$?"
    PS1="$(define_ps1)"
    set_window_title "Terminal"
}


########################################################################

# Function for using the QvarnLabs "os-rchelper" script more
# conveniently.

osrc()
{
    eval `os-rchelper "$1"`
}


###############

ulimit -c unlimited
umask 002

if command -v emacs > /dev/null
then
    export EDITOR=emacs
else
    export EDITOR=vi
fi
export PATH="$HOME/.cargo/bin:$PATH:$HOME/bin"
export PAGER=less
export LESSCHARSET=utf-8
export HISTCONTROL=ignoredups
export HISTFILESIZE=500
export DEBEMAIL=liw@liw.fi
export VIRSH_DEFAULT_CONNECT_URI=qemu:///system
export LANG=en_GB.UTF-8
export LC_COLLATE=fi_FI.UTF-8
export QVARNLABS_REPOS="$HOME/qvarnlabs/code"
export ANSIBLE_ROLES_PATH="$HOME/code/debian-ansible/roles:$HOME/pers/debian-ansible/roles"


PROMPT_DIRTRIM=1

set +H
if [ -n "$PS1" ]
then
    bind -r ''
fi