aboutsummaryrefslogtreecommitdiff
path: root/src/server/session_manager/input_manager.ts
blob: a95e6baaed348556af2e6894e2a0ddebe9bc91ab (plain)
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
import { createInterface, Interface } from "readline";
import { red } from "colors";

export interface Configuration {
    identifier: string;
    onInvalid?: (culprit?: string) => string | string;
    isCaseSensitive?: boolean;
}

export interface Registration {
    argPattern: RegExp[];
    action: (parsedArgs: IterableIterator<string>) => any | Promise<any>;
}

export default class InputManager {
    private identifier: string;
    private onInvalid: ((culprit?: string) => string) | string;
    private isCaseSensitive: boolean;
    private commandMap = new Map<string, Registration[]>();
    public interface: Interface;
    private busy = false;
    private keys: string | undefined;

    constructor({ identifier: prompt, onInvalid, isCaseSensitive }: Configuration) {
        this.identifier = prompt;
        this.onInvalid = onInvalid || this.usage;
        this.isCaseSensitive = isCaseSensitive ?? true;
        this.interface = createInterface(process.stdin, process.stdout).on('line', this.considerInput);
    }

    private usage = () => {
        const resolved = this.keys;
        if (resolved) {
            return resolved;
        }
        const members: string[] = [];
        const keys = this.commandMap.keys();
        let next: IteratorResult<string>;
        while (!(next = keys.next()).done) {
            members.push(next.value);
        }
        return `${this.identifier} commands: { ${members.sort().join(", ")} }`;
    }

    public registerCommand = (basename: string, argPattern: RegExp[], action: any | Promise<any>) => {
        const existing = this.commandMap.get(basename);
        const registration = { argPattern, action };
        if (existing) {
            existing.push(registration);
        } else {
            this.commandMap.set(basename, [registration]);
        }
    }

    private invalid = (culprit?: string) => {
        console.log(red(typeof this.onInvalid === "string" ? this.onInvalid : this.onInvalid(culprit)));
        this.busy = false;
    }

    private considerInput = async (line: string) => {
        if (this.busy) {
            console.log(red("Busy"));
            return;
        }
        this.busy = true;
        line = line.trim();
        if (this.isCaseSensitive) {
            line = line.toLowerCase();
        }
        const [command, ...args] = line.split(/\s+/g);
        if (!command) {
            return this.invalid();
        }
        const registered = this.commandMap.get(command);
        if (registered) {
            const { length } = args;
            const candidates = registered.filter(({ argPattern: { length: count } }) => count === length);
            for (const { argPattern, action } of candidates) {
                const parsed: string[] = [];
                let matched = false;
                if (length) {
                    for (let i = 0; i < length; i++) {
                        let matches: RegExpExecArray | null;
                        if ((matches = argPattern[i].exec(args[i])) === null) {
                            break;
                        }
                        parsed.push(matches[0]);
                    }
                    matched = true;
                }
                if (!length || matched) {
                    await action(parsed[Symbol.iterator]());
                    this.busy = false;
                    return;
                }
            }
        }
        this.invalid(command);
    }

}