diff options
Diffstat (limited to '.local/bin')
-rwxr-xr-x | .local/bin/cmdfmt | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/.local/bin/cmdfmt b/.local/bin/cmdfmt new file mode 100755 index 0000000..37218d7 --- /dev/null +++ b/.local/bin/cmdfmt | |||
@@ -0,0 +1,34 @@ | |||
1 | #!/usr/bin/env perl | ||
2 | |||
3 | use strict; | ||
4 | |||
5 | my $textwidth = $ARGV[0] // 80; | ||
6 | # TODO: get tabstop from vim as well <17-11-21, yigit> # | ||
7 | my $tabstop = ' ' x 4; | ||
8 | |||
9 | my $str = do { local $/; <STDIN> }; | ||
10 | |||
11 | my $sofar = ""; | ||
12 | my $buffer = ""; | ||
13 | |||
14 | while ($str =~ m/(.*?)\s(-?-\w+)\s+/g) { | ||
15 | my $pos = pos $str; | ||
16 | |||
17 | # TODO: use tabstop here instead of 4 <17-11-21, yigit> # | ||
18 | if (length("$buffer $sofar ") + $pos < $textwidth - 4) { | ||
19 | if ($sofar eq "") { | ||
20 | $sofar = "$buffer"; | ||
21 | } else { | ||
22 | $sofar = "$sofar $buffer "; | ||
23 | } | ||
24 | $sofar = $sofar . $1; | ||
25 | $buffer = $2; | ||
26 | } else { | ||
27 | print "$sofar \\\n$tabstop$buffer "; | ||
28 | $sofar = $1; | ||
29 | $buffer = $2; | ||
30 | $str = substr $str, $pos; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | print "$sofar $buffer $str"; | ||