System.Globalization
System.Collections
System.Data
System.Diagnostics
System.Linq.Expressions
System.Reflection
System.Text
System.Text.RegularExpressions
System.Threading
System.Transactions
System.Xml
System.Xml.Linq
System.Xml.XPath
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 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 suggestions)
{
var suggestionList =
(suggestions ?? Enumerable.Empty())
.Select((s, i) => new KeyValuePair(i + 1, s))
.ToList();
if (defaultValue != null)
suggestionList.Insert(0, new KeyValuePair(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() {}