13 public static int Main(
string[] args)
15 Func<IOptions, string> reader = opts =>
17 var fromTop = opts.GetType() == typeof(
HeadOptions);
18 return opts.Lines.HasValue
19 ?
ReadLines(opts.FileName, fromTop, (
int)opts.Lines)
20 :
ReadBytes(opts.FileName, fromTop, (
int)opts.Bytes);
22 Func<IOptions, string> header = opts =>
28 var fromTop = opts.GetType() == typeof(
HeadOptions);
29 var builder =
new StringBuilder(
"Reading ");
30 builder = opts.Lines.HasValue
31 ? builder.Append(opts.Lines).Append(
" lines")
32 : builder.Append(opts.Bytes).Append(
" bytes");
33 builder = fromTop ? builder.Append(
" from top:") : builder.Append(
" from bottom:");
34 return builder.ToString();
36 Action<string> printIfNotEmpty = text =>
38 if (text.Length == 0) {
return; }
39 Console.WriteLine(text);
45 (
HeadOptions opts) => Tuple.Create(header(opts), reader(opts)),
46 (
TailOptions opts) => Tuple.Create(header(opts), reader(opts)),
49 printIfNotEmpty(texts.Item1);
50 printIfNotEmpty(texts.Item2);
65 private static string ReadBytes(
string fileName,
bool fromTop,
int count)
67 var bytes = File.ReadAllBytes(fileName);
70 return Encoding.UTF8.GetString(bytes, 0, count);
72 return Encoding.UTF8.GetString(bytes, bytes.Length - count, count);