代码挑战:Bash提示路径缩短器 [英] Code challenge: Bash prompt path shortener
问题描述
我实现了一个快速路径缩短器,用于将bash包含在PS1环境变量中,从而将工作目录缩短为更紧凑但仍具有描述性的目录.我很好奇还有什么其他想法.
I implemented a prompt path shortener for bash to be included in the PS1 environment variable, which shortens the working directory into something more compact but still descriptive. I'm curious what other ideas may exist.
这是挑战:
创建一个bash函数_dir_chomp
,它可以像这样包含在PS1中(插入换行符以提高可读性):
Create a bash function _dir_chomp
which can be included into PS1 like this (line breaks inserted for readability):
PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] $(
_dir_chomp "$(pwd)" 20
)\[\033[01;37m\]$(parse_git_branch)\[\033[01;34m\] \$\[\033[00m\] '
最大长度的参数"20"作为软限制.这些是示例:
with "20" being the parameter for the maximum length as soft limit. These are the examples:
-
/usr/portage/media-plugins/banshee-community-extensions/files
变为/u/p/m/b/files
-
/home/user1/media/video/music/live-sets
变为~/m/v/m/live-sets
(请注意〜字符代替$ HOME) -
/home/user2/media
不变(不超过20个字符的限制) -
/home/user1/this-is-a-very-long-path-name-with-more-than-20-chars
变为~/this-is-a-very-long-path-name-with-more-than-20-chars
(未缩短最后一个组件:软限制) -
/home/user1/src
变为~/src
($ HOME总是缩短) -
/home/user1/.kde4/share/config/kresources
变为~/.k/s/c/kresources
(请注意保留前缀点)
/usr/portage/media-plugins/banshee-community-extensions/files
becomes/u/p/m/b/files
/home/user1/media/video/music/live-sets
becomes~/m/v/m/live-sets
(note the ~ character as replacement for $HOME)/home/user2/media
does NOT change (20 char limit not exceeded)/home/user1/this-is-a-very-long-path-name-with-more-than-20-chars
becomes~/this-is-a-very-long-path-name-with-more-than-20-chars
(last component stays unshortened: soft limit)/home/user1/src
becomes~/src
($HOME always shortened)/home/user1/.kde4/share/config/kresources
becomes~/.k/s/c/kresources
(note the prefixing dot is preserved)
当前用户为user1.
Current user is user1.
允许调用诸如awk
,perl
,ruby
,python
之类的外部解释器,但不能调用已编译的C程序或类似程序.换句话说:不允许外部源文件,代码必须是内联的.最短的版本获胜. bash函数主体(称为子函数)的长度很重要,意思是:
It's allowed to call external interpreters like awk
, perl
, ruby
, python
but not compiled C programs or similar. In other words: external source files are not allowed, code must be inline. Shortest version wins. The length of the bash function body (and called sub functions) counts, means:
_sub_helper() {
# this counts
}
_dir_chomp() {
# these characters count (between { and })
_sub_helper "foobar" # _sub_helper body counts, too
}
推荐答案
纯Bash:
_dir_chomp () {
local IFS=/ c=1 n d
local p=(${1/#$HOME/\~}) r=${p[*]}
local s=${#r}
while ((s>$2&&c<${#p[*]}-1))
do
d=${p[c]}
n=1;[[ $d = .* ]]&&n=2
((s-=${#d}-n))
p[c++]=${d:0:n}
done
echo "${p[*]}"
}
出于测试目的,我假设该问题意味着当前用户为"user1".
For purposes of testing, I'm assuming that the question means that the current user is "user1".
注意:Bash 4具有变量PROMPT_DIRTRIM
,它通过根据其值保留子目录的数量并将其余部分替换为...
Note: Bash 4 has a variable PROMPT_DIRTRIM
that shortens the \w
escape in PS1
by retaining the number of sub-directories according to its value and replacing the rest with ...
/$ PROMPT_DIRTRIM=2
/$ echo $PS1
\w\$
/$ pwd
/
/$ cd /usr/share/doc/bash
.../doc/bash$
这篇关于代码挑战:Bash提示路径缩短器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!