diff options
| author | Teddy Wing | 2016-09-17 21:15:27 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-09-17 21:15:27 -0400 |
| commit | 03fd89f3d99249d9a53757d7e4d0283f7e5bf63a (patch) | |
| tree | 15db550368908c82f2ac2a057c1a439ab64c4077 | |
| parent | 6cd7b7da10759eb88e5ff3c4dd49a60ac8dacf3b (diff) | |
| parent | c6712fd2b0ff0742f5528c261122c26ae72229eb (diff) | |
| download | irssi-slack-profile-03fd89f3d99249d9a53757d7e4d0283f7e5bf63a.tar.bz2 | |
Merge branch 'pad-colons'
| -rw-r--r-- | TODO | 2 | ||||
| -rw-r--r-- | slack_profile.pl | 76 |
2 files changed, 61 insertions, 17 deletions
@@ -17,7 +17,7 @@ v Get the requested user's GitHub URL and Gigster Karma fields (needs users.prof v3: v Add a command to set/change the current user's profile fields v Profile field completion (see https://github.com/irssi/scripts.irssi.org/blob/22060fc4669627a9859c92efa2d6651e3673d494/scripts/go.pl) -- Pad colons +v Pad colons v4: - Add sub header comments 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'); } |
