Zsh

~/.zshrc

Rina Kawakita 2018. 6. 22. 20:53
# vim:foldmethod=marker:foldlevel=0:ts=4:sw=4

# PATH {{{
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/.local/bin:$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH=/home/nietz/.oh-my-zsh

# Android Studio
export ANDROID_HOME=~/Android/Sdk
export ANDROID_SDK_ROOT=~/Android/Sdk
export ANDROID_AVD_HOME=~/.android/avd
# }}}

# Custom Zsh Config {{{

#setopt extended_glob

# get the directory where this file is located
_custom_zsh_config_base="${${(%):-%x}:A:h}"

# bail out if global config is disabled
[[ $_custom_zsh_config_base == /etc/* && $_custom_zsh_no_global == 1 ]] && return

# bail out if we are already loaded
if (( _custom_zsh_config_loaded )); then
	print -P '%B%F{red}The custom ZSH config is already loaded (probably from the global zshrc)%f%b'
	print -P "%B%F{red}The local version ($_custom_zsh_config_base) has NOT been loaded%f%b"
	print -P '%B%F{yellow}To disable this warning, run the following command:%f%b'
	print -P "%B%F{green}echo '_custom_zsh_no_global=1' >>! ~/.zshenv%f%b"
	return
fi
_custom_zsh_config_loaded=1

# load all our config files
for file ($_custom_zsh_config_base/zshrc.d/*.zsh(N)); do
	source $file
done
# }}}
#
# Default Apps {{{
export EDITOR="/usr/bin/vim"
export FCEDIT="$EDITOR"
export VISUAL="$EDITOR"
export SUDO_EDITOR="$EDITOR"
export BROWSER="/usr/bin/qutebrowser"
export PAGER="less"
#export PAGER="more"

# tesseract
export TESSDATA_PREFIX=/usr/share/

# Ranger Env
export RANGER_LOAD_DEFAULT_RC=FALSE
export RANGER_LEVEL=1
export TERMCMD="urxvt"

# QT5 configuration
export QT_QPA_PLATFORMTHEME=qt5ct
# }}}

# Prompt Theme {{{
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
#ZSH_THEME="robbyrussell"
#ZSH_THEME="af-magic"
#ZSH_THEME="bira"
#ZSH_THEME="blinks"
#ZSH_THEME="darkblood"
#ZSH_THEME="jonathan"

# Prompt
fpath=( "$HOME/.zfunctions" $fpath )
autoload -U promptinit; promptinit
PURE_PROMPT_SYMBOL=───╼
#prompt pure
ZSH_THEME="pure"
# }}}

# Syntax Highlighting {{{
## https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/INSTALL.md
# zsh-syntax-highlighting
#git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
#echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
#source ./zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
#
# zsh-syntax-highlighting for Oh-my-zsh
#git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
#plugins=( [plugins...] zsh-syntax-highlighting)
#
## Override highlighter colors
# Declare the variable
typeset -A ZSH_HIGHLIGHT_STYLES
ZSH_HIGHLIGHT_STYLES[default]=none
ZSH_HIGHLIGHT_STYLES[unknown-token]=fg=009
ZSH_HIGHLIGHT_STYLES[reserved-word]=fg=009,standout
# To differentiate aliases from other command types
ZSH_HIGHLIGHT_STYLES[alias]='fg=red,bold'
ZSH_HIGHLIGHT_STYLES[builtin]=fg=white,bold
ZSH_HIGHLIGHT_STYLES[function]=fg=white,bold
ZSH_HIGHLIGHT_STYLES[command]=fg=white,bold
ZSH_HIGHLIGHT_STYLES[precommand]=fg=white,underline
ZSH_HIGHLIGHT_STYLES[commandseparator]=none
ZSH_HIGHLIGHT_STYLES[hashed-command]=fg=009
# To have paths colored instead of underlined
ZSH_HIGHLIGHT_STYLES[path]='fg=blue,underline'
# To disable highlighting of globbing expressions
ZSH_HIGHLIGHT_STYLES[globbing]='fg=cyan'
ZSH_HIGHLIGHT_STYLES[history-expansion]=fg=white,underline
ZSH_HIGHLIGHT_STYLES[single-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[double-hyphen-option]=none
ZSH_HIGHLIGHT_STYLES[back-quoted-argument]=none
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=yellow'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=green'
ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]='fg=red'
ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]='fg=red'
ZSH_HIGHLIGHT_STYLES[assign]=none
# }}}

# Pywal {{{
# Import colorscheme from 'wal' asynchronously
# &   # Run the process in the background.
# ( ) # Hide shell job control messages.
#(cat ~/.cache/wal/sequences &)

# Alternative (blocks terminal for 0-3ms)
#cat ~/.cache/wal/sequences

# To add support for TTYs this line can be optionally added.
source ~/.cache/wal/colors-tty.sh

# Using feh to tile the wallpaper now.
# We grab the wallpaper location from wal's cache so
# that this works even when a directory is passed.
#feh --bg-tile "$(< "${HOME}/.cache/wal/wal")"

# You can create a function for this in your shellrc (.bashrc, .zshrc).
wal-tile() {
    wal -n -i "$@"
    feh --bg-tile "$(< "${HOME}/.cache/wal/wal")"
}
# }}}

# Oh-My-Zsh {{{
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git zsh-syntax-highlighting)
## zsh-syntax-highlighting
#source ./zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
plugins+=(k)
source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# ssh
# export SSH_KEY_PATH="~/.ssh/dsa_id"

### Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# }}}

# ffmpeg function {{{
## S: ffmpeg screen capture and upload Google Drive
## Dependencies : ffmpeg, google-drive-ocamlfuse
## by nietz
ffx_init () {
    # vaapi hw acceelration capture example
    # ffmpeg -probesize 100M  -vaapi_device /dev/dri/renderD128 -video_size 1920x1080 -framerate 60 -thread_queue_size 1024 -f x11grab -i :0.0 -thread_queue_size 1024 -f pulse -i alsa_output.platform-snd_aloop.0.analog-stereo.monitor -vf 'format=nv12,hwupload' -c:v h264_vaapi -vsync 1 -profile:v 100 -level 41 -b:v 0 -g 60 -bf 2 -qp 20 -i_qfactor 1 -i_qoffset 0 -b_qfactor 1.2 -b_qoffset 0 -quality 0 -c:a libopus -af "adelay=1,aresample=async=1,volume=2" -ar 48000 -ac 2 -b:a 256k -vbr on -compression_level 10 -strict -2 test.mp4 
    FFX_FFMPEG="/usr/bin/ffmpeg"
    FFX_OUTPUT_DIR="${HOME}/Videos/Screencast"
    FFX_OUTPUT_CONTAINER="mkv"
    FFX_OUTPUT_FILE="$FFX_OUTPUT_DIR/Screencast-$(date +%Y-%m-%d_%H%M%S).$FFX_OUTPUT_CONTAINER"
    FFX_WINDOW_BORDER="12"          # Window Manger's [Border X 2]
    # Note: -vf is optional delete if you want, -y is to overwrite existing file
    #
    FFX_GOOGLE_DRIVE_OCAMLFUSE="/usr/bin/google-drive-ocamlfuse"
    FFX_MOUNT_POINT="${HOME}/googledrive/ultralisk"
    FFX_GDRIVE_SUB_DIR="File/Screencast"

    FFX_X11_FORMAT="x11grab"
    FFX_TTY_FORMAT="fbdev"
    FFX_TTY_DEVICE="/dev/fb0"
    FFX_MONO="1"
    FFX_STEREO="2"
    FFX_ALSA="hw:1,0"       # -f alsa "hw:1,0"  # run 'cat /proc/asound/pcm' and select
    #FFX_PULSE="alsa_input.pci-0000_00_1b.0.analog-stereo"
    #FFX_PULSE=alsa_output.platform-snd_aloop.0.analog-stereo.monitor
    #FFX_PULSE="combined.monitor"
    FFX_PULSE=$(echo $(pacmd ls | grep "Default source" | cut -f2 -d ":"))       # pactl list source # -f pulse "pulse device_name"

    FFX_FULL_SIZE=$(xwininfo -root | grep 'geometry' | awk '{print $2;}' | grep -oEe '[0-9]+x[0-9]+')
    FFX_PRIMARY_POS=$(xrandr | awk '/ connected primary / {print $4}' | grep -oEe '\+[0-9]+\+[0-9]+' | sed -e 's/\+//' | sed -e 's/\+/,/')
    FFX_PRIMARY_SIZE=$(xrandr | awk '/ connected primary / {print $4}' | grep -oEe '[0-9]+x[0-9]+')
    FFX_SECONDARY_POS=$(xrandr | grep -E "connected [0-9]+" | awk '/ connected / {print $3}' | grep -oEe '\+[0-9]+\+[0-9]+' | sed -e 's/\+//' | sed -e 's/\+/,/')
    FFX_SECONDARY_SIZE=$(xrandr | grep -E "connected [0-9]+" | awk '/ connected / {print $3}'| grep -oEe '[0-9]+x[0-9]+')

    FFX_VIDEO="libx264"
    FFX_VF_OPTION=""
    FFX_VIDEO_PRESET="ultrafast"
    FFX_VIDEO_CRF="23"
    FFX_VIDEO_OPTION=""
    FFX_HW_VIDEO="h264_vaapi"
    FFX_HW_DEVICE="-vaapi_device /dev/dri/renderD128"
    FFX_HW_VF_OPTION="-vf format=nv12,hwupload"
    FFX_HW_VIDEO_OPTION="-vsync 1 -profile:v 100 -level 41 -b:v 0 -g 60 -bf 2 -qp 20 -i_qfactor 1 -i_qoffset 0 -b_qfactor 1.2 -b_qoffset 0 -quality 0"

    FFX_AUDIO="pcm_s16le"       # libfdk_aac, aac, vorbis(ffmpeg default)
    FFX_AF_OPTION=""            # "-af adelay=1,aresample=async=1,volume=2"
    FFX_AUDIO_OPTION="-ar 48000 -ac 2 -b:a 160k -vbr on -compression_level 10 -strict -2"
    FFX_PROBESIZE="500M"
    FFX_FRAMERATE="60"
    FFX_THREAD_QUEUE_SIZE="1024"
    FFX_THREAD="0"
    FFX_VIDEO_VF_SCALE="scale=1280:720"
    # -async 1 -vsync 1
}

ffx_full ()
{
    echo "full caputre: $FFX_FULL_SIZE"
    echo $FFX_OUTPUT_FILE
    echo "==============================================="

    $FFX_FFMPEG -probesize $FFX_PROBESIZE -video_size $FFX_FULL_SIZE -framerate $FFX_FRAMERATE \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f $FFX_X11_FORMAT -i :0.0 \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f pulse -i $FFX_PULSE \
        -c:v $FFX_VIDEO -preset $FFX_VIDEO_PRESET -crf $FFX_VIDEO_CRF \
        -c:a $FFX_AUDIO -ac $FFX_STEREO \
        $FFX_OUTPUT_FILE
}

ffx_primary ()
{
    echo "primary mornitor caputre: [pos $FFX_PRIMARY_POS] [size $FFX_PRIMARY_SIZE]"
    echo $FFX_OUTPUT_FILE
    echo "==============================================="

    $FFX_FFMPEG -probesize $FFX_PROBESIZE -video_size $FFX_PRIMARY_SIZE -framerate $FFX_FRAMERATE \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f $FFX_X11_FORMAT -i :0.0+$FFX_PRIMARY_POS \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f pulse -i $FFX_PULSE \
        -c:v $FFX_VIDEO -preset $FFX_VIDEO_PRESET -crf $FFX_VIDEO_CRF \
        -c:a $FFX_AUDIO -ac $FFX_STEREO \
        $FFX_OUTPUT_FILE -async 1 -vsync 1
}

ffx_secondary ()
{
    echo "secondary mornitor caputre: [pos $FFX_SECONDARY_POS] [size $FFX_SECONDARY_SIZE]"
    echo $FFX_OUTPUT_FILE
    echo "==============================================="

    $FFX_FFMPEG -probesize $FFX_PROBESIZE -video_size $FFX_SECONDARY_SIZE -framerate $FFX_FRAMERATE \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f $FFX_X11_FORMAT -i :0.0+$FFX_SECONDARY_POS \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f pulse -i $FFX_PULSE \
        -c:v $FFX_VIDEO -preset $FFX_VIDEO_PRESET -crf $FFX_VIDEO_CRF \
        -c:a $FFX_AUDIO -ac $FFX_STEREO \
        $FFX_OUTPUT_FILE
}

ffx_selected_window ()
{
    local FFX_SELECTED_WINDOW_GEOMETRY=`xwininfo -frame`
    local FFX_SELECTED_WINDOW_POS=$(echo $FFX_SELECTED_WINDOW_GEOMETRY | grep -oEe 'Corners:\s+\+[0-9]+\+[0-9]+' | grep -oEe '[0-9]+\+[0-9]+' | sed -e 's/\+/,/')
    #local FFX_SELECTED_WINDOW_SIZE=$(echo $FFX_SELECTED_WINDOW_GEOMETRY | grep -oEe 'geometry [0-9]+x[0-9]+' | grep -oEe '[0-9]+x[0-9]+')
    local FFX_SELECTED_WINDOW_WIDTH=$(echo $FFX_SELECTED_WINDOW_GEOMETRY | grep -i 'width' | head -n1 | grep -oEe '[0-9]+')
    local FFX_SELECTED_WINDOW_HEIGHT=$(echo $FFX_SELECTED_WINDOW_GEOMETRY | grep -i 'height' | head -n1 | grep -oEe '[0-9]+')
    if [[ $1 = "border" ]];then
        FFX_SELECTED_WINDOW_WIDTH=`expr $FFX_SELECTED_WINDOW_WIDTH + $FFX_WINDOW_BORDER`
        FFX_SELECTED_WINDOW_HEIGHT=`expr $FFX_SELECTED_WINDOW_HEIGHT + $FFX_WINDOW_BORDER`
        echo WINDOW BORDER: $FFX_WINDOW_BORDER
    fi
    local FFX_SELECTED_WINDOW_SIZE=$FFX_SELECTED_WINDOW_WIDTH"x"$FFX_SELECTED_WINDOW_HEIGHT
    echo "selected window capture: [pos $FFX_SELECTED_WINDOW_POS] [size $FFX_SELECTED_WINDOW_SIZE]"
    echo $FFX_OUTPUT_FILE
    echo "==============================================="

    $FFX_FFMPEG -probesize $FFX_PROBESIZE -video_size $FFX_SELECTED_WINDOW_SIZE -framerate $FFX_FRAMERATE \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f $FFX_X11_FORMAT -i :0.0+$FFX_SELECTED_WINDOW_POS \
        -thread_queue_size $FFX_THREAD_QUEUE_SIZE -f pulse -i $FFX_PULSE \
        -c:v $FFX_VIDEO -preset $FFX_VIDEO_PRESET -crf $FFX_VIDEO_CRF \
        -c:a $FFX_AUDIO -ac $FFX_STEREO \
        $FFX_OUTPUT_FILE
}

ffx_full_hw ()
{
}

ffx_primary_hw ()
{
}

ffx_secondary_hw ()
{
}

ffx_selected_window_hw ()
{
}

ffx_tty ()
{
}

ffx ()
{
    if [ ! -f $FFX_FFMPEG ]; then
       return 
    fi

    ffx_init

    mkdir -p $FFX_OUTPUT_DIR || { printf "%s\n" "Error: Couldn't create screenshot directory"; return; }


    if [ $# -eq 0 ]; then
        ffx_full
    fi

    case $1 in
        l)      ffx_primary                 ;;
        hwl)    ffx_primary_hw              ;;
        r)      ffx_secondary               ;;
        hwr)    ffx_secondary_hw            ;;
        s)      ffx_selected_window         ;;
        sb)     ffx_selected_window border  ;;
        hws)    ffx_selected_window_hw      ;;
        f)      ffx_full                    ;;
        hwf)    ffx_full_hw                 ;;
        t)      ffx_tty                     ;;
        *)      return                      ;;
    esac

    local OUTPUT_SIZE=$(echo $(du $FFX_OUTPUT_FILE | awk '{print $1}'))
    echo
    echo "==============================================================="
    echo "File size: $OUTPUT_SIZE K"

    if [[ $OUTPUT_SIZE = "0" ]]; then
        rm $FFX_OUTPUT_FILE
        return 1
    fi

    if [[ $2 = "upload" ]]; then

        if [ ! -f $FFX_GOOGLE_DRIVE_OCAMLFUSE ]; then
            return
        fi

        local ICON="/home/nietz/.local/share/icons/Snowy/apps/scalable/google-drive.png"

        #if ! pidof "$(type google-drive-ocamlfuse)" >/dev/null; then
        if ! mountpoint -q $FFX_MOUNT_POINT; then
            $FFX_GOOGLE_DRIVE_OCAMLFUSE $FFX_MOUNT_POINT
            sleep 5
        fi

        echo
        echo "uploading to googledrive"    # need install google-drive-ocamlfuse
        dunstify -p --appname=Gdrive -i $ICON "Google Drive
         Uploading.... to GDrive
         $FFX_OUTPUT_FILE"

        cp $FFX_OUTPUT_FILE $FFX_MOUNT_POINT/$FFX_GDRIVE_SUB_DIR || { printf "%s\n" "Error: Couldn't upload to GDrive"; return; }

        dunstify -p --appname=Gdrive -i $ICON "Google Drive
         Complete Uploading to GDrive
         $FFX_OUTPUT_FILE"

         echo
         echo "Uploading completed...."
    fi
}
# }}}

# fzf {{{
## mpd
fmpc() {
  local song_position
  song_position=$(mpc -f "%position%) %artist% - %title%" playlist | \
    fzf-tmux --header="♬ MPD Playlist Queue" --query="$1" --reverse --select-1 --exit-0 | \
    sed -n 's/^\([0-9]\+\)).*/\1/p') || return 1
  [ -n "$song_position" ] && mpc -q play $song_position
}

## Preview: Try highlight, coderay, rougify in turn, then fall back to cat
fcat () {
  fzf --preview '[[ $(file --mime {}) =~ binary ]] &&
               echo {} is a binary file ||
               (highlight -O ansi -l {} ||
               coderay {} ||
               rougify {} ||
               cat {}) 2> /dev/null | head -500'
}

TRAPWINCH() {
  zle && { zle reset-prompt; zle -R }
}

fzf-history-widget-accept() {
  fzf-history-widget
  zle accept-line
}

#zle     -N     fzf-history-widget-accept
#bindkey '^X^R' fzf-history-widget-accept
alias ffile="fzf --height 40% --reverse --preview 'file {}' --preview-window down:1"

[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# }}}

# Man pages {{{
### color for man pages
export LESS_TERMCAP_mb=$'\e[0;35m'
export LESS_TERMCAP_md=$'\e[0;31m'
export LESS_TERMCAP_me=$'\e[0;32m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[0;46;30m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[0;34m'
# }}}
source ~/zshrc.d/aliases.zsh

# opam configuration
test -r /home/nietz/.opam/opam-init/init.zsh && . /home/nietz/.opam/opam-init/init.zsh > /dev/null 2> /dev/null || true

# gem
#PATH=$PATH:/home/nietz/.gem/ruby/2.5.0/gem
#export PATH

# python venv
export WORKON_HOME=~/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

* fzf

  $ ffile


 $ fcat