-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
220 lines (180 loc) · 6.74 KB
/
Program.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BaGet.Protocol;
using BaGet.Protocol.Models;
using Microsoft.DotNet.Interactive.Utility;
using NStack;
using Terminal.Gui;
namespace Nugs
{
public class Program
{
public static void Main(string[] args)
{
Application.Run<App>();
}
}
public class App : Window
{
private readonly NuGetClient _client;
private IReadOnlyList<SearchResult> _items;
private readonly Label _search;
private readonly TextField _searchText;
private readonly ListView _searchList;
public App() : base("Nugs")
{
_client = new NuGetClient("https://api.nuget.org/v3/index.json");
_items = null;
Width = Dim.Fill();
Height = Dim.Fill();
_search = new Label ("Search: ") { X = 3, Y = 1 };
_searchText = new TextField("")
{
X = Pos.Right(_search),
Y = Pos.Top(_search),
Width = 40,
};
_searchText.Changed += SearchText_Changed;
_searchList = new ListView(new string[0])
{
X = Pos.Left(_search),
Y = Pos.Top(_search) + 3,
Width = Dim.Fill(),
};
Add(_search, _searchText, _searchList);
FocusLast();
}
#region Key handlers
public override bool ProcessHotKey(KeyEvent keyEvent)
{
if (base.ProcessHotKey(keyEvent)) return true;
if (_searchText.ProcessHotKey(keyEvent)) return true;
return false;
}
public override bool ProcessKey(KeyEvent keyEvent)
{
if (base.ProcessKey(keyEvent)) return true;
if (_searchText.ProcessKey(keyEvent)) return true;
if (_searchList.HasFocus && keyEvent.Key == Key.Enter && _items != null)
{
var item = _items[_searchList.SelectedItem];
Application.Run(new InstallationDialog(item));
}
return false;
}
public override bool ProcessColdKey(KeyEvent keyEvent)
{
if (base.ProcessColdKey(keyEvent)) return true;
if (_searchText.ProcessColdKey(keyEvent)) return true;
return false;
}
#endregion
private async void SearchText_Changed(object sender, ustring e)
{
await OnSearchAsync();
}
private async Task OnSearchAsync()
{
// TODO: Cancel the previous search if it exists.
_searchList.SetSource(new string[] { "..." });
_items = null;
var text = _searchText.Text;
await Task.Delay(TimeSpan.FromMilliseconds(200));
if (_searchText.Text != text)
{
return;
}
var results = await _client.SearchAsync(text.ToString(), includePrerelease: true);
if (_searchText.Text != text)
{
return;
}
_items = results;
_searchList.SetSource(_items.Select(i => i.PackageId).ToList());
}
}
public class InstallationDialog : Dialog
{
private static readonly string[] MagnitudeAbbreviations = new string[] { "", "k", "M", "B", "T", "q", "Q", "s", "S", "o", "n" };
public InstallationDialog(SearchResult item)
: base(item.PackageId, width: 80, height: 12)
{
var cancel = new Button(3, 14, "Cancel")
{
Clicked = () => Application.RequestStop()
};
var install = new Button(10, 14, "Install")
{
Clicked = () =>
{
var resultText = new TextView
{
Text = "Installing...",
ReadOnly = true,
Height = 15
};
var resultDialog = new Dialog(
"Install package",
width: 80,
height: 30,
new Button("Ok")
{
Clicked = () => Application.RequestStop()
});
resultDialog.Add(resultText);
Application.MainLoop.Invoke(async () =>
{
var dotnet = new Dotnet();
var result = await dotnet.AddPackage(item.PackageId, item.Version);
resultText.Text = string.Join("\n", result.Output);
});
Application.Run(resultDialog);
Application.RequestStop();
}
};
var details = $"Downloads: {FormatDownloads(item.TotalDownloads)}\n";
details += $"Tags: {string.Join(", ", item.Tags)}\n";
details += $"\n";
details += $"Description:\n";
details += item.Description;
var text = new TextView
{
Text = details,
ReadOnly = true,
Height = 5
};
AddButton(cancel);
AddButton(install);
Add(text);
}
// https://github.com/NuGet/NuGetGallery/blob/e8acf7d79ebaf19e561565a865f9be14b99a5f36/src/NuGetGallery/ViewModels/StatisticsPackagesViewModel.cs#L145
private string FormatDownloads(double number, int sigFigures = 3)
{
var numDiv = 0;
while (number >= 1000)
{
number /= 1000;
numDiv++;
}
// Find a rounding factor based on size, and round to sigFigures, e.g. for 3 sig figs, 1.774545 becomes 1.77.
var placeValues = Math.Ceiling(Math.Log10(number));
var roundingFactor = Math.Pow(10, sigFigures - placeValues);
var roundedNum = Math.Round(number * roundingFactor) / roundingFactor;
// Pad from right with zeroes to sigFigures length, so for 3 sig figs, 1.6 becomes 1.60
var formattedNum = roundedNum.ToString("F" + sigFigures);
var desiredLength = formattedNum.Contains('.') ? sigFigures + 1 : sigFigures;
if (formattedNum.Length > desiredLength)
{
formattedNum = formattedNum.Substring(0, desiredLength);
}
formattedNum = formattedNum.TrimEnd('.');
if (numDiv >= MagnitudeAbbreviations.Length)
{
return formattedNum + $"10^{numDiv*3}";
}
return formattedNum + MagnitudeAbbreviations[numDiv];
}
}
}