-
Notifications
You must be signed in to change notification settings - Fork 2
/
SyntaxHighlighter.cs
83 lines (72 loc) · 2.69 KB
/
SyntaxHighlighter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Drawing;
using System.Windows.Forms;
namespace HostsManager
{
public static class SyntaxHighlighter
{
public static void Highlight(String[] raw, RichTextBox rtb, ListView lsv)
{
lsv.Items.Clear();
rtb.Clear();
foreach (string line in raw)
{
bool isDisabledEntry = line.StartsWith(HostsFileManager.DISABLED_INDICATOR);
if (line.StartsWith("#") && !isDisabledEntry)
{
rtb.SelectionColor = Color.DarkGreen;
rtb.AppendText(line);
}
else
{
string aLine = line;
if (isDisabledEntry)
{
aLine = line.Substring(HostsFileManager.DISABLED_INDICATOR.Length).Trim();
}
string[] parts = aLine
.Replace('\t', ' ')
.Trim()
.Split(' ');
if (parts.Length == 2)
{
String Address = parts[0];
String Host = parts[1];
if (isDisabledEntry)
{
rtb.SelectionColor = Color.DarkGreen;
rtb.AppendText(HostsFileManager.DISABLED_INDICATOR);
rtb.SelectionColor = Color.Gray;
rtb.AppendText(parts[0]);
rtb.AppendText(" ");
rtb.SelectionColor = Color.DarkGray;
rtb.AppendText(parts[1]);
}
else
{
rtb.SelectionColor = Color.Blue;
rtb.AppendText(parts[0]);
rtb.AppendText(" ");
rtb.SelectionColor = Color.Purple;
rtb.AppendText(parts[1]);
}
var lvi = new ListViewItem()
{
Text = isDisabledEntry ? "No" : "Yes"
};
lvi.SubItems.Add(Host);
lvi.SubItems.Add(Address);
lvi.Tag = Host;
lsv.Items.Add(lvi);
}
else
{
rtb.SelectionColor = Color.Red;
rtb.AppendText(line);
}
}
rtb.AppendText("\r\n");
}
}
}
}