From 484fb558be4778072dc89d44ee4aab181d696410 Mon Sep 17 00:00:00 2001 From: asymmetric <101816+asymmetric@users.noreply.github.com> Date: Sun, 29 Dec 2024 09:52:57 +0100 Subject: [PATCH] fix: fallback logic if tput is not available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * README: add `ncurses` to deps * fix: fallback logic if tput is not available --------- Co-authored-by: Stéphane Lesimple --- btrfs-list | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/btrfs-list b/btrfs-list index 6c61cfb..310a6c8 100755 --- a/btrfs-list +++ b/btrfs-list @@ -171,7 +171,14 @@ sub run_cmd { my ($_stdin, $_stdout, $_stderr); $_stderr = gensym; debug("about to run_cmd ['" . join("','", @$cmd) . "']"); - my $pid = open3($_stdin, $_stdout, $_stderr, @$cmd); + my $pid = eval { open3($_stdin, $_stdout, $_stderr, @$cmd); }; + if ($@) { + if ($fatal) { + print STDERR "FATAL: failed to run [" . join(' ', @$cmd) . "] ($@)\n"; + exit 1; + } + return {status => -1, stdout => [], stderr => []}; + } ## end if ($@) debug("waiting for cmd to complete..."); my @stdout = (); my @stderr = (); @@ -243,9 +250,29 @@ sub pretty_print { my $bright = ($opt_bright ? 'bright_' : ''); CORE::state($nbcolors, $dark); if (!defined $nbcolors) { + # try to use tput if we happen to have it my $cmd = run_cmd(cmd => [qw{ tput colors }], silent_stderr => 1); - $nbcolors = $cmd->{stdout}->[0]; - chomp $nbcolors; + if ($cmd->{'status'} == -1 || !@{$cmd->{stdout}}) { + # we don't have tput, get info from env instead + if ($ENV{'TERM'}) { + if ($ENV{'TERM'} =~ /256/) { + $nbcolors = 256; + } + elsif ($ENV{'TERM'} =~ /dumb/) { + $nbcolors = -1; + } + else { + $nbcolors = 8; + } + } ## end if ($ENV{'TERM'}) + else { + $nbcolors = -1; + } + } ## end if ($cmd->{'status'} ==...) + else { + $nbcolors = $cmd->{stdout}->[0]; + chomp $nbcolors; + } $nbcolors = 8 if !$nbcolors; debug("nbcolors=$nbcolors"); $dark = ($nbcolors <= 8 ? "${bright}black" : 'grey9');