Преглед изворни кода

Add header to cli (#2296)

* Add header to cli

Now there is a switch to display the header on user info.
While doing that, I've changed how the command is working to display
all users by default and to accept more than one user at once.
I also changed the display to make it more pleasing.

As this command displays all users by default. I wonder if we still
need the list user command.

See #2294

* Minor format
Alexis Degrugillier пре 7 година
родитељ
комит
f2925594c7
2 измењених фајлова са 49 додато и 30 уклоњено
  1. 48 29
      cli/user-info.php
  2. 1 1
      lib/lib_rss.php

+ 48 - 29
cli/user-info.php

@@ -2,19 +2,46 @@
 <?php
 require(__DIR__ . '/_cli.php');
 
-$options = getopt('h', array(
-		'user:',
-	));
+const DATA_FORMAT = "%-7s | %-20s | %-25s | %-15s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s\n";
+
+$params = array(
+	'user:',
+	'header',
+);
+$options = getopt('h', $params);
+
+if (!validateOptions($argv, $params)) {
+	fail('Usage: ' . basename(__FILE__) . ' (-h --header --user username --user username …)');
+}
 
 if (empty($options['user'])) {
-	fail('Usage: ' . basename(__FILE__) . " -h --user username");
+	$users = listUsers();
+} elseif (is_array($options['user'])) {
+	$users = $options['user'];
+} else {
+	$users = array($options['user']);
 }
 
-$users = $options['user'] === '*' ? listUsers() : array($options['user']);
+sort($users);
+
+if (array_key_exists('header', $options)) {
+	printf(
+		DATA_FORMAT,
+		'default',
+		'user',
+		'last update',
+		'space used',
+		'categories',
+		'feeds',
+		'reads',
+		'unreads',
+		'favourites',
+		'tags'
+	);
+}
 
 foreach ($users as $username) {
 	$username = cliInitUser($username);
-	echo $username === FreshRSS_Context::$system_conf->default_user ? '*' : ' ', "\t";
 
 	$catDAO = FreshRSS_Factory::createCategoryDao();
 	$feedDAO = FreshRSS_Factory::createFeedDao($username);
@@ -25,31 +52,23 @@ foreach ($users as $username) {
 	$nbEntries = $entryDAO->countUnreadRead();
 	$nbFavorites = $entryDAO->countUnreadReadFavorites();
 
+	$data = array(
+		'default' => $username === FreshRSS_Context::$system_conf->default_user ? '*' : '',
+		'user' => $username,
+		'lastUpdate' => FreshRSS_UserDAO::mtime($username),
+		'spaceUsed' => $databaseDAO->size(),
+		'categories' => $catDAO->count(),
+		'feeds' => count($feedDAO->listFeedsIds()),
+		'reads' => $nbEntries['read'],
+		'unreads' => $nbEntries['unread'],
+		'favourites' => $nbFavorites['all'],
+		'tags' => $tagDAO->count(),
+	);
 	if (isset($options['h'])) {	//Human format
-		echo
-			$username, "\t",
-			date('c', FreshRSS_UserDAO::mtime($username)), "\t",
-			format_bytes($databaseDAO->size()), "\t",
-			$catDAO->count(), " categories\t",
-			count($feedDAO->listFeedsIds()), " feeds\t",
-			$nbEntries['read'], " reads\t",
-			$nbEntries['unread'], " unreads\t",
-			$nbFavorites['all'], " favourites\t",
-			$tagDAO->count(), " tags\t",
-			"\n";
-	} else {
-		echo
-			$username, "\t",
-			FreshRSS_UserDAO::mtime($username), "\t",
-			$databaseDAO->size(), "\t",
-			$catDAO->count(), "\t",
-			count($feedDAO->listFeedsIds()), "\t",
-			$nbEntries['read'], "\t",
-			$nbEntries['unread'], "\t",
-			$nbFavorites['all'], "\t",
-			$tagDAO->count(), "\t",
-			"\n";
+		$data['lastUpdate'] = date('c', $data['lastUpdate']);
+		$data['spaceUsed'] = format_bytes($data['spaceUsed']);
 	}
+	vprintf(DATA_FORMAT, $data);
 }
 
 done();

+ 1 - 1
lib/lib_rss.php

@@ -170,7 +170,7 @@ function format_bytes($bytes, $precision = 2, $system = 'IEC') {
 	$pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
 	$pow = min($pow, count($units) - 1);
 	$bytes /= pow($base, $pow);
-	return format_number($bytes, $precision) . ' ' . $units[$pow];
+	return format_number($bytes, $precision) . ' ' . $units[$pow];
 }
 
 function timestamptodate ($t, $hour = true) {