Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pasberth committed Aug 25, 2012
0 parents commit 98a0f9c
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# pathf

パスの変換とかいろいろするスクリプト言語。

使い方:

pathf script [basepath]

basepath を省略した場合は $PWD が使用されます。

スクリプトの共通的な仕様:

ちょうど正規表現の `\s``\S``\w``\W` みたいに、
`a` の対になる命令は必ず `A` です

たとえば `b``[b]asename` の略ですが、 `dirname` に相当するものは `d` ではなくて `B` です

例:

# [b]asename
pathf b # = basename $PWD

# un[B]asename
# dirname と違い、 `dirname /` は `.` を出力
pathf B # = dirname $PWD
pathf BB # = dirname $(dirname $PWD)
pathf Bb # = basename $(dirname $PWD)

# [c]ar path
pathf c

# [C]dr path
patf C

# [t]ilde
pathf t # = もし $HOME が含まれていれば ~ に置換される
pathf Bt # = dirname をした上で ~ に置換
pathf tB # = $HOME を ~ に置換した上で dirname.

# [d]elete prefix
# $PWD から /path/to を削除
# $PWD が /path/to から始まる場合、 /path/to を削除したパスを出力
# $PWD が /path/to から始まらない場合、なにも出力しない
echo /path/to | pathf d

122 changes: 122 additions & 0 deletions bin/pathf
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# -*- sh -*-

if [ $# -eq 1 ]; then
SCRIPT=$1; shift
BASEPATH=$PWD
elif [ $# -eq 2 ]; then
SCRIPT=$1; shift
BASEPATH=`realpath "$1"`; shift
else
echo "usage: $0 -[OPTIONS] SCRIPT [BASEPATH]" 1>&2
exit 1
fi

A=$BASEPATH
RESULT=($A)

next_path () {
local NEXT_PATH

if read NEXT_PATH; then
eval echo $NEXT_PATH
else
echo "$0: One or more argument PATH was required but not given." 1>&2
exit 1
fi
}

while [ -n "$SCRIPT" ]; do
case $SCRIPT in
t*)
SCRIPT=${SCRIPT##t}
A=${A/$HOME/\~}
RESULT=($A)
continue;;
b*)
SCRIPT=${SCRIPT##b}
A=`basename "$A"`
RESULT=($A)
continue;;
B*)
SCRIPT=${SCRIPT##B}
if [ "$A" = "/" ]; then
A="."
else
A=`dirname "$A"`
fi
RESULT=($A)
continue;;
c*)
SCRIPT=${SCRIPT##c}
if [ "$A" = "~" ]; then
A=\~
RESULT=($A)
continue
fi

A=${A%%${A#*/}}
if [ "$A" = "/" ]; then
A="/"
else
A=${A%%/}
fi
RESULT=($A)
continue;;
C*)
SCRIPT=${SCRIPT##C}
TMP=$A
A=${A#*/}
if #[ "$A" = "/" ] ||
#[ "$A" = "${A#*/}" ] ||
[ -z "$A" ] || \
[ "$TMP" = "$A" ]; then
A="."
else
A=$A #${A#*/}
fi
RESULT=($A)
continue;;
d*)
SCRIPT=${SCRIPT##d}
DEL_PATH=`next_path`

if [ -z "$DEL_PATH" ]; then
A=$A
# If $DEL_PATH is subdirectory of $A,
# Dones print nothing. For example:
# DEL_PATH = /usr/local
# A = /usr
elif [ "$DEL_PATH" != "${DEL_PATH##$A}" ]; then
A=""
# If $A isn't parent directory of $DEL_PATH,
# Dones print nothing. For example:
# DEL_PATH = /usr
# A = /etc
elif [ "$A" = "${A##$DEL_PATH}" ]; then
A=""
elif [ -z "$DEL_PATH" ]; then
A=$A
else
A=${A##${DEL_PATH%/}/}
fi
RESULT=($A)
continue;;
D*)
SCRIPT=${SCRIPT##D}
DEL_PATH=`next_path`
if [ "$A" = "${A##$DEL_PATH}" ]; then
A=""
else
A=$DEL_PATH
fi
RESULT=($A)
continue;;
*)
echo "invalid expression: $SCRIPT" 1>&2
exit 1
esac
done

for r in "${RESULT[@]}"; do
echo "$r"
done
58 changes: 58 additions & 0 deletions spec/pathf_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

ENV["PATH"] = File.dirname(__FILE__) + "/../bin:" + ENV["PATH"]

describe "the 'pathf' command" do

example "[b]asename"do
`pathf b /home/pasberth`.should == "pasberth\n"
end

example "un[B]asename (dirname)" do
`pathf B /home/pasberth`.should == "/home\n"
end

example "[c]ar" do
`pathf c /home/pasberth`.should == "/\n"
end

example "un[C]ar (cdr)" do
`pathf C /home/pasberth`.should == "home/pasberth\n"
end

example "[t]ilde" do
`pathf t $HOME`.should == "~\n"
end
end

describe "[b]asename" do

it "returns '/' to '/'." do
`pathf b /`.should == "/\n"
end

it "returns '~' to '~'." do
`pathf b \\~`.should == "~\n"
end

it "doesn't returns '/' even if the path is end with '/'."do
`pathf b /home/pasberth/`.should == "pasberth\n"
end
end

describe "un[B]asename (dirname)" do

it "returns '.' to '/'" do
`pathf B /`.should == ".\n"
end
end

describe "[t]ilde" do

it "returns '~' to '~'" do
`pathf tb $HOME`.should == "~\n"
end

it "returns '.' to '~'" do
`pathf tB $HOME`.should == ".\n"
end
end
14 changes: 14 additions & 0 deletions spec/pathf_spec.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

ENV["PATH"] = File.dirname(__FILE__) + "/../bin:" + ENV["PATH"]
p ENV["PATH"]

describe "the 'pathf' command" do

example "[b]asename"do
`pathf b /home/pasberth`.should == "pasberth\n"
end

example "[c]ar" do
`pathf c /home/pasberth`.should == "/\n"
end
end

0 comments on commit 98a0f9c

Please sign in to comment.