summaryrefslogtreecommitdiffstats
path: root/.local/bin/cmdfmt
blob: 634e9c635e0750dfab6048aafee706d9405b0b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env perl

use strict;

my $textwidth = $ARGV[0] // 80;

if ($textwidth == 0) {
    $textwidth = 80;
}

# TODO: get tabstop from vim as well <17-11-21, yigit> #
my $tabstop = ' ' x 4;

my $str = do { local $/; <STDIN> };

my $sofar = "";
my $buffer = "";

while ($str =~ m/(.*?)\s(-?-\w+)\s+/g) {
    my $pos = pos $str;

    # TODO: use tabstop here instead of 4 <17-11-21, yigit> #
    if (length("$buffer $sofar ") + $pos < $textwidth - 4) {
        if ($sofar eq "") {
            $sofar = "$buffer";
        } else {
            $sofar = "$sofar $buffer ";
        }
        $sofar = $sofar . $1;
        $buffer = $2;
    } else {
        print "$sofar \\\n$tabstop$buffer ";
        $sofar = $1;
        $buffer = $2;
        $str = substr $str, $pos;
    }
}

print "$sofar $buffer $str";