Command line 如何运行&x27;颜色';南音命令
我刚刚开始在构建和测试中使用Nant。我想让它在命令提示文本失败时更改其颜色(或背景),以便它容易被注意到 Windows上的命令提示符中的命令为“颜色4”,将其更改为红色,将其更改为白色,将其更改为颜色7 我如何在构建脚本中运行它,echo不工作,exec不工作(但可能使用exec错误)。我不希望为了在标准命令提示符窗口中轻松完成的事情而运行perl等Command line 如何运行&x27;颜色';南音命令,command-line,colors,nant,Command Line,Colors,Nant,我刚刚开始在构建和测试中使用Nant。我想让它在命令提示文本失败时更改其颜色(或背景),以便它容易被注意到 Windows上的命令提示符中的命令为“颜色4”,将其更改为红色,将其更改为白色,将其更改为颜色7 我如何在构建脚本中运行它,echo不工作,exec不工作(但可能使用exec错误)。我不希望为了在标准命令提示符窗口中轻松完成的事情而运行perl等 有人知道怎么做吗 尝试使用自定义任务。如果任务包含在nant文件中,则不会有任何外部依赖关系 <project > &l
有人知道怎么做吗 尝试使用自定义任务。如果任务包含在nant文件中,则不会有任何外部依赖关系
<project >
<target name="color">
<consolecolor color="Red" backgroundcolor="White"/>
<echo message="red text"/>
<consolecolor color="White" backgroundcolor="Black"/>
<echo message="white text"/>
</target>
<script language="C#">
<code>
[TaskName("consolecolor")]
public class TestTask : Task
{
private string _color;
private string _backgroundColor;
[TaskAttribute("color",Required=true)]
public string Color
{
get { return _color; }
set { _color = value; }
}
[TaskAttribute("backgroundcolor",Required=false)]
public string BackgroundColor
{
get { return _backgroundColor; }
set { _backgroundColor = value; }
}
protected override void ExecuteTask()
{
System.Console.ForegroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color);
System.Console.BackgroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor);
}
}
</code>
</script>
</project>
作为我对@Martin Vobr发表的帖子的后续评论: 我添加了额外的逻辑来正确地更改背景。这将允许在命令窗口中启动构建,然后可以一目了然地检查进度。我用蓝色背景表示“建筑”,绿色表示“成功”,红色表示“失败”
Console.CursorTop){
//consoleBufferLength=Console.CursorTop;
//}
对于(int y=0;y
设置背景颜色后,您可以添加try{System.Console.Clear();}catch(){}.1。这将清除屏幕并将整个背景设置为相同的颜色。您可以一眼看出构建是否失败。2.在重定向批处理文件时,将处理catch。
<!-- http://stackoverflow.com/questions/3446135/how-to-run-color-command-in-nant-script -->
<!-- Sample: <consolecolor color="Red" backgroundcolor="White"/> -->
<!-- Alternative: http://riccardotramma.com/2011/05/nantcolours-v1-0-a-task-library-for-output-colouring-in-nant/ -->
<script language="C#">
<code>
<![CDATA[
[TaskName("consolecolor")]
public class TestTask : Task
{
private string _color;
private string _backgroundColor;
[TaskAttribute("color",Required=true)]
public string Color
{
get { return _color; }
set { _color = value; }
}
[TaskAttribute("backgroundcolor",Required=false)]
public string BackgroundColor
{
get { return _backgroundColor; }
set { _backgroundColor = value; }
}
protected override void ExecuteTask()
{
System.Console.ForegroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color);
System.Console.BackgroundColor = (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor);
// clearing the screen sets the entire screen to be the new color
ChangeColor((System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),Color), (System.ConsoleColor) Enum.Parse(typeof(System.ConsoleColor),BackgroundColor));
}
// added by Brad Bruce
// http://stackoverflow.com/questions/6460932/change-entire-console-background-color-win32-c
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool ReadConsoleOutputAttribute(IntPtr hConsoleOutput,
[System.Runtime.InteropServices.Out] ushort[] lpAttribute, uint nLength, COORD dwReadCoord,
out uint lpNumberOfAttrsRead);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput,
ushort wAttribute, uint nLength, COORD dwWriteCoord, out uint
lpNumberOfAttrsWritten);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct COORD {
public short X;
public short Y;
public COORD(short X, short Y) {
this.X = X;
this.Y = Y;
}
};
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
//C#: Get stdout handle
const int STD_OUTPUT_HANDLE = -11;
const int STD_INPUT_HANDLE = -10;
const int STD_ERROR_HANDLE = -12;
//INVALID_HANDLE_VALUE //(return value if invalid handle is specified)
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes
public enum CharacterAttributes{
FOREGROUND_BLUE = 0x0001,
FOREGROUND_GREEN = 0x0002,
FOREGROUND_RED = 0x0004,
FOREGROUND_INTENSITY = 0x0008,
BACKGROUND_BLUE = 0x0010,
BACKGROUND_GREEN = 0x0020,
BACKGROUND_RED = 0x0040,
BACKGROUND_INTENSITY = 0x0080,
COMMON_LVB_LEADING_BYTE = 0x0100,
COMMON_LVB_TRAILING_BYTE = 0x0200,
COMMON_LVB_GRID_HORIZONTAL = 0x0400,
COMMON_LVB_GRID_LVERTICAL = 0x0800,
COMMON_LVB_GRID_RVERTICAL = 0x1000,
COMMON_LVB_REVERSE_VIDEO = 0x4000,
COMMON_LVB_UNDERSCORE = 0x8000
}
static void ChangeColor(System.ConsoleColor color, System.ConsoleColor backgroundColor) {
uint written = 0;
COORD writeCoord = new COORD(0, 0);
ushort[] attribute = new ushort[400];
IntPtr consoleOutputHandle = GetStdHandle( STD_OUTPUT_HANDLE );
int consoleBufferWidth = Console.BufferWidth;
int consoleBufferLength = Console.BufferHeight;
//if (consoleBufferLength > Console.CursorTop) {
// consoleBufferLength = Console.CursorTop;
//}
for (int y = 0; y < consoleBufferLength; y++) // rows
{
writeCoord.X = (short)0;
writeCoord.Y = (short)y;
ReadConsoleOutputAttribute(consoleOutputHandle, attribute, (uint)consoleBufferWidth, writeCoord, out written);
for (int x2 = 0; x2 < consoleBufferWidth; x2++){ // columns
attribute[x2] &= 0xFF00; // zero the background and foreground color
attribute[x2] |= (ushort)((((int)backgroundColor) << 4) | (int)color);
}
FillConsoleOutputAttribute(consoleOutputHandle, attribute[0], (uint)consoleBufferWidth, writeCoord, out written);
}
}
}
]]>
</code>
</script>