summary refs log tree commit diff
path: root/screen/home.ts
blob: 296fec2dfafff128e1dfb7d5b1a6bee81188d7f8 (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
import { ElemType } from "../screenbuilder.ts";
import { Screen } from "../screen.ts";
import type { Input, Text } from "../elements.ts";
import process from "node:process";

function changeTitle(title: string) {
    if (process.platform == 'win32') {
        process.title = title;
    } else {
        process.stdout.write(`\x1b]2;${title}\x1b\x5c`);
    }
}

export default {
    elements: [
        {
            type: ElemType.TextElem,
            id: 'home',
            data: ["Loading home posts...\n", function (this: Text) {
                const msgInput: Input = this.screen.elements.get("msg-input") as Input;
                const inputValueHeight = msgInput.value.split("\n").length + 1;
                const termHeight = process.stdout.rows;
                const termWidth  = process.stdout.columns;

                let splitText = this.text.split("\n");
                splitText = splitText.map(t => t.replace(new RegExp(`([^]){${termWidth}}`, "g"),"$1\n"));
                splitText = splitText.join("\n").split("\n")

                splitText = splitText.slice(-(termHeight - inputValueHeight));

                return splitText.join("\n")
            }]
        },
        {
            type: ElemType.HR,
            id: 'hr',
            data: []
        },
        {
            type: ElemType.InputElem,
            id: 'msg-input',
            data: [false, false, true]
        },
        {
            type: ElemType.ButtonElem,
            id: 'done-btn',
            data: ["Send", function (this: Screen) {
                const msgInput: Input = this.elements.get('msg-input') as Input
                this.client.sendHome(msgInput.value);
                msgInput.value = ""
                this.render()
            }]
        }
    ],
    focus: "msg-input",
    name: 'home',
    onload (screen: Screen) {
        screen.client.setScreen(screen)
        screen.client.loadHome(screen)
        changeTitle(`maelink - home`)
    }
}