Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

javascript - Async Input / Output CLI deno

I'm building a chat program in deno cli. I already wrote the function that gets new messages and prints them on screen every 1 second. While it's doing this it also asynchronously listens to the cli input (using standard deno readLines) so when the user hits enter his message gets posted. However if you happen to be writing a message and the refresh hits before you press enter, the new messages are written right next to what you were writing and you need to type it again.

terminal test

As you can see, I was typing nonsense (fdghsjkasjdhfv) but Alice said hello again, the refresh function kicked in and wrote the new message right after what I was typing.

The question is: Is there a way to save the typed stuff, clear it and print it back once the refresh function has posted the new messages?

The code I'm using:

import { readLines } from "https://deno.land/std/io/mod.ts";

async function read() {
    for await(const line of readLines(Deno.stdin)) {
        if (!settings.printing) return line;
    }
}

let settings = { exit: false, user: 'anon', lastID: 0, printing: false };

async function main() {
    console.log('== DISCUSSION ==========================');
    printMessages();
    do {
        let line = await read();
        if (/^!s*(quit|exit)$/.test(line)) settings.exit = true;
        else sendMessage(line);
    } while (!exit);
}

await main();

function sendMessage(text) {
    postRequest('postMessage', {
        user: settings.user, data: text
    }).then(response => {
        if (response.status != 200) {
            console.log('[Connection Error!]');
            return;
        }
    });
}

function printMessages() {
    postRequest('getDiscussion', {
        id: settings.lastID // get the messages after the lastID received
    }).then(response => {
        if (response.status != 200) return;
        response.json().then(j => {
            if (j.length != 0) settings.lastID = j[j.length - 1].id;
            settings.printing = true;
            for (const m of j) {
                const time = new Date(m.time).toTimeString().slice(0, 5);
                console.log(
                    `[${time}][${m.user}]: `,
                    m.data
                );
            }
            settings.printing = false;
            if (!settings.exit) setTimeout(printMessages, 1000);
        });
    });
}


function postRequest(api, data) {
    let form = new FormData();
    for (const property in data) {
        form.append(property, data[property]);
    }
    return fetch(`https://example.com/api/${api}.php`, {
        method: "POST",
        body: form
    });
}

question from:https://stackoverflow.com/questions/65905677/async-input-output-cli-deno

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...