diff --git a/nowrap b/nowrap index cfd5243..f351775 100755 --- a/nowrap +++ b/nowrap @@ -77,19 +77,10 @@ use open ':locale'; my $TABSTOP = 8; my $ESCAPE_SEQUENCE_PATTERN = qr/(\e\[\d*(;\d+)*m)/; -my $columns = `tput cols`; -chomp($columns); my $isWrap = 0; my $indentString = ''; my $indentLength = 0; - -if ($columns and $ENV{TERM} eq "cygwin") { - # use one less than the number of columns when running on Windows under - # cygwin. Thanks to Ingo Karkat: https://github.com/goodell/nowrap/issues/2 - --$columns; -} - -$columns = 80 unless $columns; +my $columns; GetOptions( "help" => \&print_usage_and_exit, @@ -108,6 +99,19 @@ GetOptions( }, ) or die "unable to parse options, stopped"; +unless (defined $columns) { + $columns = `tput cols`; + chomp($columns); + + if ($columns and $ENV{TERM} eq "cygwin") { + # use one less than the number of columns when running on Windows under + # cygwin. Thanks to Ingo Karkat: https://github.com/goodell/nowrap/issues/2 + --$columns; + } + + $columns = 80 unless $columns; +} + if ($columns < $indentLength + 1) { die "--columns too small to accommodate --indent-string and any characters, stopped"; } diff --git a/script/nowrap.pl b/script/nowrap.pl index 02aae5a..a69cfd3 100755 --- a/script/nowrap.pl +++ b/script/nowrap.pl @@ -31,19 +31,10 @@ my $TABSTOP = 8; my $ESCAPE_SEQUENCE_PATTERN = qr/(\e\[\d*(;\d+)*m)/; -my $columns = `tput cols`; -chomp($columns); my $isWrap = 0; my $indentString = ''; my $indentLength = 0; - -if ($columns and $ENV{TERM} eq "cygwin") { - # use one less than the number of columns when running on Windows under - # cygwin. Thanks to Ingo Karkat: https://github.com/goodell/nowrap/issues/2 - --$columns; -} - -$columns = 80 unless $columns; +my $columns; GetOptions( "help" => \&print_usage_and_exit, @@ -62,6 +53,19 @@ }, ) or die "unable to parse options, stopped"; +unless (defined $columns) { + $columns = `tput cols`; + chomp($columns); + + if ($columns and $ENV{TERM} eq "cygwin") { + # use one less than the number of columns when running on Windows under + # cygwin. Thanks to Ingo Karkat: https://github.com/goodell/nowrap/issues/2 + --$columns; + } + + $columns = 80 unless $columns; +} + if ($columns < $indentLength + 1) { die "--columns too small to accommodate --indent-string and any characters, stopped"; } diff --git a/tests/test.sh b/tests/test.sh index ea8eba9..a8cc1a4 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -114,3 +114,27 @@ else exit 1 fi +# make sure nowrap doesn't invoke 'tput' if '-c' is passed +( + # TERM is the one that matters (breaks 'tput cols'), but COLUMNS and LINES + # are to avoid any clever fallback that might get added to nowrap in the + # future + unset TERM + unset COLUMMS + unset LINES + + # send stderr to /dev/null because nohup always warns about redirecting its output + nohup ${NOWRAP} --columns=72 tc0.in 2>/dev/null + status=$? + if [[ $status -ne 0 ]] ; then + echo "ERROR: non-zero exit code ($status)" + fi + + if $DIFF tc0.expected nohup.out ; then + : + else + echo "ERROR: nohup run failed (expected != output)" + fi + + rm -f nohup.out +)