Farewell Perl. Hello Python
This commit is contained in:
parent
9416a9e209
commit
f00781edb4
2 changed files with 44 additions and 49 deletions
39
bin/s2t
39
bin/s2t
|
|
@ -1,19 +1,30 @@
|
|||
#!/usr/bin/env perl
|
||||
#!/usr/bin/env python3
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
import sys
|
||||
|
||||
my $N_SPACES = $ARGV[0] || 4;
|
||||
DEFAULT_N_SPACES = 4
|
||||
TAB, SPACE = ' ', ' '
|
||||
|
||||
while (my $line = <STDIN>) {
|
||||
# Skip empty lines
|
||||
print "\n" and next if $line !~ m/\S/;
|
||||
try:
|
||||
n_spaces = int(sys.argv[1])
|
||||
except:
|
||||
n_spaces = DEFAULT_N_SPACES
|
||||
|
||||
$line =~ m/^(\s*)(.+)$/;
|
||||
my $n_spaces = length($1);
|
||||
my $n_tabs = ($n_spaces > 0 and $n_spaces < $N_SPACES)
|
||||
? 1
|
||||
: int($n_spaces / $N_SPACES);
|
||||
if n_spaces < 1:
|
||||
n_spaces = DEFAULT_N_SPACES
|
||||
|
||||
print "\t" x $n_tabs . "$2\n";
|
||||
}
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip()
|
||||
n_leading_spaces = 0
|
||||
|
||||
for ch in line:
|
||||
if ch != SPACE:
|
||||
break
|
||||
n_leading_spaces += 1
|
||||
|
||||
if 0 < n_leading_spaces < n_spaces:
|
||||
n_tabs = 1
|
||||
else:
|
||||
n_tabs = n_leading_spaces // n_spaces
|
||||
|
||||
print(TAB * n_tabs, line[n_leading_spaces:], sep='')
|
||||
|
|
|
|||
56
bin/t2s
56
bin/t2s
|
|
@ -1,42 +1,26 @@
|
|||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env python3
|
||||
|
||||
n_spaces="$1"
|
||||
import sys
|
||||
|
||||
if [ ! "$n_spaces" ]; then
|
||||
n_spaces=4
|
||||
fi
|
||||
DEFAULT_N_SPACES = 4
|
||||
TAB, SPACE = ' ', ' '
|
||||
|
||||
if [ $n_spaces -lt 1 ]; then
|
||||
echo 'Sorry. Number of spaces should be a positive number'
|
||||
exit 1
|
||||
fi
|
||||
try:
|
||||
n_spaces = int(sys.argv[1])
|
||||
except:
|
||||
n_spaces = DEFAULT_N_SPACES
|
||||
|
||||
space=''
|
||||
i=0
|
||||
while [ true ]; do
|
||||
space="$space "
|
||||
i=$(( $i + 1 ))
|
||||
if [ $i -eq $n_spaces ]; then
|
||||
if n_spaces < 1:
|
||||
n_spaces = DEFAULT_N_SPACES
|
||||
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip()
|
||||
n_tabs = 0
|
||||
|
||||
for ch in line:
|
||||
if ch != TAB:
|
||||
break
|
||||
fi
|
||||
done
|
||||
n_tabs += 1
|
||||
|
||||
while IFS='' read -r line || [[ -n "$line" ]]; do
|
||||
if [[ ${line:0:1} != ' ' ]]; then
|
||||
echo "$line"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Line length
|
||||
line_len=${#line}
|
||||
|
||||
# Get leading tabs and their length
|
||||
tabs=$(echo "$line" | sed -n 's/^\( *\)\(.*\)$/\1/p')
|
||||
tabs_len=${#tabs}
|
||||
|
||||
line_without_tabs_len=$(( $line_len - $tabs_len ))
|
||||
line_without_tabs=${line:$tabs_len:$line_without_tabs_len}
|
||||
|
||||
spaces=$(echo "$tabs" | sed "s/ /$space/g")
|
||||
echo "$spaces$line_without_tabs"
|
||||
done
|
||||
spaces = SPACE * n_spaces * n_tabs
|
||||
print(spaces, line[n_tabs:], sep='')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue