osi-tools/lib/indent

45 lines
1.2 KiB
Plaintext
Raw Normal View History

2023-11-10 06:18:15 +00:00
#!/usr/bin/env perl
2024-08-12 07:08:22 +00:00
# Taken from: https://git.parabola.nu/packages/libretools.git/tree/src/chroot-tools/indent?id=f24f9c272ce7b1b8fa1773ed63ca77bfd71ee254
# Relicensed from GPLv3+ to AGPLv3+.
#
2023-11-10 06:18:15 +00:00
# Copyright (C) 2013 Luke Shumaker <lukeshu@parabola.nu>
2024-08-12 07:08:22 +00:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2023-11-10 06:18:15 +00:00
use warnings;
use strict;
use constant BUFFER_SIZE => 40;
binmode(STDIN);
binmode(STDOUT);
exit(1) if ($#ARGV != 0);
my $indent = $ARGV[0];
# 0: no indent has been printed for this line, an indent WILL need to be printed
# 1: an indent needs to be printed for this line IFF there is any more output on it
# 2: no indent (currently) needs to be printed for this line
my $print_indent = 0;
my $buffer;
my $size;
my $c;
while (1) {
$size = sysread(STDIN, $buffer, BUFFER_SIZE);
last if ($size < 1);
for (0..$size-1) {
$c = substr($buffer, $_, 1);
if ($c eq "\n") {
syswrite(STDOUT, $indent) if ($print_indent == 0);
syswrite(STDOUT, $c, 1);
$print_indent = 0;
} elsif ($c eq "\r") {
syswrite(STDOUT, $c, 1);
$print_indent = 1 if ($print_indent == 2);
} else {
syswrite(STDOUT, $indent) if ($print_indent < 2);
syswrite(STDOUT, $c, 1);
$print_indent = 2;
}
}
}