-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·117 lines (96 loc) · 3.58 KB
/
Copy pathProgram.cs
File metadata and controls
executable file
·117 lines (96 loc) · 3.58 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using System.ComponentModel.DataAnnotations;
using System.Web;
namespace ConsoleApplication
{
[HelpOption]
[Command(ThrowOnUnexpectedArgument = false)]
class Program
{
[Option(Description = "Open first result", LongName = "lucky", ShortName = "l")]
public bool Lucky { get; set; } = true;
[Option(Description = "Output option to console", LongName = "console", ShortName = "c")]
public bool Inline { get; set; } = true;
[Option(Description = "Number of results 1 - 25", LongName = "number", ShortName = "n")]
[Range(1, 25)]
public int Results { get; set; } = 5;
[Argument(0)]
public string Terms { get; set; }
public string[] RemainingArguments { get; }
public static int Main(string[] args)
=> CommandLineApplication.Execute<Program>(args);
private void OnExecute(CommandLineApplication app)
{
if (String.IsNullOrEmpty(Terms))
{
app.ShowHelp();
}
else
{
string encodedTerms = HttpUtility.HtmlEncode(Terms);
string locale = CultureInfo.CurrentCulture.Name;
if (Inline | Lucky)
{
string api = $"https://docs.microsoft.com/api/search?search={encodedTerms}&locale={locale}&$top={Results}";
var items = GetResults(api);
if (Lucky)
{
OpenResult(items.Result.results[0].url);
}
if (Inline)
{
Console.WriteLine("Items found: " + items.Result.count.ToString());
foreach (Result r in items.Result.results)
{
Console.WriteLine(r.iconType);
Console.WriteLine(r.title);
Console.WriteLine(r.url);
Console.WriteLine(" ");
}
}
}
else
{
string url = $"https://docs.microsoft.com/search/index?search={encodedTerms}";
OpenResult(url);
}
}
}
private void OpenResult(string url)
{
var cmd = "open";
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
cmd = "xdg-open";
Process.Start(new ProcessStartInfo(cmd, url));
}
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
cmd = "open";
Process.Start(new ProcessStartInfo(cmd, url));
}
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(Utils.GetDefaultBrowserPath(), url);
}
}
public async static Task<Docs> GetResults(string url)
{
var http = new HttpClient();
var response = await http.GetStringAsync(url);
var data = JsonConvert.DeserializeObject<Docs>(response);
return data;
}
}
}