Skip to content

Commit c3004ea

Browse files
Move terminal width calculation to it's own function for readability
1 parent 462d7cc commit c3004ea

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

diff-so-fancy

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -589,22 +589,7 @@ sub trim {
589589
# Print a line of em-dash or line-drawing chars the full width of the screen
590590
sub horizontal_rule {
591591
my $color = $_[0] || "";
592-
593-
# Make width static so we only calculate it once
594-
state $width;
595-
if (!$width) {
596-
my $tput = `tput cols 2>1`;
597-
if (!$tput) {
598-
print color('orange') . "Warning: `tput cols` did not return numeric input" . color('reset') . "\n";
599-
$tput = 80;
600-
}
601-
602-
$width = $ruler_width || $tput;
603-
}
604-
605-
if (is_windows()) {
606-
$width--;
607-
}
592+
my $width = get_terminal_width();
608593

609594
# em-dash http://www.fileformat.info/info/unicode/char/2014/index.htm
610595
#my $dash = "\x{2014}";
@@ -879,4 +864,34 @@ sub starts_with_ansi {
879864
}
880865
}
881866

867+
sub get_terminal_width {
868+
# Make width static so we only calculate it once
869+
state $width;
870+
871+
if ($width) {
872+
return $width;
873+
}
874+
875+
# If there is a ruler width in the config we use that
876+
if ($ruler_width) {
877+
$width = $ruler_width;
878+
# Otherwise we check the terminal width using tput
879+
} else {
880+
my $tput = `tput cols`;
881+
882+
if ($tput) {
883+
$width = int($tput);
884+
885+
if (is_windows()) {
886+
$width--;
887+
}
888+
} else {
889+
print color('orange') . "Warning: `tput cols` did not return numeric input" . color('reset') . "\n";
890+
$width = 80;
891+
}
892+
}
893+
894+
return $width;
895+
}
896+
882897
# vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4

0 commit comments

Comments
 (0)