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
using System;
using System.Collections.Generic;
using System.IO;

class MyLess
{
    int height = 20;
    List<string> lines = new List<string>();
    int pos = 0;
    string search = null;

    MyLess(TextReader tr) {
        string line;
        while ((line = tr.ReadLine()) != null)
            lines.Add(line);
        tr.Close();
    }
    void Show() {
        Console.Clear();
        for (int i = 0 ; i < height && (i + pos) < lines.Count ; i++)
            Console.WriteLine("{0,5}{1} {2}", i + pos + 1, i == 0 ? '>' : '|', lines[i + pos]);
    }
    void Forward() {
        pos = pos + height > lines.Count ? lines.Count - 1 : pos + height;
    }
    void Back() {
        pos = pos < height ? 0 : pos - height;
    }
    void Find(bool forward) {
        if (search == null) return;

        bool found = false;
        if (forward) {
            for (int i = pos + 1 ; i < lines.Count ; i++) {
                if (lines[i].Contains(search)) {
                    found = true; pos = i; break;
                }
            }
        } else {
            for (int i = pos - 1 ; i >= 0 ; i--) {
                if (lines[i].Contains(search)) {
                    found = true; pos = i; break;
                }
            }
        }
        Show();
        if (found)
            Console.Write("{0}{1}", forward ? '/' : '?', search);
        else
            Console.Write("見つかりません");
    }
    void Do() {
        Show();
        while (true) {
            ConsoleKeyInfo key = Console.ReadKey(true);
            switch (key.KeyChar) {
                case 'q': return;
                case 'f': Forward(); Show(); break;
                case 'b': Back(); Show(); break;
                case '/':
                    Console.Write('/');
                    search = Console.ReadLine().Replace("\n", "");
                    if (search.Length == 0)
                        search = null;
                    Find(true);
                    break;
                case 'n': Find(true); break;
                case 'N': Find(false); break;
                default: break;
            }
        }
    }

    static void Main(string[] args) {
        MyLess less;
        if (args.Length == 0)
            less = new MyLess(Console.In);
        else
            less = new MyLess(new StreamReader(args[0]));

        less.Do();
    }
}