-
Notifications
You must be signed in to change notification settings - Fork 1
/
SourceTextView.cs
148 lines (124 loc) · 3.43 KB
/
SourceTextView.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Cilurbo.Services;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using Terminal.Gui;
namespace Cilurbo;
public enum Languages {
IL,
CSharp,
}
class SourceView : View {
public SourceView ()
{
Width = Dim.Fill ();
Height = Dim.Fill ();
TextView = new SourceTextView (this);
Add (TextView);
ScrollBarView sbv = new (TextView, true);
// copied from terminal.gui sample
sbv.ChangedPosition += () => {
TextView.TopRow = sbv.Position;
if (TextView.TopRow != sbv.Position) {
sbv.Position = TextView.TopRow;
}
TextView.SetNeedsDisplay ();
};
sbv.OtherScrollBarView.ChangedPosition += () => {
TextView.LeftColumn = sbv.OtherScrollBarView.Position;
if (TextView.LeftColumn != sbv.OtherScrollBarView.Position) {
sbv.OtherScrollBarView.Position = TextView.LeftColumn;
}
TextView.SetNeedsDisplay ();
};
sbv.VisibleChanged += () => {
if (sbv.Visible && TextView.RightOffset == 0) {
TextView.RightOffset = 1;
} else if (!sbv.Visible && TextView.RightOffset == 1) {
TextView.RightOffset = 0;
}
};
sbv.OtherScrollBarView.VisibleChanged += () => {
if (sbv.OtherScrollBarView.Visible && TextView.BottomOffset == 0) {
TextView.BottomOffset = 1;
} else if (!sbv.OtherScrollBarView.Visible && TextView.BottomOffset == 1) {
TextView.BottomOffset = 0;
}
};
TextView.DrawContent += (e) => {
sbv.Size = TextView.Lines;
sbv.Position = TextView.TopRow;
if (sbv.OtherScrollBarView != null) {
sbv.OtherScrollBarView.Size = TextView.Maxlength;
sbv.OtherScrollBarView.Position = TextView.LeftColumn;
}
sbv.LayoutSubviews ();
sbv.Refresh ();
};
}
public SourceTextView TextView { get; private set; }
public string? Title { get; set; }
Languages language = Languages.CSharp;
public Languages Language {
get { return language; }
set {
if (language != value) {
language = value;
Show (current_metadata);
}
}
}
object? current_metadata;
public void Show (object? metadata)
{
var tab = (SuperView.SuperView as TabView)!.SelectedTab;
switch (metadata) {
case PEFile file:
Title = file.Name;
tab.Text = file.Name;
TextView.Text = Language == Languages.IL ? file.Disassemble () : file.Decompile ();
break;
case IEntity entity:
Title = entity.Name;
tab.Text = entity.Name;
TextView.Text = Language == Languages.IL ? entity.Disassemble () : entity.Decompile ();
break;
default:
Title = "-";
tab.Text = "-";
TextView.Text = "";
break;
}
current_metadata = metadata;
}
}
class SourceTextView : TextView {
readonly SourceView parent;
public SourceTextView (SourceView parentView)
{
parent = parentView;
Width = Dim.Fill ();
Height = Dim.Fill ();
ReadOnly = true;
}
public override bool ProcessKey (KeyEvent kb)
{
switch (kb.Key) {
case Key.CtrlMask | Key.E:
SaveSource ();
return true;
case Key.CtrlMask | Key.G:
// syntax coloring is based on the file extension, the filename is not really important
_ = GistSupport.Gist (parent.Title ?? "Cilurbo", parent.Language == Languages.IL ? "code.il" : "code.cs", Text.ToString ());
return true;
}
return base.ProcessKey (kb);
}
void SaveSource ()
{
using SaveDialog d = new ("Export Source", "", new () { parent.Language == Languages.IL ? ".il" : ".cs" });
Application.Run (d);
if (!d.Canceled && (d.FilePath is not null)) {
File.WriteAllText (d.FilePath.ToString ()!, Text.ToString ());
}
}
}