Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dir module: shorten path like fish shell #897

Open
knorr3 opened this issue Dec 21, 2020 · 6 comments · May be fixed by #1265
Open

Dir module: shorten path like fish shell #897

knorr3 opened this issue Dec 21, 2020 · 6 comments · May be fixed by #1265
Labels
good first issue Issue or PR is friendly to new contributors: easy and simple. proposal An issue (rarely PR) for feature-requests, ideas, etc

Comments

@knorr3
Copy link

knorr3 commented Dec 21, 2020

This request is about improving the dir module.
It shortens the path, so the prompt is less cluttered.

Describe the solution you'd like

I want to shorten the pwd to be just like the zsh fishy theme or fish shell.

Implementation

Maybe one can adapt the current fishy solution:

local i pwd
pwd=("${(s:/:)PWD/#$HOME/~}")
if (( $#pwd > 1 )); then
  for i in {1..$(($#pwd-1))}; do
    if [[ "$pwd[$i]" = .* ]]; then
      pwd[$i]="${${pwd[$i]}[1,2]}"
    else
      pwd[$i]="${${pwd[$i]}[1]}"
    fi
  done
fi
dir="${(j:/:)pwd}"

(Source: https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/fishy.zsh-theme)

Documentation, adoption

Variable Default Meaning
SPACESHIP_DIR_SHOW true Show directory section
SPACESHIP_DIR_PREFIX in· Prefix before current directory
SPACESHIP_DIR_SUFFIX $SPACESHIP_PROMPT_DEFAULT_SUFFIX Suffix after current directory
SPACESHIP_DIR_TRUNC 3 Number of folders of cwd to show in prompt, 0 to show all
SPACESHIP_DIR_TRUNC_PREFIX   Prefix before cwd when it's truncated. For example …/ or .../, empty to disable
SPACESHIP_DIR_TRUNC_REPO true While in git repo, show only root directory and folders inside it
SPACESHIP_DIR_SHORT false Enable to shorten the displayed path like /etc/apache2/extra -> /e/a/extra

Screen Shot 2020-12-21 at 9 38 18 AM

@denysdovhan denysdovhan added the proposal An issue (rarely PR) for feature-requests, ideas, etc label Jun 11, 2021
@denysdovhan
Copy link
Member

This is an interesting proposal. We might try this under an option.

@denysdovhan denysdovhan added the good first issue Issue or PR is friendly to new contributors: easy and simple. label Jun 11, 2021
@rjsu26
Copy link

rjsu26 commented Aug 24, 2021

Hi. I want to work on this issue. Is it available?

@denysdovhan
Copy link
Member

@rjsu26 sure, go for it.

@ryzheboka
Copy link

Hi, can I try to implement it?

@bugless-soup
Copy link

Hello! I'm trying to implement this feature.
I've edited dir.zsh in the following way:

  • Added the setting SPACESHIP_DIR_SHORT
  • Changed the dir assignments so that they don't concatenate it with trunc_prefix anymore
  • Pasted the code from fish shell, using print -P "$dir" as input
  • Added a final row that concatenates those two variables

All tests are passing.
Is this good enough for a pull request?

dir.zsh:

#
# Working directory
#
# Current directory. Return only three last items of path

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------

SPACESHIP_DIR_SHOW="${SPACESHIP_DIR_SHOW=true}"
SPACESHIP_DIR_PREFIX="${SPACESHIP_DIR_PREFIX="in "}"
SPACESHIP_DIR_SUFFIX="${SPACESHIP_DIR_SUFFIX="$SPACESHIP_PROMPT_DEFAULT_SUFFIX"}"
SPACESHIP_DIR_TRUNC="${SPACESHIP_DIR_TRUNC=3}"
SPACESHIP_DIR_TRUNC_PREFIX="${SPACESHIP_DIR_TRUNC_PREFIX=}"
SPACESHIP_DIR_TRUNC_REPO="${SPACESHIP_DIR_TRUNC_REPO=true}"
SPACESHIP_DIR_SHORT="${SPACESHIP_DIR_SHORT=false}"
SPACESHIP_DIR_COLOR="${SPACESHIP_DIR_COLOR="cyan"}"
SPACESHIP_DIR_LOCK_SYMBOL="${SPACESHIP_DIR_LOCK_SYMBOL=" "}"
SPACESHIP_DIR_LOCK_COLOR="${SPACESHIP_DIR_LOCK_COLOR="red"}"

# ------------------------------------------------------------------------------
# Section
# ------------------------------------------------------------------------------

spaceship_dir() {
  [[ $SPACESHIP_DIR_SHOW == false ]] && return

  local 'dir' 'trunc_prefix'

  # Threat repo root as a top-level directory or not
  if [[ $SPACESHIP_DIR_TRUNC_REPO == true ]] && spaceship::is_git; then
    local git_root=$(git rev-parse --show-toplevel)

    if (cygpath --version) >/dev/null 2>/dev/null; then
      git_root=$(cygpath -u $git_root)
    fi

    # Check if the parent of the $git_root is "/"
    if [[ $git_root:h == / ]]; then
      trunc_prefix="$git_root"
    else
      trunc_prefix="$SPACESHIP_DIR_TRUNC_PREFIX$git_root:t"
    fi

    # `${NAME#PATTERN}` removes a leading prefix PATTERN from NAME.
    # `$~~` avoids `GLOB_SUBST` so that `$git_root` won't actually be
    # considered a pattern and matched literally, even if someone turns that on.
    # `$git_root` has symlinks resolved, so we use `${PWD:A}` which resolves
    # symlinks in the working directory.
    # See "Parameter Expansion" under the Zsh manual.
    dir="${${PWD:A}#$~~git_root}"
  else
    if [[ SPACESHIP_DIR_TRUNC -gt 0 ]]; then
      # `%(N~|TRUE-TEXT|FALSE-TEXT)` replaces `TRUE-TEXT` if the current path,
      # with prefix replacement, has at least N elements relative to the root
      # directory else `FALSE-TEXT`.
      # See "Prompt Expansion" under the Zsh manual.
      trunc_prefix="%($((SPACESHIP_DIR_TRUNC + 1))~|$SPACESHIP_DIR_TRUNC_PREFIX|)"
    fi

    dir="%${SPACESHIP_DIR_TRUNC}~"
  fi

  if [[ $SPACESHIP_DIR_SHORT == true ]]; then
    local cwd
    cwd=("${(s:/:)$(print -P "$dir")}")
    if (( $#cwd > 1 )); then
      local i
      for i in {1..$(($#cwd-1))}; do
        if [[ "$cwd[$i]" = .* ]]; then
          cwd[$i]="${${cwd[$i]}[1,2]}"
        else
          cwd[$i]="${${cwd[$i]}[1]}"
        fi
      done
    fi
    dir="${(j:/:)cwd}"
  fi

  dir="$trunc_prefix$dir"

  if [[ ! -w . ]]; then
    SPACESHIP_DIR_SUFFIX="%F{$SPACESHIP_DIR_LOCK_COLOR}${SPACESHIP_DIR_LOCK_SYMBOL}%f${SPACESHIP_DIR_SUFFIX}"
  fi

  spaceship::section \
    "$SPACESHIP_DIR_COLOR" \
    "$SPACESHIP_DIR_PREFIX" \
    "$dir" \
    "$SPACESHIP_DIR_SUFFIX"
}

@tin-pham
Copy link

Second this

@knorr3 knorr3 linked a pull request Oct 17, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue or PR is friendly to new contributors: easy and simple. proposal An issue (rarely PR) for feature-requests, ideas, etc
Development

Successfully merging a pull request may close this issue.

6 participants