diff --git a/dot_config/zsh/direnv.zsh b/dot_config/zsh/direnv.zsh new file mode 100644 index 0000000..2d51151 --- /dev/null +++ b/dot_config/zsh/direnv.zsh @@ -0,0 +1,102 @@ +#!/usr/bin/env zsh + +autoload colors is-at-least + +BOLD="bold" +NONE="NONE" + +[[ -z "$DIRENV_HOME" ]] && export DIRENV_HOME="$HOME/.config/direnv" + +ZSH_DIRENV_VERSION_FILE=${DIRENV_HOME}/version.txt + +_zsh_direnv_log() { + local font=$1 + local color=$2 + local msg=$3 + + if [ $font = $BOLD ] + then + echo $fg_bold[$color] "[zsh-direnv-plugin] $msg" $reset_color + else + echo $fg[$color] "[zsh-direnv-plugin] $msg" $reset_color + fi +} + +_zsh_direnv_last_version() { + echo $(curl -s https://api.github.com/repos/direnv/direnv/releases | grep tag_name | head -n 1 | cut -d '"' -f4) +} + +_zsh_direnv_download_install() { + local version=$1 + local machine + case "$(uname -m)" in + x86_64) + machine=amd64 + ;; + i686 | i386) + machine=386 + ;; + aarch64) + machine=arm64 + ;; + *) + _zsh_direnv_log $BOLD "red" "Machine $(uname -m) not supported by this plugin" + return 1 + ;; + esac + _zsh_direnv_log $NONE "blue" " -> download and install direnv ${version}" + wget -qc --no-check-certificate https://github.com/direnv/direnv/releases/download/${version}/direnv.${OSTYPE%-*}-${machine} -O "${DIRENV_HOME}/direnv" + chmod +x "${DIRENV_HOME}/direnv" + echo ${version} > ${ZSH_DIRENV_VERSION_FILE} +} + +_zsh_direnv_install() { + _zsh_direnv_log $NONE "blue" "#############################################" + _zsh_direnv_log $BOLD "blue" "Installing direnv..." + _zsh_direnv_log $NONE "blue" "-> creating direnv home dir : ${DIRENV_HOME}" + mkdir -p ${DIRENV_HOME} || _zsh_direnv_log $NONE "green" "dir already exist" + local last_version=$(_zsh_direnv_last_version) + _zsh_direnv_log $NONE "blue" "-> retrieve last version of direnv..." + _zsh_direnv_download_install ${last_version} + if [ $? -ne 0 ] + then + _zsh_direnv_log $BOLD "red" "Install KO" + else + _zsh_direnv_log $BOLD "green" "Install OK" + fi + _zsh_direnv_log $NONE "blue" "#############################################" +} + +update_zsh_direnv() { + _zsh_direnv_log $NONE "blue" "#############################################" + _zsh_direnv_log $BOLD "blue" "Checking new version of direnv..." + + local current_version=$(cat ${ZSH_DIRENV_VERSION_FILE}) + local last_version=$(_zsh_direnv_last_version) + + if is-at-least ${last_version#v*} ${current_version#v*} + then + _zsh_direnv_log $BOLD "green" "Already up to date, current version : ${current_version}" + else + _zsh_direnv_log $NONE "blue" "-> Updating direnv..." + _zsh_direnv_download_install ${last_version} + _zsh_direnv_log $BOLD "green" "Update OK" + fi + _zsh_direnv_log $NONE "blue" "#############################################" +} + +_zsh_direnv_load() { + # export PATH + export PATH=${PATH}:${DIRENV_HOME} + eval "$(direnv hook zsh)" +} + +# install direnv if it isnt already installed +[[ ! -f "${ZSH_DIRENV_VERSION_FILE}" ]] && _zsh_direnv_install + +# load direnv if it is installed +if [[ -f "${ZSH_DIRENV_VERSION_FILE}" ]]; then + _zsh_direnv_load +fi + +unset -f _zsh_direnv_install _zsh_direnv_load diff --git a/dot_config/zsh/sshagent.zsh b/dot_config/zsh/sshagent.zsh new file mode 100644 index 0000000..e6147d0 --- /dev/null +++ b/dot_config/zsh/sshagent.zsh @@ -0,0 +1,31 @@ +SSH_ENV="$HOME/.ssh/environment" +KEY="$HOME/.ssh/id_rsa" + +function start_agent () { + ssh-agent -s | sed 's/^echo/#echo/' > "$SSH_ENV" + chmod 600 "$SSH_ENV" + . "$SSH_ENV" > /dev/null +} + +function add_identities () { + ssh-add -l | grep "The agent has no identities" > /dev/null + if [ $? -eq 0 ]; then + ssh-add $KEY &> /dev/null + fi +} + +function is_ssh_agent_pid_valid () { + ps -ef | grep $1 | grep -v grep | grep ssh-agent > /dev/null +} + +if [ -n "$SSH_AGENT_PID" ] && is_ssh_agent_pid_valid $SSH_AGENT_PID; then + add_identities +else + if [ -f "$SSH_ENV" ]; then + . "$SSH_ENV" > /dev/null + fi + if ! is_ssh_agent_pid_valid $SSH_AGENT_PID; then + start_agent + fi + add_identities +fi diff --git a/dot_config/zsh/starship.toml b/dot_config/zsh/starship.toml new file mode 100644 index 0000000..7844f19 --- /dev/null +++ b/dot_config/zsh/starship.toml @@ -0,0 +1,4 @@ +add_newline = false + +[time] +disabled = false diff --git a/dot_p10k.zsh b/dot_p10k.zsh new file mode 100644 index 0000000..18a3d81 --- /dev/null +++ b/dot_p10k.zsh @@ -0,0 +1,198 @@ +# Generated by Powerlevel10k configuration wizard on 2021-07-02 at 12:03 CEST. +# Based on romkatv/powerlevel10k/config/p10k-pure.zsh, checksum 13301. +# Wizard options: nerdfont-complete + powerline, small icons, pure, rpromt, 24h time, +# 2 lines, compact, transient_prompt, instant_prompt=verbose. +# Type `p10k configure` to generate another config. +# +# Config file for Powerlevel10k with the style of Pure (https://github.com/sindresorhus/pure). +# +# Differences from Pure: +# +# - Git: +# - `@c4d3ec2c` instead of something like `v1.4.0~11` when in detached HEAD state. +# - No automatic `git fetch` (the same as in Pure with `PURE_GIT_PULL=0`). +# +# Apart from the differences listed above, the replication of Pure prompt is exact. This includes +# even the questionable parts. For example, just like in Pure, there is no indication of Git status +# being stale; prompt symbol is the same in command, visual and overwrite vi modes; when prompt +# doesn't fit on one line, it wraps around with no attempt to shorten it. +# +# If you like the general style of Pure but not particularly attached to all its quirks, type +# `p10k configure` and pick "Lean" style. This will give you slick minimalist prompt while taking +# advantage of Powerlevel10k features that aren't present in Pure. + +# Temporarily change options. +'builtin' 'local' '-a' 'p10k_config_opts' +[[ ! -o 'aliases' ]] || p10k_config_opts+=('aliases') +[[ ! -o 'sh_glob' ]] || p10k_config_opts+=('sh_glob') +[[ ! -o 'no_brace_expand' ]] || p10k_config_opts+=('no_brace_expand') +'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand' + +() { + emulate -L zsh -o extended_glob + + # Unset all configuration options. + unset -m '(POWERLEVEL9K_*|DEFAULT_USER)~POWERLEVEL9K_GITSTATUS_DIR' + + # Zsh >= 5.1 is required. + autoload -Uz is-at-least && is-at-least 5.1 || return + + # Prompt colors. + local grey='242' + local red='#FF5C57' + local yellow='#F3F99D' + local blue='#57C7FF' + local magenta='#FF6AC1' + local cyan='#9AEDFE' + local white='#F1F1F0' + + # Left prompt segments. + typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=( + # =========================[ Line #1 ]========================= + context # user@host + dir # current directory + vcs # git status + # =========================[ Line #2 ]========================= + newline # \n + # virtualenv # python virtual environment + prompt_char # prompt symbol + ) + + # Right prompt segments. + typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=( + # =========================[ Line #1 ]========================= + command_execution_time # previous command duration + # virtualenv # python virtual environment + # context # user@host + time # current time + # =========================[ Line #2 ]========================= + newline # \n + ) + + # Basic style options that define the overall prompt look. + typeset -g POWERLEVEL9K_BACKGROUND= # transparent background + typeset -g POWERLEVEL9K_{LEFT,RIGHT}_{LEFT,RIGHT}_WHITESPACE= # no surrounding whitespace + typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SUBSEGMENT_SEPARATOR=' ' # separate segments with a space + typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SEGMENT_SEPARATOR= # no end-of-line symbol + typeset -g POWERLEVEL9K_VISUAL_IDENTIFIER_EXPANSION= # no segment icons + + # Add an empty line before each prompt except the first. This doesn't emulate the bug + # in Pure that makes prompt drift down whenever you use the Alt-C binding from fzf or similar. + typeset -g POWERLEVEL9K_PROMPT_ADD_NEWLINE=false + + # Magenta prompt symbol if the last command succeeded. + typeset -g POWERLEVEL9K_PROMPT_CHAR_OK_{VIINS,VICMD,VIVIS}_FOREGROUND=$magenta + # Red prompt symbol if the last command failed. + typeset -g POWERLEVEL9K_PROMPT_CHAR_ERROR_{VIINS,VICMD,VIVIS}_FOREGROUND=$red + # Default prompt symbol. + typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIINS_CONTENT_EXPANSION='❯' + # Prompt symbol in command vi mode. + typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VICMD_CONTENT_EXPANSION='❮' + # Prompt symbol in visual vi mode is the same as in command mode. + typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIVIS_CONTENT_EXPANSION='❮' + # Prompt symbol in overwrite vi mode is the same as in command mode. + typeset -g POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE=false + + # Grey Python Virtual Environment. + typeset -g POWERLEVEL9K_VIRTUALENV_FOREGROUND=$grey + # Don't show Python version. + typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=false + typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER= + + # Blue current directory. + typeset -g POWERLEVEL9K_DIR_FOREGROUND=$blue + + # Context format when root: user@host. The first part white, the rest grey. + typeset -g POWERLEVEL9K_CONTEXT_ROOT_TEMPLATE="%F{$white}%n%f%F{$grey}@%m%f" + # Context format when not root: user@host. The whole thing grey. + typeset -g POWERLEVEL9K_CONTEXT_TEMPLATE="%F{$grey}%n@%m%f" + # Don't show context unless root or in SSH. + typeset -g POWERLEVEL9K_CONTEXT_{DEFAULT,SUDO}_CONTENT_EXPANSION= + + # Show previous command duration only if it's >= 5s. + typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=5 + # Don't show fractional seconds. Thus, 7s rather than 7.3s. + typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=0 + # Duration format: 1d 2h 3m 4s. + typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_FORMAT='d h m s' + # Yellow previous command duration. + typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_FOREGROUND=$yellow + + # Grey Git prompt. This makes stale prompts indistinguishable from up-to-date ones. + typeset -g POWERLEVEL9K_VCS_FOREGROUND=$grey + + # Disable async loading indicator to make directories that aren't Git repositories + # indistinguishable from large Git repositories without known state. + typeset -g POWERLEVEL9K_VCS_LOADING_TEXT= + + # Don't wait for Git status even for a millisecond, so that prompt always updates + # asynchronously when Git state changes. + typeset -g POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS=0 + + # Cyan ahead/behind arrows. + typeset -g POWERLEVEL9K_VCS_{INCOMING,OUTGOING}_CHANGESFORMAT_FOREGROUND=$cyan + # Don't show remote branch, current tag or stashes. + typeset -g POWERLEVEL9K_VCS_GIT_HOOKS=(vcs-detect-changes git-untracked git-aheadbehind) + # Don't show the branch icon. + typeset -g POWERLEVEL9K_VCS_BRANCH_ICON= + # When in detached HEAD state, show @commit where branch normally goes. + typeset -g POWERLEVEL9K_VCS_COMMIT_ICON='@' + # Don't show staged, unstaged, untracked indicators. + typeset -g POWERLEVEL9K_VCS_{STAGED,UNSTAGED,UNTRACKED}_ICON= + # Show '*' when there are staged, unstaged or untracked files. + typeset -g POWERLEVEL9K_VCS_DIRTY_ICON='*' + # Show '⇣' if local branch is behind remote. + typeset -g POWERLEVEL9K_VCS_INCOMING_CHANGES_ICON=':⇣' + # Show '⇡' if local branch is ahead of remote. + typeset -g POWERLEVEL9K_VCS_OUTGOING_CHANGES_ICON=':⇡' + # Don't show the number of commits next to the ahead/behind arrows. + typeset -g POWERLEVEL9K_VCS_{COMMITS_AHEAD,COMMITS_BEHIND}_MAX_NUM=1 + # Remove space between '⇣' and '⇡' and all trailing spaces. + typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='${${${P9K_CONTENT/⇣* :⇡/⇣⇡}// }//:/ }' + + # Grey current time. + typeset -g POWERLEVEL9K_TIME_FOREGROUND=$grey + # Format for the current time: 09:51:02. See `man 3 strftime`. + typeset -g POWERLEVEL9K_TIME_FORMAT='%D{%H:%M:%S}' + # If set to true, time will update when you hit enter. This way prompts for the past + # commands will contain the start times of their commands rather than the end times of + # their preceding commands. + typeset -g POWERLEVEL9K_TIME_UPDATE_ON_COMMAND=false + + # Transient prompt works similarly to the builtin transient_rprompt option. It trims down prompt + # when accepting a command line. Supported values: + # + # - off: Don't change prompt when accepting a command line. + # - always: Trim down prompt when accepting a command line. + # - same-dir: Trim down prompt when accepting a command line unless this is the first command + # typed after changing current working directory. + typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=always + + # Instant prompt mode. + # + # - off: Disable instant prompt. Choose this if you've tried instant prompt and found + # it incompatible with your zsh configuration files. + # - quiet: Enable instant prompt and don't print warnings when detecting console output + # during zsh initialization. Choose this if you've read and understood + # https://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt. + # - verbose: Enable instant prompt and print a warning when detecting console output during + # zsh initialization. Choose this if you've never tried instant prompt, haven't + # seen the warning, or if you are unsure what this all means. + typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose + + # Hot reload allows you to change POWERLEVEL9K options after Powerlevel10k has been initialized. + # For example, you can type POWERLEVEL9K_BACKGROUND=red and see your prompt turn red. Hot reload + # can slow down prompt by 1-2 milliseconds, so it's better to keep it turned off unless you + # really need it. + typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true + + # If p10k is already loaded, reload configuration. + # This works even with POWERLEVEL9K_DISABLE_HOT_RELOAD=true. + (( ! $+functions[p10k] )) || p10k reload +} + +# Tell `p10k configure` which file it should overwrite. +typeset -g POWERLEVEL9K_CONFIG_FILE=${${(%):-%x}:a} + +(( ${#p10k_config_opts} )) && setopt ${p10k_config_opts[@]} +'builtin' 'unset' 'p10k_config_opts' diff --git a/dot_zshenv b/dot_zshenv new file mode 100644 index 0000000..64d4d9a --- /dev/null +++ b/dot_zshenv @@ -0,0 +1,7 @@ +export WINPATH=$(tr ':' '\n' <<<"$PATH" | grep -E '^(/mnt)/c/' | paste -sd ':') +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$WINPATH" +export ZDOTDIR="$HOME/.config/dotfiles/zsh" + +[[ -n $SSH_CONNECTION ]] && export EDITOR='vim' || export EDITOR='nvim' + +[[ -f "$ZDOTDIR/local/env.zsh" ]] && source "$ZDOTDIR/local/env.zsh" diff --git a/dot_zshrc b/dot_zshrc new file mode 100644 index 0000000..f954381 --- /dev/null +++ b/dot_zshrc @@ -0,0 +1,105 @@ +# Enable Powerlevel10k instant prompt +[[ -r "$HOME/.cache/p10k-instant-prompt-${(%):-%n}.zsh" ]] && source "$HOME/.cache/p10k-instant-prompt-${(%):-%n}.zsh" + +export SPACEVIMDIR="$HOME/.config/spacevim/" +export FZF_MARKS_FILE=$HOME/.config/fzfmarks +export STARSHIP_CONFIG=$HOME/.config/zsh/starship.toml +export ZSH_EVALCACHE_DIR="$HOME/.cache/evalcache" + +# Setopt Stuff +setopt multios prompt_subst auto_pushd pushd_ignore_dups pushdminus inc_append_history + +# Zinit Clone if not exist +[[ ! -f $HOME/.config/zinit/bin/zinit.zsh ]] && command git clone https://github.com/zdharma/zinit $HOME/.config/zinit/bin + +# Zinit +declare -A ZINIT +ZINIT[BIN_DIR]="$HOME/.config/zinit/bin" +ZINIT[HOME_DIR]="$HOME/.config/zinit" +ZINIT[ZCOMPDUMP_PATH]="$HOME/.cache/zsh/compdump" +source $HOME/.config/zinit/bin/zinit.zsh + +# Programs +zinit ice as"program" from"gh-r" mv"fd* -> fd" pick"fd/fd"; zinit light "sharkdp/fd" +zinit ice as"program" from"gh-r" mv"bat* -> bat" pick"bat/bat"; zinit light "sharkdp/bat" +zinit ice as"program" from"gh-r" mv"exa* -> exa"; zinit light "ogham/exa" +zinit ice as"program" from"gh-r" mv"ripgrep* -> ripgrep" pick"ripgrep/rg"; zinit light "BurntSushi/ripgrep" +zinit ice as"program" from"gh-r" mv"jq-* -> jq"; zinit light "stedolan/jq" + +# Completions and Autosuggestions +zinit ice lucid blockf atpull'zinit creinstall -q .'; zinit light "zsh-users/zsh-completions" +zinit ice lucid atload"_zsh_autosuggest_start"; zinit light "zsh-users/zsh-autosuggestions" + +# History +export SAVEHIST=20000 +export HISTFILE=$HOME/.cache/zsh/history + +# Aliases +alias ls='exa -al --color=always --group-directories-first' +alias cat='bat' +alias rm='rm -i' +alias cp='cp -i' +alias mv='mv -i' +alias grep='grep --color=auto' +alias fgrep='fgrep --color=auto' +alias egrep='egrep --color=auto' +alias diff='diff --color=auto' +alias ip='ip --color=auto' +alias vim='nvim' + +alias sc-status="sudo systemctl status" +alias sc-start="sudo systemctl start" +alias sc-stop="sudo systemctl stop" +alias sc-reload="sudo systemctl reload" +alias sc-restart="sudo systemctl restart" +alias sc-enable="sudo systemctl enable" +alias sc-disable="sudo systemctl disable" +alias sc-daemon-reload="sudo systemctl daemon-reload" + +alias termbin="nc termbin.com 9999" + +# Library Files +source $HOME/.config/zsh/direnv.zsh +source $HOME/.config/zsh/sshagent.zsh +zinit pack for ls_colors +zinit snippet OMZL::history.zsh +zinit snippet OMZL::functions.zsh +zinit snippet OMZL::completion.zsh +zinit snippet OMZL::directories.zsh +zinit snippet OMZL::key-bindings.zsh +zinit snippet OMZL::termsupport.zsh +zinit snippet OMZL::clipboard.zsh +zinit snippet OMZL::grep.zsh + +# Completion Stuff +zstyle ':completion:*:*:*:users' ignored-patterns '*' +zstyle -e ':completion:*:(ssh|scp|sftp|rsh|rsync):hosts' hosts 'reply=(${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })' +zstyle ':completion:*:(scp|rsync):*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *' +zstyle ':completion:*:(scp|rsync):*' group-order users files all-files hosts-domain hosts-host hosts-ipaddr +zstyle ':completion:*:ssh:*' tag-order users 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *' +zstyle ':completion:*:ssh:*' group-order hosts-domain hosts-host users hosts-ipaddr +zstyle ':completion:*:*:*:hosts-host' ignored-patterns '*(.|:)*' loopback ip6-loopback localhost localhost4 localhost6 ip6-localhost broadcasthost pxm +zstyle ':completion:*:*:*:hosts-domain' ignored-patterns '<->.<->.<->.<->' '^[-[:alnum:]]##(.[-[:alnum:]]##)##' '*@*' '*localdomain*' '*.eeleater.org' '*.local' +zstyle ':completion:*:*:*:hosts-ipaddr' ignored-patterns '^(<->.<->.<->.<->|(|::)([[:xdigit:].]##:(#c,2))##(|%*))' '127.0.0.<->' '255.255.255.255' '::1' 'fe80::*' + +# Plugins +zinit ice lucid; zinit light "mroth/evalcache" +zinit ice lucid; zinit snippet OMZP::command-not-found +zinit ice lucid; zinit snippet OMZP::fzf +zinit ice lucid; zinit light "b4b4r07/enhancd" +zinit ice lucid; zinit light "jreese/zsh-titles" +zinit ice lucid; zinit light "urbainvaes/fzf-marks" +zinit ice lucid; zinit light "Aloxaf/fzf-tab" +zinit ice lucid; zinit light "wfxr/forgit" +zinit ice lucid; zinit light "reegnz/jq-zsh-plugin" +[[ -f /home/linuxbrew/.linuxbrew/bin/brew ]] && _evalcache /home/linuxbrew/.linuxbrew/bin/brew shellenv + +# syntax highlighting +zinit ice lucid atinit"ZINIT[COMPINIT_OPTS]=-C; zpcompinit; zpcdreplay"; zinit light "zdharma/fast-syntax-highlighting" + +# Theme +zinit ice depth=1; zinit light "romkatv/powerlevel10k" +[[ ! -f "$ZDOTDIR/.p10k.zsh" ]] || source "$ZDOTDIR/.p10k.zsh" # p10k configure || nano ~/.config/dotfiles/zsh/.p10k.zsh + +# Local RC +[[ -f "$HOME/.config/local/rc.zsh" ]] && source "$HOME/.config/local/rc.zsh"