Skip to content

Commit

Permalink
Propagate decoration state to repo warnings, fixes #10601
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 9, 2022
1 parent 0f688b1 commit 0a4c077
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Composer/Util/HttpDownloader.php
Expand Up @@ -454,6 +454,14 @@ private function getResponse($index)
*/
public static function outputWarnings(IOInterface $io, $url, $data)
{
$cleanMessage = function ($msg) use ($io) {
if (!$io->isDecorated()) {
$msg = Preg::replace('{'.chr(27).'\\[[;\d]*m}u', '', $msg);

This comment has been minimized.

Copy link
@ktomk

ktomk Mar 14, 2022

Contributor

just writing some initial notes here while looking at the code.

we may spare a function call by replacing chr(27) with "\033" and the string concatenation by placing it directly into the pattern string as \033, and dropping a slash when not needed:

'{'.chr(27).'\\[[;\d]*m}u'
'{\033\[[;\d]*m}u'

this covers only ESC [ m, not really profound with this, but it seems there is more, printf \\033c comes to mind.

the u UTF-8 modifier: I don't know if looks like it sets PCRE_UCP option, if so:

   \d  any character that matches \p{Nd} (decimal digit)

which then would be an issue. tend to consider binary mode (or unset PCRE_UCP in the pattern). binary mode might be more optimized thought.

}

return $msg;
};

// legacy warning/info keys
foreach (array('warning', 'info') as $type) {
if (empty($data[$type])) {
Expand All @@ -469,7 +477,7 @@ public static function outputWarnings(IOInterface $io, $url, $data)
}
}

$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].'</'.$type.'>');
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$cleanMessage($data[$type]).'</'.$type.'>');
}

// modern Composer 2.2+ format with support for multiple warning/info messages
Expand All @@ -487,7 +495,7 @@ public static function outputWarnings(IOInterface $io, $url, $data)
continue;
}

$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$spec['message'].'</'.$type.'>');
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$cleanMessage($spec['message']).'</'.$type.'>');
}
}
}
Expand Down

0 comments on commit 0a4c077

Please sign in to comment.