diff options
| author | Teddy Wing | 2016-09-17 21:10:17 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-09-17 21:10:17 -0400 |
| commit | fac1321b1b459fb885fa1a49f84a803d61ad998e (patch) | |
| tree | 20d48130b43562bf673dfc6f7b62f81a5fb89a4d | |
| parent | 6cd7b7da10759eb88e5ff3c4dd49a60ac8dacf3b (diff) | |
| download | irssi-slack-profile-fac1321b1b459fb885fa1a49f84a803d61ad998e.tar.bz2 | |
print_whois: Pad labels to vertically align colons
Previously, if field labels were longer than five charaters, they
wouldn't be aligned with the colons `:` of the other fields in WHOIS
output.
The lack of alignment didn't look good.
This change pads the field labels with spaces on their right side to
make the colons line up visually and vertically.
We also make a more fundamental change to how the subroutine works.
Instead of trying to print fields immediately using `maybe_print_field`,
they are instead added to an array of hashes. When we want to print
fields, we loop over this array and only print those fields that contain
non-blank values.
| -rw-r--r-- | slack_profile.pl | 76 |
1 files changed, 60 insertions, 16 deletions
diff --git a/slack_profile.pl b/slack_profile.pl index 1f9f3a7..cd34179 100644 --- a/slack_profile.pl +++ b/slack_profile.pl @@ -266,12 +266,14 @@ sub find_user { sub print_whois { my ($user) = @_; - sub maybe_print_field { - my ($name, $value) = @_; + # Append spaces to the end of $label such that the length of the result is + # equal to $length + sub pad_label { + my ($label, $length) = @_; - if ($value) { - Irssi::print(" $name : $value"); - } + my $padding = $length - length $label; + + $label . ' ' x $padding; } my $bot = ''; @@ -280,22 +282,64 @@ sub print_whois { $bot = ' (bot)'; } - Irssi::print($user->{'name'} . $bot); - maybe_print_field('name ', $user->{'real_name'}); - maybe_print_field('title', $user->{'profile'}->{'title'}); - maybe_print_field('email', $user->{'profile'}->{'email'}); - maybe_print_field('phone', $user->{'profile'}->{'phone'}); - maybe_print_field('skype', $user->{'profile'}->{'skype'}); - maybe_print_field('tz ', $user->{'tz_label'}); + my @fields = ( + { + label => 'name', + value => $user->{'real_name'}, + }, + { + label => 'title', + value => $user->{'profile'}->{'title'}, + }, + { + label => 'email', + value => $user->{'profile'}->{'email'}, + }, + { + label => 'phone', + value => $user->{'profile'}->{'phone'}, + }, + { + label => 'skype', + value => $user->{'profile'}->{'skype'}, + }, + { + label => 'tz', + value => $user->{'tz_label'}, + }, + ); foreach my $key (keys %{$user->{'fields'}}) { - my $label = $user->{'fields'}->{$key}->{'label'}; - my $value = $user->{'fields'}->{$key}->{'value'}; + push @fields, { + label => $user->{'fields'}->{$key}->{'label'}, + value => $user->{'fields'}->{$key}->{'value'}, + }; + } - maybe_print_field($label, $value); + push @fields, { + label => 'status', + value => $user->{'presence'}, + }; + + # Determine the longest label so we can pad others accordingly + my $max_label_length = 0; + for my $field (@fields) { + my $length = length $field->{'label'}; + if ($length > $max_label_length) { + $max_label_length = $length; + } } - maybe_print_field('status', $user->{'presence'}); + Irssi::print($user->{'name'} . $bot); + + for my $field (@fields) { + if ($field->{'value'}) { + # Pad field labels so that the colons line up vertically + my $label = pad_label($field->{'label'}, $max_label_length); + + Irssi::print(" $label : $field->{'value'}"); + } + } Irssi::print('End of SWHOIS'); } |
