Skip to content

Commit 255a8db

Browse files
committed
add new scripts
1 parent 756457c commit 255a8db

File tree

6 files changed

+159
-37
lines changed

6 files changed

+159
-37
lines changed

2line

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# @Function
3+
# echo each arguments on one line colorfully.
4+
#
5+
# @Usage
6+
# $ ./2line arg1 arg2
7+
# $ ./2line *.txt
8+
#
9+
# @author Jerry Lee
10+
11+
12+
colorEcho() {
13+
local color="$1"
14+
shift
15+
if [ -c /dev/stdout ] ; then
16+
# if stdout is console, turn on color output.
17+
echo -ne "\033[1;${color}m"
18+
echo -n "$@"
19+
echo -e "\033[0m"
20+
else
21+
echo "$@"
22+
fi
23+
}
24+
25+
readonly ECHO_COLORS=(37 31 32 34 33 35 56)
26+
COUNT=0
27+
28+
for a in "$@"; do
29+
colorEcho "${ECHO_COLORS[$((COUNT++ % ${#ECHO_COLORS[@]}))]}" "$a"
30+
done

ap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# @Function
3+
# convert to absolute path.
4+
#
5+
# @Usage
6+
# $ ./ap
7+
# $ ./ap a.txt ../dir1/b.txt
8+
#
9+
# @author Jerry Lee
10+
11+
[ $# -eq 0 ] && files=(.) || files=("$@")
12+
13+
for f in "${files[@]}" ; do
14+
! [ -e $f ] && {
15+
echo "$f does not exists!"
16+
continue
17+
}
18+
readlink -f "$f"
19+
done

c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
# @Function
3+
# Run command and put output to system clipper.
4+
#
5+
# @Usage
6+
# $ ./c echo "hello world!"
7+
# $ echo "hello world!" | c
8+
#
9+
# @author Jerry Lee
10+
11+
readonly PROG=`basename $0`
12+
13+
copy() {
14+
local name=$(uname | tr A-Z a-z)
15+
16+
case "${name}" in
17+
darwin*)
18+
pbcopy ;;
19+
cygwin*)
20+
clip ;;
21+
mingw*)
22+
clip ;;
23+
*)
24+
xsel -b ;;
25+
esac
26+
}
27+
28+
teeAndCopy() {
29+
tee >(content="$(cat)"; echo -n "$content" | copy)
30+
}
31+
32+
if [ $# -eq 0 ]; then
33+
teeAndCopy
34+
else
35+
"$@" | teeAndCopy
36+
fi

colines

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Function
4+
# cat lines colorfully. colines means COL(orfule L)INES.
5+
#
6+
# @Usage
7+
# $ echo -n 'Hello\nWorld' | colines
8+
# $ colines /path/to/file1
9+
# $ colines /path/to/file1 /path/to/file2
10+
11+
12+
__author__ = 'Jerry Lee'
13+
14+
import sys
15+
16+
ECHO_COLORS = (37, 31, 32, 34, 33, 35, 56)
17+
idx = 0
18+
19+
20+
def color_print(*args):
21+
line = " ".join(args)
22+
global idx
23+
idx += 1
24+
color = ECHO_COLORS[idx % len(ECHO_COLORS)]
25+
if sys.stdout.isatty():
26+
line_ = """\033[1;%sm%s\033[0m""" % (color, line)
27+
print line_
28+
else:
29+
print(line)
30+
31+
32+
if __name__ == '__main__':
33+
if len(sys.argv) > 1:
34+
for arg in sys.argv[1:]:
35+
if len(sys.argv) > 2:
36+
print('=' * 80)
37+
print(arg)
38+
print('=' * 80)
39+
for line in open(arg).readlines():
40+
color_print(line.rstrip('\r\n'))
41+
else:
42+
# Read from std input
43+
while True:
44+
line = sys.stdin.readline()
45+
if not line:
46+
break
47+
color_print(line.rstrip('\r\n'))

colorful-lines

Lines changed: 0 additions & 37 deletions
This file was deleted.

rp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
[ $# -eq 0 ] && {
3+
echo "ERROR: NO argument!"
4+
exit 1
5+
}
6+
7+
[ $# -eq 1 ] && {
8+
relativeTo=.
9+
files=("$@")
10+
} || {
11+
argv=("$@")
12+
argc=$#
13+
14+
# Get last argument
15+
relativeTo="${argv[$((argc - 1))]}"
16+
files=( "${argv[@]:0:$((argc - 1))}" )
17+
}
18+
19+
[ -f "$relativeTo" ] && relativeTo="$(dirname "$relativeTo")"
20+
21+
for f in "${files[@]}" ; do
22+
! [ -e $f ] && {
23+
echo "$f does not exists!"
24+
continue
25+
}
26+
realpath "$f" --relative-to="$relativeTo"
27+
done

0 commit comments

Comments
 (0)