# 如何對命令行進行彩色和樣式輸出
通過在命令行的 `output` 中使用顏色,你可以區分不同類型的輸出(比如,重要的信息,標題,注釋,等等)。
> 默認時,Windows 的 command console 不支持彩色輸出。 Console 對 Windows 系統禁用了顏色輸出,但如果你的命令調用了發射顏色序列的其他腳本,它們會被錯誤地顯示為轉義字符。安裝 [Cmder](http://cmder.net/), [ConEmu](https://conemu.github.io/), [ANSICON](https://github.com/adoxa/ansicon/releases),[Mintty](https://mintty.github.io/) ,[GitBash](https://git-scm.com/) 或者 [Cygwin](http://www.cygwin.com/) 等免費程序以便為你的 windows 命令行添加顏色支持。
## 使用顏色樣式
只要你輸出文本,就可以對文字加上標簽以實現彩色輸出。例如:
```
// 紅色背景上的白字
$output->writeln('<error>white text on a red background</error>');
// 綠字
$output->writeln('<info>green text</info>');
// 黃字
$output->writeln('<comment>yellow text</comment>');
// 黃色色背景上的黑字
$output->writeln('<warning>black text on a yellow background</warning>');
// 青色背景上的黑字
$output->writeln('<question>black text on a cyan background</question>');
// 紅背景上的白字
$output->writeln('<error>white text on a red background</error>');
// 支持混合輸出
$output->writeln('<info>green text</info><question>black text on a cyan background</question>......');
```
同時 ThinkPHP5 還支持快捷彩色輸出
目前支持:
`info`、 `error`、 `comment`、 `question`、 `highlight`、 `warning`。
即:
```
$output->info(green text);
```
等價于:
```
$output->writeln(<info>green text</info>)
```
標簽閉合時可以用 `</>` 來替代,它會撤消所有由“最后一個未關閉的標簽”所建立的格式化選項。
使用 `\think\console\output\formatter\Style` 類,也可以建立你自己的樣式:
```
use \think\console\output\formatter\Style;
// ...
$style = new Style('red', 'yellow', array('bold', 'blink'));
$output->getFormatter()->setStyle('fire', $style);
$output->writeln('<fire>fire</fire>');
```
默認可用的前景和背景顏色是: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan` 以及 `white`.
另有可用的選項是: `bold`, `underscore`, `blink`, `reverse` (可開啟 "`reverse video`" 模式,即將前景和背景顏色互換) 以及 `conceal` (設置前景的顏色為透明,可隱藏上屏的文字卻仍可以選擇和復制; 此選項在要求用戶鍵入敏感信息時常會用到)。
```
// 綠字
$output->writeln('<fg=green>green text</>');
// 青背景上的黑字
$output->writeln('<fg=black;bg=cyan>black text on a cyan background</>');
// 黃背景上的粗體字
$output->writeln('<bg=yellow;options=bold>bold text on a yellow background</>');
```