@@ -50,13 +50,25 @@ class CLI {
5050 * @param Plugin $plugin The plugin instance.
5151 */
5252 public function __construct ( $ plugin ) {
53- \WP_CLI ::log ( '' );
54- \WP_CLI ::log ( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦ ' );
55- \WP_CLI ::log ( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║ ' );
56- \WP_CLI ::log ( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩ ' );
5753 $ this ->plugin = $ plugin ;
5854 }
5955
56+ /**
57+ * Output the Intro.
58+ *
59+ * @since 2.5.1
60+ */
61+ public function do_intro () {
62+ static $ intro ;
63+ if ( ! $ intro ) {
64+ \WP_CLI ::log ( '' );
65+ \WP_CLI ::log ( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦ ' );
66+ \WP_CLI ::log ( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║ ' );
67+ \WP_CLI ::log ( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩ ' );
68+ $ intro = true ;
69+ }
70+ }
71+
6072 /**
6173 * Syncs assets with Cloudinary.
6274 * ## EXAMPLES
@@ -113,7 +125,7 @@ public function analyze() {
113125
114126 // Initial query.
115127 $ query_args = $ this ->base_query_args ;
116- $ query = new \WP_Query ( $ query_args );
128+ $ query = new \WP_Query ( $ query_args );
117129
118130 // Kill all _cld_ related meta.
119131 delete_post_meta_by_key ( '_cld_unsynced ' );
@@ -132,6 +144,8 @@ public function analyze() {
132144 * @param string $process The process to do.
133145 */
134146 protected function do_process ( &$ query , $ process ) {
147+ $ this ->do_intro ();
148+
135149 // Bail early.
136150 if ( ! method_exists ( $ this , "process_ {$ process }" ) ) {
137151 \WP_CLI ::log ( \WP_CLI ::colorize ( "%Invalid Process: {$ process }.%n " ) );
@@ -175,8 +189,9 @@ protected function process_sync( $posts, $total ) {
175189 }
176190 foreach ( $ posts as $ index => $ asset ) {
177191 $ done ++; // Set $done early to not show 0 of x.
178- $ file = get_attached_file ( $ asset );
179- $ bar ->tick ( 0 , 'Syncing: ' . basename ( $ file ) . ' ( ' . ( $ done ) . ' of ' . $ total . ') ' );
192+ $ file = get_attached_file ( $ asset );
193+ $ filename = self ::pad_name ( basename ( $ file ), 20 , ' ' , '* ' );
194+ $ bar ->tick ( 0 , 'Syncing ( ' . ( $ done ) . ' of ' . $ total . ') : ' . $ filename );
180195 if ( ! $ this ->plugin ->get_component ( 'sync ' )->is_synced ( $ asset ) ) {
181196 $ this ->plugin ->get_component ( 'sync ' )->managers ['push ' ]->process_assets ( $ asset , $ bar );
182197 }
@@ -241,4 +256,34 @@ protected function process_analyze( $posts, $total ) {
241256 update_option ( '_cld_cli_analyzed ' , true , false );
242257 }
243258 }
259+
260+ /**
261+ * Pad a file name to fit within max chars.
262+ *
263+ * @param string $name The name to pad.
264+ * @param int $max_length The max length of the filename.
265+ * @param string $pad_char The pad char to use when name is less of the max.
266+ * @param string $concat_char The char to use when shortening names to fit.
267+ *
268+ * @return string
269+ */
270+ protected static function pad_name ( $ name , $ max_length , $ pad_char = '. ' , $ concat_char = '* ' ) {
271+ $ name_length = strlen ( $ name );
272+ $ prefix = null ;
273+ if ( $ name_length > $ max_length ) {
274+ $ diff = $ name_length - $ max_length ;
275+ $ concat_length = $ diff > 3 ? 3 : $ diff ;
276+ $ usable_length = $ max_length - $ concat_length ;
277+ $ front = substr ( $ name , 0 , floor ( $ usable_length / 2 ) );
278+ $ back = substr ( $ name , strlen ( $ name ) - ceil ( $ usable_length / 2 ) );
279+ $ name = $ front . implode ( array_fill ( 0 , $ concat_length , $ concat_char ) ) . $ back ;
280+ }
281+ $ used_length = $ max_length - strlen ( $ name );
282+ if ( 0 < $ used_length ) {
283+ $ prefix = implode ( array_fill ( 0 , $ used_length , $ pad_char ) );
284+ }
285+ $ out = $ prefix . $ name ;
286+
287+ return $ out ;
288+ }
244289}
0 commit comments