aboutsummaryrefslogtreecommitdiff
path: root/modules/ext/alias.js
blob: 80663ac474be6fa8d24d4228fa2632b32269d84c (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
102
103
104
105
106
107
108
class AliasModule {
    constructor() {
        this.commands = {};
        this.terminal = null;
        this.key = 'webx11_aliases';
        this.defaults = { 'll': 'ls', 'h': 'help', 'cls': 'clear' };
        this.aliases = this.load();
    }
    
    getHelpInfo() {
        return {
            'alias': 'List, set, or show aliases (usage: alias [name=value])',
            'unalias': 'Remove a user-defined alias (usage: unalias <name>)',
        };
    }
    
    load() {
        try {
            const data = localStorage.getItem(this.key);
            return data ? { ...this.defaults, ...JSON.parse(data) } : { ...this.defaults };
        } catch (e) { return { ...this.defaults }; }
    }
    save() {
        const custom = {};
        Object.keys(this.aliases).forEach(k => {
            if (!this.defaults[k]) custom[k] = this.aliases[k];
        });
        localStorage.setItem(this.key, JSON.stringify(custom));
    }
    registerCommands() {
        const self = this;
        // Register management commands
        this.commands['alias'] = (args) => self.handleAlias(args);
        this.commands['unalias'] = (args) => self.handleUnalias(args);
        // Register each alias as a delegating command
        Object.keys(this.aliases).forEach(name => {
            if (name === 'alias' || name === 'unalias') return;
            const target = this.aliases[name];
            this.commands[name] = (args) => {
                this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : ''));
            };
        });
    }
    handleAlias(args) {
        if (!args[0]) {
            // List all aliases
            this.terminal.print('=== Aliases ===', 'info');
            const keys = Object.keys(this.aliases);
            if (keys.length === 0) {
                this.terminal.print('  No aliases defined', 'warning');
            } else {
                keys.forEach(name => {
                    const mark = this.defaults[name] ? ' (built-in)' : '';
                    this.terminal.print(`  ${name}=${this.aliases[name]}${mark}`);
                });
            }
            this.terminal.print('Usage: alias &lt;name&gt;=&lt;value&gt;', 'info');
            return;
        }
        // Parse name=value
        const input = args.join(' ');
        const eqIdx = input.indexOf('=');
        if (eqIdx === -1) {
            // Show specific alias value
            const name = args[0];
            if (this.aliases[name]) {
                this.terminal.print(`${name}=${this.aliases[name]}`, 'info');
            } else {
                this.terminal.print(`Alias '${name}' not found`, 'error');
            }
            return;
        }
        const name = input.substring(0, eqIdx).trim();
        const value = input.substring(eqIdx + 1).trim();
        if (!name) return this.terminal.print('Invalid alias name', 'error');
        if (!value) return this.terminal.print('Invalid alias value', 'error');
        if (name === 'alias' || name === 'unalias') {
            return this.terminal.print('Cannot alias reserved command name', 'error');
        }
        this.aliases[name] = value;
        this.save();
        // Register the new alias as a command immediately
        if (!this.defaults[name]) {
            const target = value;
            this.commands[name] = (args) => {
                this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : ''));
            };
        }
        this.terminal.print(`Alias set: ${name}=${value}`, 'success');
    }
    handleUnalias(args) {
        if (!args[0]) return this.terminal.print('Usage: unalias &lt;name&gt;', 'warning');
        const name = args[0];
        if (this.defaults[name]) {
            return this.terminal.print(`Cannot unalias built-in alias '${name}'`, 'warning');
        }
        if (!this.aliases[name]) {
            return this.terminal.print(`Alias '${name}' not found`, 'error');
        }
        delete this.aliases[name];
        delete this.commands[name];
        this.save();
        this.terminal.print(`Alias removed: ${name}`, 'success');
    }
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
    webx11.moduleManager.registerModule(new AliasModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/alias.js');
}