(关闭)C# 控制台自动完成/建议输入,代码改进
(Closed) C# Console autocomplete / suggest input, Code Improvement
我目前正在编写 C# 控制台应用程序。部分原因是,用户需要输入一个相当复杂的系统名称。为了方便起见,我编写了一个使用 string[] 关键字的函数,并自动完成用户输入的字符串 - 在 运行.
代码正在运行并按预期运行,但我很好奇如何改进代码(例如可用性、效率)。另外,缺少哪些您期望的功能?
感谢您的反馈!
if (Keywords.Length == 0)
throw new Exception("No Keywords set!");
bool searching = true; // true while looking for the keyword
Console.CursorVisible = true; // To help users understand where they are typing
string System = ""; // Initialization of output
string suggestion = Keywords[0]; // Initialization of default suggestion
int toClear = suggestion.Length; // Get the length of the line that needs to be cleared
while (searching)
{
Console.Write(new String(' ', toClear)); // Clear line
Console.CursorLeft = 0; // Relocate cursor to line start
Console.Write(System); // Write what has been written previously
if(suggestion != "") // If there is a suggestion fitting the entered string,
{ // complete the string in color and reset the cursor
int col = Console.CursorLeft;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(suggestion.Substring(System.Length));
Console.ForegroundColor = ConsoleColor.White;
Console.CursorLeft = col;
}
string tempInput = Console.ReadKey().KeyChar.ToString();
if (tempInput.Equals("\r")) // Evaluate input:
{ // -> Enter
if (!suggestion.Equals("")) // Valid entry?
{
searching = false;
System = suggestion; // -> use suggestion
}
}
else if (tempInput.Equals("\b")) // Deleting last sign
{
if(System.Length>0)
System = System.Substring(0, System.Length - 1);
}
else // Any other sign is added to the string
System += tempInput;
// Set length to clear, if suggestion == "", the system string length needs to be cleared
toClear = (System.Length>suggestion.Length) ? System.Length : suggestion.Length;
// Reset suggestion. If no match is found, suggestion remains empty
suggestion = "";
// Check keywords for suggestion
for(int i= 0; i<Keywords.Length; i++)
{
if (Keywords[i].StartsWith(System))
{
suggestion = Keywords[i];
break;
}
}
}
// reset cursor visibility
Console.CursorVisible = false;
return System;
Do not raise reserved exception types
if (Keywords.Length == 0)
throw new Exception("No Keywords set!");
改为使用
if (Keywords.Length == 0)
throw new ArgumentException(nameof(Keywords), "No Keywords set!");
我不会使用 System
作为变量名。 System
是一个很常见的命名空间。
不确定是不是格式化的原因,但赋值之间的制表符不一致,使代码更难阅读
你计算的长度要清零,但从来没有用过
//设置长度清零,如果suggested == "",系统字符串长度需要清零
toClear = (System.Length>suggestion.Length) ? System.Length : suggestion.Length;
我目前正在编写 C# 控制台应用程序。部分原因是,用户需要输入一个相当复杂的系统名称。为了方便起见,我编写了一个使用 string[] 关键字的函数,并自动完成用户输入的字符串 - 在 运行.
代码正在运行并按预期运行,但我很好奇如何改进代码(例如可用性、效率)。另外,缺少哪些您期望的功能?
感谢您的反馈!
if (Keywords.Length == 0)
throw new Exception("No Keywords set!");
bool searching = true; // true while looking for the keyword
Console.CursorVisible = true; // To help users understand where they are typing
string System = ""; // Initialization of output
string suggestion = Keywords[0]; // Initialization of default suggestion
int toClear = suggestion.Length; // Get the length of the line that needs to be cleared
while (searching)
{
Console.Write(new String(' ', toClear)); // Clear line
Console.CursorLeft = 0; // Relocate cursor to line start
Console.Write(System); // Write what has been written previously
if(suggestion != "") // If there is a suggestion fitting the entered string,
{ // complete the string in color and reset the cursor
int col = Console.CursorLeft;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(suggestion.Substring(System.Length));
Console.ForegroundColor = ConsoleColor.White;
Console.CursorLeft = col;
}
string tempInput = Console.ReadKey().KeyChar.ToString();
if (tempInput.Equals("\r")) // Evaluate input:
{ // -> Enter
if (!suggestion.Equals("")) // Valid entry?
{
searching = false;
System = suggestion; // -> use suggestion
}
}
else if (tempInput.Equals("\b")) // Deleting last sign
{
if(System.Length>0)
System = System.Substring(0, System.Length - 1);
}
else // Any other sign is added to the string
System += tempInput;
// Set length to clear, if suggestion == "", the system string length needs to be cleared
toClear = (System.Length>suggestion.Length) ? System.Length : suggestion.Length;
// Reset suggestion. If no match is found, suggestion remains empty
suggestion = "";
// Check keywords for suggestion
for(int i= 0; i<Keywords.Length; i++)
{
if (Keywords[i].StartsWith(System))
{
suggestion = Keywords[i];
break;
}
}
}
// reset cursor visibility
Console.CursorVisible = false;
return System;
Do not raise reserved exception types
if (Keywords.Length == 0) throw new Exception("No Keywords set!");
改为使用
if (Keywords.Length == 0) throw new ArgumentException(nameof(Keywords), "No Keywords set!");
我不会使用
System
作为变量名。System
是一个很常见的命名空间。不确定是不是格式化的原因,但赋值之间的制表符不一致,使代码更难阅读
你计算的长度要清零,但从来没有用过//设置长度清零,如果suggested == "",系统字符串长度需要清零 toClear = (System.Length>suggestion.Length) ? System.Length : suggestion.Length;