-
Notifications
You must be signed in to change notification settings - Fork 4
Piping and console editing support #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
boutell
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, first, thanks for contributing! This is the first PR, and I'm pleased to see it.
And, I see a lot of little improvements here.
However, I'm not convinced it makes sense to make a shell script wrapper a mandatory part of every execution of tome just to eliminate the need for vipe, a tool that seems well designed for its specialized job.
Is there a way to achieve this feature without the need for the shell script wrapper?
|
Also, isn't it problematic to potentially confuse actual errors with normal output by displaying interactive output on stderr? |
I've thought about this. All I can say is "sort of." You can still return errors, it just has to be dumped over stdout. You also can still control the exit codes (what really should be the indicator of a failure). The cost here is that any program relying on stderr for error reporting might not have the ability to pull errors from stdout, but I can't think of a single situation where this is true and you would allow or want UI interaction |
|
Some solutions that may enhance determinism of what I said are: 1. Redirect userspace errors:global.console = new console.Console({
stdout: process.stderr, // or fs.createWriteStream('./out.log', { flags: 'a' }) or a socket or a pipe or etc...
stderr: process.stdout // or fs.createWriteStream('./err.log', { flags: 'a' }) or a socket or a pipe or etc...
});2. Redirect uncaught and internal errors:const other_stderr_stream = // ... // file, stdout, socket, etc...
process.on('uncaughtException', (err) => {
other_stderr_stream.write(`Uncaught: ${err.stack || err}\n`);
// Optional: exit after logging
process.exit(1);
});
process.on('unhandledRejection', (reason, p) => {
other_stderr_stream.write(`Unhandled rejection at: ${p}\nReason: ${reason}\n`);
});
3. (Unverified) pollute stderr and stdoutAnother solution (I'm not sure if it would work in modern Node.js) would be to pollute process.stderr and process.stdout, but I wouldn't recommend that. |
In most cases, you need to manually instruct shells to attach a TTY in order for display to work over stderr. If you don't attach one, properties like process.stderr.rows become unavailable. There are some cases where you don't need to do this, but the majority require it, so its best to wrap it to ensure a TTY is available deterministically. I didn't write one for windows, as I'm not sure if this project intends to target that platform (I couldn't get the main branch to work on my PC), but I imagine the wrapper would be similar. EDIT: One alternative to writing shims is to instruct the user to attach the TTY to stderr if it is absent instead of shimming it automatically with a shell wrapper, but this might be clunkier than just offering the wrapper. |
|
If anything, I'm only offering up my code as a back-contribution or proof-of-concept. I'm not expecting you to merge (if you don't want to). I just want to show you what I did and what's possible with your tool. So no hard-feelings either way. I'm using this for a specific purpose that I could see you viewing as an edge-case. Even if you do, I still thought it was worth showing you what I've done and what I am doing with your work. |
This PR attempts to add a much more clever alternative to
vipevipe(and many similar solutions) use temp files for interactive handling of stdin and stdout in bash/shell scripts. This has an unnecessary footprint of relying on the file system to temporarily buffer stdin so that it can be edited before being sent back to stdout.This PR attempts to achieve the goal of vipe without that unnecessary footprint, and does so by taking advantage of the 3rd stdio stream: stderr.
Currently, tome uses stdout for rendering, which makes it un-useful for piping (piping controls stdin and stdout), but if we swap the stdout and stderr streams, we can get an interactive UI while still being able to pipe outputs in a shell script.
To test this change:
NOTE 1: Specifying a fake file such as fake.js is used effectively only for language detection during pipes
NOTE 2: When piping and pressing control+q, the editor does not autosave nor prompt to save. Saving is still possible, but must be done so manually and can only be done for existing files (no-ops otherwise when piping)