aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-my.js
blob: 5fc13ca3fed1f64cc8dad269dc953b544deb40e5 (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
class MyModule {
    constructor() {
        this.commands = {}; this.terminal = null; this.userData = {};
        try { const d = localStorage.getItem('webx11_my_data'); if (d) this.userData = JSON.parse(d); } catch (e) {}
    }
    getHelpInfo() {
        return {
            'my': 'my — Show module info and stored key count',
            'my-set': 'my-set <key> <value> — Store a key-value pair',
            'my-get': 'my-get <key> — Retrieve a stored value by key',
            'my-list': 'my-list — List all stored keys',
            'my-export': 'my-export — Export all stored data as JSON',
            'my-import': 'my-import <json> — Import data from JSON string',
            'my-clear': 'my-clear — Clear all stored data'
        };
    }
    _save() {
        try { localStorage.setItem('webx11_my_data', JSON.stringify(this.userData)); } catch (e) {}
    }
    registerCommands() {
        this.commands['my'] = () => this.showInfo();
        this.commands['my-set'] = (args) => this.setData(args);
        this.commands['my-get'] = (args) => this.getData(args);
        this.commands['my-list'] = () => this.listData();
        this.commands['my-export'] = () => this.exportData();
        this.commands['my-import'] = (args) => this.importData(args);
        this.commands['my-clear'] = () => this.clearData();
    }
    showInfo() {
        this.terminal.print("=== My Module v2.0 ===", "info");
        this.terminal.print("Personal workspace for custom commands");
        this.terminal.print(`Stored keys: ${Object.keys(this.userData).length}`, "success");
    }
    setData(args) {
        if (args.length < 2) {
            this.terminal.print("Usage: my-set <key> <value>", "warning"); return;
        }
        const key = args[0], value = args.slice(1).join(' ');
        this.userData[key] = { value, timestamp: new Date().toISOString() };
        this._save();
        this.terminal.print(`Saved: ${key} = ${value}`, "success");
    }
    getData(args) {
        if (!args[0]) {
            this.terminal.print("Usage: my-get <key>", "warning"); return;
        }
        if (this.userData[args[0]]) {
            this.terminal.print(`${args[0]}: ${this.userData[args[0]].value}`, "info");
        } else {
            this.terminal.print(`Key '${args[0]}' not found`, "error");
        }
    }
    listData() {
        const keys = Object.keys(this.userData);
        if (!keys.length) return this.terminal.print('No data stored.', 'info');
        this.terminal.print('=== My Data ===', 'info');
        keys.forEach(k => {
            const v = this.userData[k];
            const type = typeof v.value;
            this.terminal.print(`  ${k} (${type}): ${String(v.value).substring(0, 40)}`);
        });
    }
    exportData() {
        this.terminal.print(JSON.stringify(this.userData, null, 2));
    }
    importData(args) {
        if (!args[0]) return this.terminal.print('Usage: my-import <json>', 'warning');
        try {
            const data = JSON.parse(args.join(' '));
            Object.assign(this.userData, data);
            this._save();
            this.terminal.print(`Imported ${Object.keys(data).length} keys`, 'success');
        } catch (e) {
            this.terminal.print('Invalid JSON: ' + e.message, 'error');
        }
    }
    clearData() {
        this.userData = {};
        localStorage.removeItem('webx11_my_data');
        this.terminal.print("Personal data cleared", "success");
    }
}