summaryrefslogtreecommitdiff
path: root/zsh/functions/anonsh
blob: c9b1b4ffeb04d532dab97725e1a27dbd834f3781 (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
# Convenience helper to set up an "incognito mode" for a shell session.
#
# Installation:
# 1. Create a folder like ~/.zsh/functions
# 2. Add it to your ZSH function path with fpath=(~/.zsh/functions(:A) $fpath) in ~/.zshenv
# 3. Save this script as ~/.zsh/functions/anonsh
# 4. Add `autoload -Uz anonsh` to your ~/.zshrc
#
# Now you can type `anonsh` in any zsh session
#
# - Use `clear` to clear both the screen and scrollback buffer
# - Use `fc -p` to clear command history without affecting your usual HISTFILE

# Only store new command history entries in RAM
unset HISTFILE

# Redefine the hardstatus text to "anonsh" so it doesn't show $PWD
# where people can see it when the shell doesn't have focus
#
# (The zsh_hardstatus_pre* and title functions are part of my .zshrc setup)
if typeset -f zsh_hardstatus_precmd > /dev/null && typeset -f title > /dev/null; then
    autoload -Uz add-zsh-hook
    add-zsh-hook -d precmd zsh_hardstatus_precmd
    add-zsh-hook -d preexec zsh_hardstatus_preexec

    function zsh_hardstatus_precmd { title "anonsh" "anonsh"; }
    add-zsh-hook precmd zsh_hardstatus_precmd
    add-zsh-hook preexec zsh_hardstatus_precmd
fi

# Show ... in the prompt instead of $PWD so it can't give away
# something after running `clear`
#
# ($base_prompt is a custom part of my .zshrc setup)
if (( ${+base_prompt} )); then
    export base_prompt="${base_prompt//%%1~/...}"
fi
export PS1="${PS1//%%1~/...}"
export PS2="${PS2//%%1~/...}"
export PS3="${PS3//%%1~/...}"
export PS4="${PS4//%%1~/...}"

# Redefine "clear" to also clear the GNU screen scrollback buffer and to ask
# for the terminal scrollback buffer to be cleared in as many ways as possible
clear() {
    # Call the regular clear command
    command clear

    # Manually emit all the escapes I know for requesting the terminal be
    # cleared and ask GNU Screen to pass it through as a literal
    #
    # Source: https://apple.stackexchange.com/a/318217/388700
    printf '\eP\e[2J\e[3J\e[;H\ec\e\\'

    # Flush GNU Screen's internal scrollback for this shell
    if [ "$TERM" = "screen" ]; then
        screen -X scrollback 0
        screen -X scrollback 5000
    fi
}

# TODO: Try to find a workaround to call the terminal scrollback-clearing part
#       of `clear` on switching GNU screen focus and then refresh the visible
#       part from screen's stored copy so nothing anonsh can show accidentally.
#
#       (Without removing the `termcapinfo` line to from ~/.screenrc which
#       would disable it when `anonsh` isn't in use.)

# Call `clear` on exit to clear terminal scrollback
zshexit() { clear; }

# vim: set ft=zsh :