cronvel / terminal-kit

Terminal utilities for node.js
MIT License
3.06k stars 196 forks source link

Use terminal-kit with not stream #23

Open pistacchio opened 7 years ago

pistacchio commented 7 years ago

Hi. First of all, thanks for this library, it is great. I'm trying to use terminal-kit to send / read data over a very simple telnet server. Writing works fine, it clears the screen and greets who's connected with a red "Welcome". Input events, on the other side, don't seem to work. This is my sample code:

const tkit = require('terminal-kit');
const net  = require('net');

const server = net.createServer(socket => {
    const term = tkit.createTerminal({
        stdin:  socket,
        stdout: socket,
    });

    term.clear();
    term.red('Welcome!');

    term.on('key', key => {
        console.log(key);
    });

    // THIS WORKS
    // socket.on('data', data => {
    //     console.log(data);
    // });
});

// Listen on port 8888
server.listen(8888);
pistacchio commented 7 years ago

I just missed term.grabInput(true); I live the issue here, in case someone wants to try something similar :)

cronvel commented 7 years ago

@pistacchio Fine, tell me if you manage to build some great tools ;)

Off topic: I have looked at Dedalus, I'm curious to see if there is some game somewhere using that engine?

I have started a similar project few month ago: Spellcast. It is a more generalist scripting language with a completely different approach, that can do CYOA, Visual Novel, text-based RPG, or anything needing a scenario engine.

pistacchio commented 7 years ago

@cronvel Hi! There have been some works in the last few IFComp written in Dedalus. I haven't maintained it any further (still I think it has some potential), but I worked on another system, Steller. I haven't documented it yet, but this is the way you write a story: https://github.com/pistacchio/steller.js/blob/master/example/story.js

I'm quite satisfied with the result, it is fully tested and stable and, I think, powerful. While working in it, I found it quite annoying to write all the structure in JSON. I started working on a new file format that could give the power of ES6 objects (Javascript functions, getters, setters and so on) with the simplicity of YAML and called it Jaml. I haven't released it yet, but, since I've found out about it days ago, it really reminds me of KFG, which is amazing and I might as well ditch it all together and use KFG. What is missing, for my purposes, is a tight integration of code within data, just like ES6 objects allow to do.

Since I wanted to use it to write interactive fictions, here is a typical example: every room has a description. I expect it to be a string. It can be something simple like

...
myroom: {
    description: "A large room with red walls",
    ...
}

but maybe it's all black for some reason or the description is dynamic and based on some variables:

...
myroom: {
    get description()  {
        if (story.heroIsBlind) return "It's pitch black";
        return `A large room with red walls. You see ${story.numberOfStatues} statues`;
    },
    ...
}

Getters solve the problem nicely and you can still always ask the room.description.

cronvel commented 7 years ago

@pistacchio Hi! If you are interested, I can do A Cloak of Darkness in Spellcast Scripting, once I got some time for that. It might interest you!

KFG supports tags for the code part. The system using it have to describe them and turn them into actual code.

Spellcast Scripting has defined many many tags, and is a complete programming language dedicated to scenario. I already coded some complex RPG system with that.

Steller looks promising too, a bit more dev-oriented compared to Dedalus. Would like to see that in action! ;)

Rush commented 11 months ago

Thanks for the tips in this thread but the example doesn't put the remote telnet terminal in interactive mode, at least not on terminal-kit 2.4.0. Any tips why?

const tkit = require('terminal-kit');
const net  = require('net');

const server = net.createServer(socket => {
    const term = tkit.createTerminal({
        stdin:  socket,
        stdout: socket,
    });

    term.clear();
    term.red('Welcome!');

    term.on('key', key => {
        console.log(key);
    });

    term.grabInput(true);

    // THIS WORKS
    // socket.on('data', data => {
    //     console.log(data);
    // });
});

// Listen on port 8888
server.listen(8888);

I want to build a CLI. Or should I set the terminal to interactive mode myself?