In Bash, how do I display script output and then resume to the previous output (like less, or top, or watch) - linux

I don't want to write C code for this,
But I want my script to behave like 'watch': display output on a clean console, and eventually return to the original output that was displayed.
I'm not using watch because it messes with my output and lack functions I want to add.
Regular 'clear' will fill up my console with pages and pages of the same output and I want to avoid that.
Any suggestions?

Terminal curses software such as ncurses should work as well as making 'q' to quit like less fairly simple

Related

trick console prog to make it think it writes to terminal

Many terminal programs will behave differently depending on the STDOUT destination, either terminal or pipe or file. Usually they will remove colors. There are usually command line options for some of them to keep colors or formatting or anything else that is intended only for direct terminal output. But those options are not always present and it takes time to find them thus I need a generic way to trick the program so that it thinks that STDOUT is terminal, not a pipe. How to achieve this?
There are several tools for this, they basically create a pty for your command.
The best known is probably expect: http://expect.sf.net
Alternatively, empty: http://empty.sf.net
There are several examples in that page, have a look.
For simple cases, script -c 'mycommand' may be a viable alternative.
And tmux, which is powerful and pretty easy to script.

How is the fancy command line implemented in Linux?

When installing things in Linux I often see stdout change after printing eg. there might be a counter showing installation progress that starts at 1% and goes up to 100%. How is this done? When I write C programs and I print something using printf, I can't change it afterwards - if I type 1%, it stays that way. How is it done? Is there a different function I have to use?
\r brings you back to the beginning of the line without issuing the \n to go to the next line. Use this to overwrite text on the screen to build progress bars, etc.
See:
How to add a progress bar to a shell script?

Go to beginning of output command created - shortcut

I can scroll trough bash output using shift+pgup/pgdown.
But lets say, some command outputted lot of text, I have to pageup few times to go to beginning of output of this command.
Can I just simply do this by some shortcut? Something that simply allows me to scroll between previous commands (not history!), seeing their output.
You could try piping the output into less:
someCommand | less
less will allow you to search and scroll through the output text pretty easily.
once in less you can just type % to jump back to the top of the page. Essentially that means jump to 0% of the page. There are also a bunch of extra commands on the page I linked to above.
Another option is to use screen and use backward search (beware: read the Overview first, especially the part about the C-a prefix) to e.g. search for some specific characters in your prompt (like your username).
The scroll back history in Unix shells is a shell specific functionality, meaning that it is up to the specific shell (xterm, rxvt, text console, etc) to handle it. The functionality you request would require the shell to identify the individual program runs, to know where to scroll to. Scanning text is not technically hard per se, but as prompts and command display can differ due to user settings it can be hard to make it work generally good. Some communication between the shell and the terminal could make it better.
There sure are some nice fancy terminal programs doing things like this, to for example show syntax help when writing commands, but for your case I agree with previous answer, that piping commands to less is a good way to isolate the output. It might be a bit cumbersome first, as it requires you to think about it first, and not just go back in history, but if you learn the shell better and learn to use the command history it will probably work fine. I recommend you to, if you haven't already. What I mean is ctrl-r etc. More described for example here:
http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/

NodeJS Multiplexing Terminal?

Here's my issue. I'm outputting directly to console and also getting user input via the terminal. Sometimes, the user is typing a thing and then the text they were typing gets messed up when the console outputs something. It doesn't break the program, it's just annoying.
I looked at ncurses in the npm catalog, but it seems pretty complicated. All I want is to print stuff to the screen without disrupting user input.
Any help, and I do mean any help, is very much appreciated.
How are you reading from the terminal? If you're doing it in "raw" mode where you get input for each character (or each few characters), then when you get a character, set an "output inhibit" flag and also set a timeout that will clear the flag when the user has stopped typing for a bit. Whatever does your output needs to check the flag and hold off if it's set.
Alternatively, if the user is typing line-by-line, you could set the flag on each character and then clear it (and simply flush your output) when they enter a newline.
If the terminal is in "cooked" mode (your code doesn't see anything until the user types a newline) there's really little you can unless you bite the ncurses bullet.

Node command line interface change

I'm making a command interface for a node server, but I have reached a point that I want it to look better.
I want to have the console so you enter a command at the bottom of the terminal screen, you hit enter, and it adds the reply to the actual command line.
If you have ever run a minecraft bukkit server from the command line, you should know what I'm talking about.
Here's a picture of what I'm talking about if you still don't understand. Imagine this was in terminal, and ignore the scroll bars: http://cl.ly/1K0h1V0r0H3f3U3t3L22
Is there anyway to set the console to look like this without having to make your own program for it or having the screen reprint all the other info to fake that look?
I have not done this, but I believe something similar is possible with very little effort by using Node.js REPL. You can override the eval parameter to provide your own command processing.
It would not have the exact look you are wanting, but it will be an interactive prompt that you can utilize (more similar to a Windows command shell or a Linux shell).
If you want the exact look from your screenshot, I don't believe that there is any Node.js module that will help you. There are some that allow you to use colors in the console, and some basic highlighting (e.g. bold), but nothing that gives you complete control over the console screen.

Resources