Skip to content

Commit

Permalink
修复控制台终端在VSCode调试中的错误,并简化了下代码。 🍉
Browse files Browse the repository at this point in the history
  • Loading branch information
PopeyeZhong committed Dec 3, 2023
1 parent 44e52e9 commit 5a85569
Showing 1 changed file with 24 additions and 71 deletions.
95 changes: 24 additions & 71 deletions Zongsoft.Core/src/Terminals/ConsoleTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,79 +57,48 @@ private ConsoleTerminal()
{
_syncRoot = new object();

Console.TreatControlCAsInput = false;
Console.CancelKeyPress += this.Console_CancelKeyPress;
if(!System.Diagnostics.Debugger.IsAttached)
{
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += this.Console_CancelKeyPress;
}
}
#endregion

#region 公共属性
public TextReader Input
{
get
{
return Console.In;
}
set
{
Console.SetIn(value);
}
get => Console.In;
set => Console.SetIn(value);
}

public TextWriter Output
{
get
{
return Console.Out;
}
set
{
Console.SetOut(value);
}
get => Console.Out;
set => Console.SetOut(value);
}

public TextWriter Error
{
get
{
return Console.Error;
}
set
{
Console.SetError(value);
}
get => Console.Error;
set => Console.SetError(value);
}

public CommandOutletColor BackgroundColor
{
get
{
return this.ConvertColor(Console.BackgroundColor, CommandOutletColor.Black);
}
set
{
Console.BackgroundColor = ConvertColor(value, ConsoleColor.Black);
}
get => ConvertColor(Console.BackgroundColor, CommandOutletColor.Black);
set => Console.BackgroundColor = ConvertColor(value, ConsoleColor.Black);
}

public CommandOutletColor ForegroundColor
{
get
{
return this.ConvertColor(Console.ForegroundColor, CommandOutletColor.White);
}
set
{
Console.ForegroundColor = ConvertColor(value, ConsoleColor.White);
}
get => ConvertColor(Console.ForegroundColor, CommandOutletColor.White);
set => Console.ForegroundColor = ConvertColor(value, ConsoleColor.White);
}
#endregion

#region 公共方法
public void Clear()
{
Console.Clear();
}

public void Clear() => Console.Clear();
public void Reset()
{
//激发“Resetting”事件
Expand Down Expand Up @@ -315,23 +284,11 @@ public void WriteLine(CommandOutletColor foregroundColor, string format, params
#region 显式实现
Encoding ICommandOutlet.Encoding
{
get
{
return Console.OutputEncoding;
}
set
{
Console.OutputEncoding = value;
}
get => Console.OutputEncoding;
set => Console.OutputEncoding = value;
}

TextWriter ICommandOutlet.Writer
{
get
{
return Console.Out;
}
}
TextWriter ICommandOutlet.Writer => Console.Out;
#endregion

#region 激发事件
Expand Down Expand Up @@ -368,21 +325,17 @@ private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
#endregion

#region 私有方法
private CommandOutletColor ConvertColor(ConsoleColor color, CommandOutletColor defaultColor)
private static CommandOutletColor ConvertColor(ConsoleColor color, CommandOutletColor defaultColor)
{
CommandOutletColor result;

if(Enum.TryParse<CommandOutletColor>(color.ToString(), out result))
if(Enum.TryParse<CommandOutletColor>(color.ToString(), out var result))
return result;
else
return defaultColor;
}

private ConsoleColor ConvertColor(CommandOutletColor color, ConsoleColor defaultColor)
private static ConsoleColor ConvertColor(CommandOutletColor color, ConsoleColor defaultColor)
{
ConsoleColor result;

if(Enum.TryParse<ConsoleColor>(color.ToString(), out result))
if(Enum.TryParse<ConsoleColor>(color.ToString(), out var result))
return result;
else
return defaultColor;
Expand All @@ -405,7 +358,7 @@ private void WriteContent(CommandOutletContent content, bool appendLine, Command
while(content != null)
{
//设置片段内容指定的颜色值
this.ForegroundColor = content.Color.HasValue ? content.Color.Value : foregroundColor.Value;
this.ForegroundColor = content.Color ?? foregroundColor.Value;

//输出指定的片段内容文本
Console.Write(content.Text);
Expand Down

0 comments on commit 5a85569

Please sign in to comment.