From 746fe2af51e64c24dc74afc91a2291ffdaf0a4f5 Mon Sep 17 00:00:00 2001 From: Guo Yunhe Date: Wed, 8 May 2019 23:54:09 +0300 Subject: [PATCH] Better print column alignment --- CHANGELOG.md | 4 ++ opi | 151 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 104 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d76b62e..0c8a243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support SLE [#8](https://github.com/openSUSE-zh/opi/issues/8) +### Changed + +- Better print column alignment + ## [0.2.0] ### Added diff --git a/opi b/opi index 67513d7..ed76f2d 100755 --- a/opi +++ b/opi @@ -73,17 +73,12 @@ if (!scalar(@bins)) { # # Print package name options # -my $i = 1; -foreach my $n (@binary_names) { - print "$i. $n\n"; - $i++; -} +print_package_names(@binary_names); # # Select a package name option # -print "Choose a number: "; my $selected_name_number = type_a_number(1, scalar(@binary_names)); my $selected_name = $binary_names[$selected_name_number - 1]; @@ -95,18 +90,12 @@ my @binary_options = get_binary_by_name($selected_name, @bins); # # Print binary package options # -$i = 1; -foreach my $b (@binary_options) { - print_binary_option($b, $i); - print "\n"; - $i++; -} +print_binary_options(@binary_options); # # Select a binary package option # -print "Choose a number: "; my $selected_binary_number = type_a_number(1, scalar(@binary_options)); my $selected_binary = $binary_options[$selected_binary_number - 1]; @@ -121,6 +110,13 @@ print "\n"; # install_binary($selected_binary); + +=begin functions + +Get system information + +=cut + sub get_distribution { my $config = Config::Tiny->read('/etc/os-release'); my $name = $config->{_}->{NAME}; @@ -150,9 +146,18 @@ sub get_architecture { return $&; } + +=begin functions + +Receive user inputs + +=cut + sub type_a_number { - my $min = $_[0]; - my $max = $_[1]; + my $min = shift; + my $max = shift; + my $message = shift; + print $message || "Choose a number: "; my $typed_number = ; chomp $typed_number; if ($typed_number =~ /^(\d+)$/) { @@ -162,34 +167,89 @@ sub type_a_number { } } - print "Number must be between $min and $max. Please try again: "; - return type_a_number(@_); + return type_a_number($min, $max, "Number must be between $min and $max. Please try again: "); +} + +sub ask_yes_or_no { + print $_[0]; + + if (lc($_[1]) eq 'y') { + print '(Y/n) '; + } else { + print '(y/N) '; + } + + my $yes_no = ; + chomp $yes_no; + $yes_no = lc(substr($yes_no, 0, 1)); + + if (lc($_[1]) eq 'y') { + return $yes_no ne 'n'; + } else { + return $yes_no eq 'y'; + } +} + +sub ask_keep_repo { + my $repo = $_[0]; + unless ( ask_yes_or_no("Do you want to keep these repositories? ", 'y') ) { + system "sudo zypper removerepo $repo"; + } +} + +=begin functions + +Print package lists + +=cut + +sub print_package_names { + my $i = 1; + foreach my $n (@_) { + printf("%2d. %s\n", $i, $n); + $i++; + } +} + +sub print_binary_options { + my $i = 1; + foreach my $b (@_) { + print_binary_option($b, $i); + $i++; + } } sub print_binary_option { - my $binary = $_[0]; - my $number = $_[1]; + my $binary = shift; + my $number = shift; my $color; my $symbol; if (is_official_project($binary->{project})) { $color = 'green'; - $symbol = '✓'; + $symbol = '+'; } elsif (is_personal_project($binary->{project})) { $color = 'red'; - $symbol = '?'; + $symbol = '!'; } else { $color = 'cyan'; - $symbol = '+'; + $symbol = '?'; } + my $colored_name = colored(substr($binary->{project}, 0, 39) . ' ' . $symbol, $color); + if ($number) { - print "$number. "; + printf("%2d. %-50s | %-10s | %-10s\n", $number, $colored_name, $binary->{version}, $binary->{arch}); + } else { + print $colored_name, " | ", $binary->{version}, " | ", $binary->{arch}; } - - print colored($binary->{project} . ' ' . $symbol, $color); - print " | $binary->{version} | $binary->{arch}"; } +=begin functions + +Search OBS API + +=cut + sub prepare_query_string { my $query_string = join "', '", @_; $query_string = "'" . $query_string . "'"; @@ -237,6 +297,11 @@ sub search_published_binary { next; } + # Filter out branch projects + if ( $binary->{project} =~ /:branches:/m ) { + next; + } + # Filter out debuginfo, debugsource, buildsymbols packages if ( substr($binary->{'name'}, -10) eq '-debuginfo' ) { next; @@ -273,6 +338,12 @@ sub search_published_binary { } } +=begin functions + +Handle binary data + +=cut + sub get_binary_names { my @names = (); foreach my $bin (@_) { @@ -341,33 +412,11 @@ sub get_binary_by_name { return @filtered_binary_list; } +=begin functions -sub ask_yes_or_no { - print $_[0]; - - if (lc($_[1]) eq 'y') { - print '(Y/n) '; - } else { - print '(y/N) '; - } - - my $yes_no = ; - chomp $yes_no; - $yes_no = lc(substr($yes_no, 0, 1)); - - if (lc($_[1]) eq 'y') { - return $yes_no ne 'n'; - } else { - return $yes_no eq 'y'; - } -} +Add repository and install packages -sub ask_keep_repo { - my $repo = $_[0]; - unless ( ask_yes_or_no("Do you want to keep these repositories? ", 'y') ) { - system "sudo zypper removerepo $repo"; - } -} +=cut sub install_binary { my $binary = $_[0];