Skip to content

Instantly share code, notes, and snippets.

@atifaziz

atifaziz/Util.linq

Last active Aug 27, 2020
Embed
What would you like to do?
LINQPadless helper for LINQPad's Util ๐Ÿ“„ http://share.linqpad.net/age6lg.linq
<Query Kind="Program">
<Namespace>System.Globalization</Namespace>
<RemoveNamespace>System.Collections</RemoveNamespace>
<RemoveNamespace>System.Data</RemoveNamespace>
<RemoveNamespace>System.Diagnostics</RemoveNamespace>
<RemoveNamespace>System.Linq.Expressions</RemoveNamespace>
<RemoveNamespace>System.Reflection</RemoveNamespace>
<RemoveNamespace>System.Text</RemoveNamespace>
<RemoveNamespace>System.Text.RegularExpressions</RemoveNamespace>
<RemoveNamespace>System.Threading</RemoveNamespace>
<RemoveNamespace>System.Transactions</RemoveNamespace>
<RemoveNamespace>System.Xml</RemoveNamespace>
<RemoveNamespace>System.Xml.Linq</RemoveNamespace>
<RemoveNamespace>System.Xml.XPath</RemoveNamespace>
</Query>
static class Util
{
#if LINQPAD
public static string CurrentQueryName => LINQPad.Util.CurrentQuery.Name;
public static string CurrentQueryPath => LINQPad.Util.CurrentQueryPath;
public static string ReadLine() =>
ReadLine(null);
public static string ReadLine(string prompt) =>
LINQPad.Util.ReadLine(prompt);
public static string ReadLine(string prompt, string defaultValue) =>
LINQPad.Util.ReadLine(prompt, defaultValue);
public static string ReadLine(string prompt, string defaultValue, IEnumerable<string> suggestions) =>
LINQPad.Util.ReadLine(prompt, defaultValue, suggestions);
#else
static LINQPad.Query _currentQuery;
public static string CurrentQueryPath => Environment.GetEnvironmentVariable("LPLESS_LINQ_FILE_PATH") ?? UserQuery.PATH;
public static LINQPad.Query CurrentQuery => _currentQuery ??= new LINQPad.Query(CurrentQueryPath);
public static string CurrentQueryName => CurrentQuery.Name;
public static string ReadLine() =>
ReadLine(null);
public static string ReadLine(string prompt) =>
ReadLine(prompt, null);
public static string ReadLine(string prompt, string defaultValue) =>
ReadLine(prompt, defaultValue, null);
public static string ReadLine(string prompt, string defaultValue, IEnumerable<string> suggestions)
{
var suggestionList =
(suggestions ?? Enumerable.Empty<string>())
.Select((s, i) => new KeyValuePair<int, string>(i + 1, s))
.ToList();
if (defaultValue != null)
suggestionList.Insert(0, new KeyValuePair<int, string>(0, defaultValue));
foreach (var (n, s) in suggestionList)
Console.Error.WriteLine($"{n}. {s}");
var raw = suggestionList.Count == 0;
if (!raw)
Console.Error.WriteLine("(start with $ for custom line where \"$example\" becomes \"example\")");
while (true)
{
Console.Error.Write(prompt + " ");
Console.Error.Flush();
var line = Console.ReadLine();
Console.Error.WriteLine(line);
if (line is null)
return null;
if (raw)
return line;
if (line.Length > 0 && line[0] == '$')
return line.Substring(1);
if (defaultValue != null && line.Length == 0)
return defaultValue;
if (int.TryParse(line, NumberStyles.None, CultureInfo.InvariantCulture, out var selection)
&& suggestionList.FindIndex(s => s.Key == selection) is {} i && i >= 0)
{
return suggestionList[i].Value;
}
Console.Error.WriteLine("Invalid entry!");
}
}
#endif
}
namespace LINQPad
{
#if !LINQPAD
public sealed class Query
{
public Query(string path) =>
FilePath = path ?? throw new ArgumentNullException(nameof(path));
public string Name => Path.GetFileNameWithoutExtension(FilePath);
public string FilePath { get; }
public string Location => Path.GetDirectoryName(FilePath);
}
#endif
}
void Main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment