Skip to content
Browse files

Refactor printing colorized package names to be more reusable.

Moves some functions to clydelib/ui.lua. Creates mk_pkg_colorizer()
and prompt_for_numbers(). sync_search now uses mk_pkg_colorizer to
create its own package printer.

This will be useful for using the yaourt-style number chooser for
the new source_provider transaction callback in pacman 3.5.
  • Loading branch information...
1 parent 1c21af3 commit bad20d8729b089e491eaa3afb225b0e572fa85cf @juster juster committed Mar 13, 2011
Showing with 146 additions and 109 deletions.
  1. +15 −50 clyde
  2. +38 −58 clydelib/sync.lua
  3. +93 −1 clydelib/ui.lua
View
65 clyde
@@ -14,6 +14,7 @@ local utilcore = require "clydelib.utilcore"
local signal = require "clydelib.signal"
local callback = require "clydelib.callback"
local aur = require "clydelib.aur"
+local ui = require "clydelib.ui"
local get_builduser = aur.get_builduser
local needs_root = util.needs_root
local printf = util.printf
@@ -31,7 +32,6 @@ local textdomain = utilcore.textdomain
local geteuid = utilcore.geteuid
local C = colorize
-
local ROOTDIR = "/"
local CONFFILE = "/etc/pacman.conf"
local CLYDECONF = "/etc/clyde.conf"
@@ -965,65 +965,26 @@ end
-- Offer to install packages in the package_names list
-- Returns the names of packages they want to install.
-function install_numprompt ( package_names )
+local function offer_install_pkgs ( package_names )
if not next( package_names ) then return {} end
local bars = C.yelb("==>")
printf("\n%s %s\n%s %s\n",
bars,
C.bright("Enter #'s (separated by blanks) of packages "
.. "to be installed"),
- bars, C.bright(("-"):rep(60)), bars )
-
- local function ask_package_nums ( maxnum )
- io.write( bars .. " " )
- local nums_choice = io.read()
+ bars, C.bright(string.rep( "-", 60 )), bars )
- if nums_choice == "" then return nil end
+ local max = #package_names
+ local chosen = ui.prompt_for_numbers( bars, max )
- input_numbers = {}
- for num in nums_choice:gmatch( "(%S+)" ) do
- if num:match( "%D" ) then
- error( "Invalid input", 0 )
- else
- num = tonumber( num )
- if ( num < 1 or num > maxnum ) then
- error( "Out of range", 0 )
- end
- table.insert( input_numbers, num )
- end
- end
-
- return input_numbers
+ if not chosen then
+ print( "Aborting." )
+ return {}
end
- local install_me = {}
- local max = table.maxn( package_names )
- while ( true ) do
- local success, answer
- = pcall( ask_package_nums, max )
-
- if ( success ) then
- if ( answer ) then
- for i, num in ipairs( answer ) do
- table.insert( install_me, package_names[num] )
- end
- else
- print( "Aborting." )
- end
- break
- elseif ( answer == "Invalid input" ) then
- print( "Please enter only digits and/or whitespace." )
- elseif ( answer == "Out of range" ) then
- print( "Package numbers must be between 1 and "
- .. max .. "." )
- else
- -- Rethrow an unknown error.
- error( answer, 0 )
- end
- end
-
- return install_me
+ for i, n in ipairs( chosen ) do chosen[i] = package_names[n] end
+ return chosen
end
function main()
@@ -1122,9 +1083,13 @@ function main()
local found_names = sync.sync_search( pm_targets, true )
if not next( found_names ) then return 1 end
- local install = install_numprompt( found_names )
+ local install = offer_install_pkgs( found_names )
if not next( install ) then return 1 end
+ for i, pkg in ipairs( install ) do
+ print( "*DBG* " .. pkg )
+ end
+
if (myuid > 0) then
-- See whether sudo (preferable) or su exist
local sucmds = { { file = "/usr/bin/sudo",
View
96 clydelib/sync.lua
@@ -11,7 +11,6 @@ local aur = require "clydelib.aur"
local upgrade = require "clydelib.upgrade"
local callback = require "clydelib.callback"
local ui = require "clydelib.ui"
-local colorize_verstr = ui.colorize_verstr
local rmrf = util.rmrf
local makepath = util.makepath
local eprintf = util.eprintf
@@ -239,9 +238,9 @@ end
-- SYNC SEARCH ---------------------------------------------------------------
--- These little helper functions are used by print_fancy_match. Their
--- results are gathered in a table, so if they don't want to be printed
--- they return nil.
+-- These little helper functions are used with mk_match_printer from
+-- ui.lua. Inside mk_match_printer, their results are gathered in a
+-- table, so if they don't want to be printed they return nil.
-- Helper function for installed_tag().
local function get_installed_version ( pkg_name )
@@ -254,15 +253,16 @@ end
-- Returns a tag (string) to print showing if a pkg is installed and
-- if a different version is installed. If the pkg is not installed,
-- returns nil.
-local function installed_tag(pkg_name, pkg_version)
+local function installed_tag ( pkg )
+ local pkg_name, pkg_version = pkg.name, pkg.version
local installed_version = get_installed_version( pkg_name )
if not installed_version then return nil end
-- If the found version is different from installed version,
-- print the installed version.
local tagtext = ""
if alpm.pkg_vercmp( installed_version, pkg_version ) ~= 0 then
- tagtext = colorize_verstr( installed_version, C.yel ) .. " "
+ tagtext = ui.colorize_verstr( installed_version, C.yel ) .. " "
end
tagtext = tagtext .. C.yel( "installed" )
@@ -272,7 +272,8 @@ local function installed_tag(pkg_name, pkg_version)
end
-- Returns the size of the download or nil if N/A.
-local function download_size_tag ( size )
+local function download_size_tag ( pkg )
+ local size = pkg.size
if size and config.showsize then
if size > (1024 * 1024) then
return string.format( "[%.2f MB]", size / (1024 * 1024))
@@ -287,65 +288,47 @@ local function download_size_tag ( size )
end
-- Returns the colorized group list, as a string to be printed
-local function groups_tag ( group_names )
+local function groups_tag ( pkg )
+ local group_names = pkg.groups
if not group_names or not next( group_names ) then return nil end
- return C.blub( "(" ) .. C.blu( table.concat( group_names, " " )) .. C.blub( ")" )
+ return C.blub( "(" )
+ .. C.blu( table.concat( group_names, " " ))
+ .. C.blub( ")" )
end
-- Obviously, this only works for AUR packages...
-local function votes_tag ( votes )
- if votes then
- return C.yelb( "(" ) .. C.yel( votes ) .. C.yelb( ")" )
+local function votes_tag ( pkg )
+ if pkg.votes then
+ return C.yelb( "(" ) .. C.yel( pkg.votes ) .. C.yelb( ")" )
else
return nil
end
end
-local print_counter = 1
+-- Returns a closure that prints a fancy colorized entry for a search match.
+local function mk_match_printer ( shownumbers )
+ -- We use the package printer provided in the clydelib.ui module
+ -- and we provide our own custom tag generators...
+ local print_counter = 1
+ local taggers = { installed_tag, download_size_tag, groups_tag,
+ votes_tag }
+ local pkg_colorizer = ui.mk_pkg_colorizer( taggers )
--- Prints a fancy colorized entry for a search match.
--- TODO: shownumbers is kind of funky, better design?
-local function print_fancy_match ( match, shownumbers )
- local dbcolors = {
- extra = C.greb;
- core = C.redb;
- community = C.magb;
- testing = C.yelb;
- aur = C.cyab;
- }
-
- local words = {}
+ local function match_printer ( match )
+ if shownumbers then
+ io.write( print_counter .. " " )
+ print_counter = print_counter + 1
+ end
- local function revyel( str )
- return C.yel .. C.onblab .. C.reverse .. str .. C.reset
- end
+ print( pkg_colorizer( match ))
- if shownumbers then
- io.write( print_counter .. " " )
- print_counter = print_counter + 1
+ -- pkg_printer only prints one line so we print the desc ourselves
+ io.write( " " )
+ indentprint( match.desc, 3 )
+ print()
end
-
- -- Our package name is changed to dbname/pkgname
- local dbcolor = dbcolors[ match.dbname ] or C.bright
- local longname = dbcolor .. match.dbname .. C.reset
- .. "/" .. C.whib(match.name)
- table.insert( words, longname )
-
- -- Package version is the next word
- table.insert( words, colorize_verstr( match.version, C.gre ))
-
- -- Any of the following words might be nil...
- table.insert( words, installed_tag( match.name, match.version ))
- table.insert( words, download_size_tag( tonumber( match.size )))
- table.insert( words, groups_tag( match.groups ))
- table.insert( words, votes_tag( match.votes ))
-
- print( table.concat( words, " " ))
- io.write( " " )
- indentprint( match.desc, 3 )
- print()
- return
+ return match_printer
end
-- Returns an array of packages which match the search queries.
@@ -440,12 +423,9 @@ function sync_search ( targets, shownumbers )
print( C.bright( match.name ))
end
- local function print_fancy ( match )
- print_fancy_match( match, shownumbers )
- end
-
- local match_printcb = config.quiet and print_dumb or print_fancy
- local found_names = {}
+ local match_printcb = config.quiet and print_dumb
+ or mk_match_printer( shownumbers )
+ local found_names = {}
-- First we search the ALPM repos...
if not config.op_s_search_aur_only then
View
94 clydelib/ui.lua
@@ -8,8 +8,56 @@ module( "clydelib.ui", package.seeall )
local C = colorize
+--- Creates a closure that convert pkginfo to a colorized line/string.
+-- This colorizes the repo name and package name as well as the
+-- version string.
+--
+-- @param col_funcs A table of functions who are passed the pkginfo
+-- table at runtime. These return a string they would
+-- like added to the package line or nil if they
+-- don't want to add anything.
+function mk_pkg_colorizer ( col_funcs )
+ local col_funcs = col_funcs
+ local function pkg_colorizer ( pkg )
+
+ -- Every colorizer made shows at least the package name and
+ -- version...
+
+ -- The package name printed is dbname/pkgname...
+ local words = {}
+ local dbname = colorize_dbname( pkg.dbname )
+ local longname = dbname .. "/" .. C.whib( pkg.name )
+ table.insert( words, longname )
+
+ -- Package version is the next word...
+ table.insert( words, colorize_verstr( pkg.version, C.gre ))
+
+ -- Now we iterate through any custom "word" generators...
+ for i, func in ipairs( col_funcs ) do
+ table.insert( words, func( pkg ))
+ end
+
+ return table.concat( words, " " )
+ end
+
+ return pkg_colorizer
+end
+
+function colorize_dbname ( dbname )
+ local dbcolors = {
+ extra = C.greb;
+ core = C.redb;
+ community = C.magb;
+ testing = C.yelb;
+ aur = C.cyab;
+ }
+
+ local dbcolor = dbcolors[ dbname ] or C.bright
+ return dbcolor( dbname )
+end
+
-- Enbolden the periods and hyphen in a version string.
-function colorize_verstr( verstr, color )
+function colorize_verstr ( verstr, color )
local verstr = color( verstr )
verstr = verstr:gsub( "-(%d+)\27%[0m$",
function ( pkgrel )
@@ -22,3 +70,47 @@ function colorize_verstr( verstr, color )
verstr = verstr:gsub( "%.", C.bright( "." ) .. color )
return verstr
end
+
+-- Prompts the user for numbers. Many choices can be separated by whitespace.
+-- The numbers chosen are returned as a list. If no numbers were
+-- chosen (i.e. only ENTER was typed) then nil is returned.
+function prompt_for_numbers ( prompt, max )
+ local function ask_nums ( maxnum )
+ io.write( prompt .. " " )
+ local nums_choice = io.read()
+
+ if nums_choice == "" then return nil end
+
+ input_numbers = {}
+ for num in nums_choice:gmatch( "(%S+)" ) do
+ if num:match( "%D" ) then
+ error( "Invalid input", 0 )
+ else
+ num = tonumber( num )
+ if ( num < 1 or num > maxnum ) then
+ error( "Out of range", 0 )
+ end
+ table.insert( input_numbers, num )
+ end
+ end
+
+ return input_numbers
+ end
+
+ local chosen_nums = {}
+ while ( true ) do
+ local success, answer
+ = pcall( ask_nums, max )
+
+ if ( success ) then
+ return answer -- answer may be nil
+ elseif ( answer == "Invalid input" ) then
+ print( "Please enter only digits and/or whitespace." )
+ elseif ( answer == "Out of range" ) then
+ print( "Input numbers must be between 1 and " .. max .. "." )
+ else
+ -- Rethrow an unknown error.
+ error( answer, 0 )
+ end
+ end
+end

0 comments on commit bad20d8

Please sign in to comment.
Something went wrong with that request. Please try again.