我有一个类似于这样的类的控制台应用程序:
Friend Class Documentation
Friend Shared MyColor1 As ConsoleColor =
ConsoleColor.Yellow
Private Shared ReadOnly ProcessName As String =
Process.GetCurrentProcess().MainModule.ModuleName
Friend Shared ReadOnly Help As XElement =
<Help>
<!-- Application Usage Examples -->
<UsageExamples>
[+] Usage examples:
<%= ProcessName %> /Switch1=Value "C:\File.txt"
{1}(Command explanation){1}
</UsageExamples>
</Help>
End Class
有一种方法可以在一个不同的consolecolor中显示(Command explanation)
字符串,我以前应该在var中动态设置吗?
所以我的想法是不打印这个:
http://img9.imageshack.us/img9/9795/90px.jpg
对上面的XML / Class进行必要的(和动态的)修改,以便在特定位置设置颜色以打印其他颜色:
如何在不沉浸于更难的方法的情况下管理这个问题,比如编写一个应该解析并打印字符串以在特定字符/关键字位置或其他位置着色的方法?
这可能是以更简单,自动化/动态的方式进行的吗?
有更好的想法吗?
更新
这是我的方法:
我编写了一个解析器方法来编写彩色文本,它需要一个带分隔符的字符串。
''' <summary>
''' Writes colored text on the Console.
''' </summary>
''' <param name="Text">Indicates the text to write.</param>
Friend Shared Sub WriteColoredText(ByVal Text As String)
' Store the current console colors to restore them later...
Dim CurrentForegroundColor As ConsoleColor = Console.ForegroundColor
Dim CurrentBackgroundColor As ConsoleColor = Console.BackgroundColor
Dim StringParts As String() = Text.Split({"{"c, "}"c})
Dim PrintFlag As Boolean = False
For Each str As String In StringParts
If str Like "f#" OrElse str Like "f##" Then ' Change the ForeColor.
PrintFlag = False
Console.ForegroundColor = CInt(str.Replace(str.First, String.Empty))
ElseIf str Like "b#" OrElse str Like "b##" Then ' Change the BackgroundColor.
PrintFlag = False
Console.BackgroundColor = CInt(str.Replace(str.First, String.Empty))
ElseIf str Like "-f" Then ' Restore the Forecolor.
PrintFlag = False
Console.ForegroundColor = CurrentForegroundColor
ElseIf str Like "-b" Then ' Restore the BackgroundColor.
PrintFlag = False
Console.BackgroundColor = CurrentBackgroundColor
Else ' Print the string as normal.
PrintFlag = True
End If
If PrintFlag Then ' If it is not a '{fxx}/{bxx}/{-f}/{-b}' string then print the text.
Console.Write(str)
End If
Next str
' Restore the original console colors.
Console.BackgroundColor = CurrentBackgroundColor
Console.ForegroundColor = CurrentForegroundColor
End Sub
然后使用带有这样的分隔符的XML:
<!-- Application Usage Examples -->
<UsageExamples>
{f11}[+]{-f} {f14}Usage examples{-f}
<%= ProcessName %> /Switch1=Value "C:\File.txt"
{f11}( Command explanation ){-f}
</UsageExamples>
我可以得到这样的输出,但是我想要做的是简化所有这些以避免像我的方法avobe 那样编写自定义解析器方法的需要,如果可能的话:
答案 0 :(得分:0)
嗯,(虽然有更好的想法)我按照我在提问时提到的方法做到了。
享受此片段!
' Name.......: HelpSection
' Author.....: Elektro
' Description: Class that manages the Help documentation of a Console application with colorization capabilities.
' Last Update: 05/01/2014
' References.: LINQ
' Indications: Use {FXX} as the start ConsoleColor ForeColor number, {-F} to restore the console forecolor.
' Use {BXX} as the start ConsoleColor BackColor number, {-B} to restore the console BackColor.
#Region " Usage Examples "
#Region " Example 1 "
'Module Module1
' Sub Main()
' Console.Title = HelpSection.Help.<Title>.Value
' Dim sb As New System.Text.StringBuilder
' sb.AppendLine(HelpSection.Help.<Logo>.Value)
' sb.AppendLine(HelpSection.Help.<Separator>.Value)
' sb.AppendLine(String.Format(" Executable name.......: {0}", HelpSection.Help.<Process>.Value))
' sb.AppendLine(String.Format(" Application name......: {0}", HelpSection.Help.<Name>.Value))
' sb.AppendLine(String.Format(" Application version...: {0}", HelpSection.Help.<Version>.Value))
' sb.AppendLine(String.Format(" Application author....: {0}", HelpSection.Help.<Author>.Value))
' sb.AppendLine(String.Format(" Application copyright.: {0}", HelpSection.Help.<Copyright>.Value))
' sb.AppendLine(String.Format(" Author website........: {0}", HelpSection.Help.<Website>.Value))
' sb.AppendLine(String.Format(" Author Skype..........: {0}", HelpSection.Help.<Skype>.Value))
' sb.AppendLine(String.Format(" Author Email..........: {0}", HelpSection.Help.<Email>.Value))
' sb.AppendLine(HelpSection.Help.<Separator>.Value)
' sb.AppendLine(HelpSection.Help.<Syntax>.Value)
' sb.AppendLine(HelpSection.Help.<SyntaxExtra>.Value)
' sb.AppendLine(HelpSection.Help.<UsageExamples>.Value)
' HelpSection.WriteColoredTextLine(sb.ToString)
' ' HelpSection.WriteColoredText(sb.ToString)
' Threading.Thread.Sleep(60000)
' End Sub
'End Module
#End Region
#Region " Example 2 "
' Individual Method Usage:
'
' HelpSection.WriteColoredText(" Hello World! ", ConsoleColor.Blue, ConsoleColor.Blue)
' HelpSection.WriteColoredTextLine(" Hello World! ", ConsoleColor.Magenta, ConsoleColor.Gray)
' HelpSection.WriteColoredText("{F10}Hello {F14}World!{-F}")
' HelpSection.WriteColoredTextLine("{B15}{F12} Hello World! {-F}{-B}")
#End Region
#End Region
#Region " ConsoleColor Enumeration Helper "
' Black = 0
' DarkBlue = 1
' DarkGreen = 2
' DarkCyan = 3
' DarkRed = 4
' DarkMagenta = 5
' DarkYellow = 6
' Gray = 7
' DarkGray = 8
' Blue = 9
' Green = 10
' Cyan = 11
' Red = 12
' Magenta = 13
' Yellow = 14
' White = 15
#End Region
#Region " HelpSection "
Friend Class HelpSection
#Region " Members "
'''' <summary>
'''' Indicates dynamically the name of the current process.
'''' </summary>
Private ReadOnly ProcessName As String =
Process.GetCurrentProcess().MainModule.ModuleName
'''' <summary>
'''' Use this var into an XML if need to escape an '>' character.
'''' </summary>
Private ReadOnly c_GreaterThan As Char = ">"c
'''' <summary>
'''' Use this var into an XML if need to escape an '<' character.
'''' </summary>
Private ReadOnly c_LowerThan As Char = "<"c
#End Region
#Region " Help Text "
Friend Shared ReadOnly Help As XElement =
<Help>
<!-- Current process name -->
<!-- That means even when the user manually changes the executable name -->
<Process>{F10}<%= ProcessName %>{-F}</Process>
<!-- Application title -->
<Title>My Application .:: By Elektro ::.</Title>
<!-- Application name -->
<Name>{F10}My Application{-F}</Name>
<!-- Application author -->
<Author>{f10}Elektro{-F}</Author>
<!-- Application version -->
<Version>{F10}1.0{-F}</Version>
<!-- Copyright information -->
<Copyright>{F10}© Elektro Software 2014{-F}</Copyright>
<!-- Website information -->
<Website>{F10}http://foro.elhacker.net{-F}</Website>
<!-- Skype contact information -->
<Skype>{F10}ElektroStudios{-F}</Skype>
<!-- Email contact information -->
<Email>{F10}ElektroStudios@ElHacker.Net{-F}</Email>
<!-- Application Logotype -->
<Logo>{F11}
.____
| | ____ ____ ____
| | / _ \ / ___\ / _ \
| |__( |_| ) /_/ > |_| )
|_______ \____/\___ / \____/
\/ /____/
{-F}</Logo>
<!-- Separator shape -->
<Separator>
{F11}------------------------------------------------------>>>>{-F}
</Separator>
<!-- Application Syntax -->
<Syntax>
{F11}[+]{-F} {F14}Syntax{-F}
<%= ProcessName %> {F10}(SWITCH){-F}={F10}(VALUE){-F} {F10}(IN FILE){-F}
</Syntax>
<!-- Application Syntax (Additional Specifications) -->
<SyntaxExtra>
{F11}[+]{-F} {F14}Switches{-F}
{F10}/Switch1{-F} | {F11}Description.{-F}
{F10}/Switch2{-F} | {F11}Description.{-F}
{F10} {-F} |
{F10}/? {-F} | {F11}Display this help.{-F}
{F11}[+]{-F} {F14}Switch value types{-F}
{F10}/Switch1{-F} ({F11}INT{-F})
{F10}/Switch2{-F} ({F11}X,Y{-F})
</SyntaxExtra>
<!-- Application Usage Examples -->
<UsageExamples>
{F11}[+]{-F} {F14}Usage examples{-F}
<%= ProcessName %> /Switch1=Value "C:\File.txt"
{F11}( Command explanation ){-F}
</UsageExamples>
</Help>
#End Region
#Region " Public Methods "
''' <summary>
''' Writes colored text on the Console.
''' </summary>
''' <param name="Text">Indicates the color-delimited text to parse and then write.</param>
Friend Shared Sub WriteColoredText(ByVal Text As String)
' Split the string to retrieve and parse the color delimitters.
Dim StringParts As String() =
Text.Split({"{"c, "}"c}, StringSplitOptions.RemoveEmptyEntries)
' If there is any string part to parse then Exit.
If StringParts.Count = 0 Then
Exit Sub
End If
' Store the current console colors to restore them later.
Dim CurrentForegroundColor As ConsoleColor = Console.ForegroundColor
Dim CurrentBackgroundColor As ConsoleColor = Console.BackgroundColor
' A flag that indicates whether a string should be printed (to avoid color-delimitter prints).
Dim PrintFlag As Boolean = False
' Parse the string parts
For Each str As String In StringParts
If str.ToLower Like "f#" _
OrElse str.ToLower Like "f##" Then ' Change the ForeColor.
PrintFlag = False
Console.ForegroundColor = CInt(str.Replace(str.First, String.Empty))
ElseIf str.ToLower Like "b#" _
OrElse str.ToLower Like "b##" Then ' Change the BackgroundColor.
PrintFlag = False
Console.BackgroundColor = CInt(str.Replace(str.First, String.Empty))
ElseIf str.ToLower Like "-f" Then ' Restore the original Forecolor.
PrintFlag = False
Console.ForegroundColor = CurrentForegroundColor
ElseIf str.ToLower Like "-b" Then ' Restore the original BackgroundColor.
PrintFlag = False
Console.BackgroundColor = CurrentBackgroundColor
Else ' String is not a delimitter so we can print it.
PrintFlag = True
End If
' If it is not a delimitter like '{Fx}/{Bx}/{Fxx}/{Bxx}/{-F}/{-B}' then print the string part.
If PrintFlag Then
Console.Write(str)
End If
Next str
' Finish by restoring the original console colors.
Console.BackgroundColor = CurrentBackgroundColor
Console.ForegroundColor = CurrentForegroundColor
End Sub
''' <summary>
''' Writes colored text on the Console.
''' </summary>
''' <param name="Text">Indicates the text to write.</param>
''' <param name="ForeColor">Indicates the text color.</param>
''' <param name="BackColor">Indicates the background color.</param>
Friend Shared Sub WriteColoredText(ByVal Text As String,
Optional ByVal ForeColor As ConsoleColor = Nothing,
Optional ByVal BackColor As ConsoleColor = Nothing)
' Store the current console colors to restore them later.
Dim CurrentForegroundColor As ConsoleColor = Console.ForegroundColor
Dim CurrentBackgroundColor As ConsoleColor = Console.BackgroundColor
' Set the new temporal console colors.
Console.ForegroundColor = If(ForeColor = Nothing, CurrentForegroundColor, ForeColor)
Console.BackgroundColor = If(ForeColor = Nothing, CurrentBackgroundColor, BackColor)
' Print the text.
Console.Write(Text)
' Finish by restoring the original console colors.
Console.ForegroundColor = CurrentForegroundColor
Console.BackgroundColor = CurrentBackgroundColor
End Sub
''' <summary>
''' Writes colored text on the Console and adds an empty line at the end.
''' </summary>
''' <param name="Text">Indicates the color-delimited text to parse and then write.</param>
Friend Shared Sub WriteColoredTextLine(ByVal Text As String)
WriteColoredText(Text & Environment.NewLine)
End Sub
''' <summary>
''' Writes colored text on the Console and adds an empty line at the end.
''' </summary>
''' <param name="Text">Indicates the color-delimited text to parse and then write.</param>
''' <param name="ForeColor">Indicates the text color.</param>
''' <param name="BackColor">Indicates the background color.</param>
Friend Shared Sub WriteColoredTextLine(ByVal Text As String,
Optional ByVal ForeColor As ConsoleColor = Nothing,
Optional ByVal BackColor As ConsoleColor = Nothing)
WriteColoredText(Text & Environment.NewLine, ForeColor, BackColor)
End Sub
#End Region
End Class
#End Region