From a0d26185d3b78862f1f8399392122a3df08c631d Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 17 Nov 2021 12:17:17 +0300 Subject: bin: tracking command formatter --- .local/bin/cmdfmt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 .local/bin/cmdfmt (limited to '.local/bin/cmdfmt') 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 @@ +#!/usr/bin/env perl + +use strict; + +my $textwidth = $ARGV[0] // 80; +# TODO: get tabstop from vim as well <17-11-21, yigit> # +my $tabstop = ' ' x 4; + +my $str = do { local $/; }; + +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"; -- cgit v1.2.3-70-g09d2