Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash non scrolling terminal output

Tags:

java

bash

Some programs produce output to bash in a way that the user experience like strings that changes values rather than rows printed(bash scrolls down to be able to show all the changing output). What is this phenomena called? How is it achieved in e.g a java program or a bash script. How can the output be parsed? Is it just printing new lines but using some special function?

top is one program that uses this "phenomena" of outputting data


Okey cursors are used.

Then how can i parse them in java? I tried this code but it does not print anything

public static void exeTest(String [] args) throws IOException{
        if (args.length <= 0) {
            System.out.println("empty command");
            return;
        }

       Process process = new ProcessBuilder(args).start();
       InputStream is = process.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);


       System.out.printf("Output of running %s is:", 
          Arrays.toString(args));
       String line;
       while ((line = br.readLine()) != null) {
            System.out.println(line);
       }

Thanks for all good answers, im now more familiar at this topic even thou I still cant parse a program output like the one from top in java. Ill close this question and do more research about how to parse this in java. Then I may start another more specific one about just parsing the output in java.

like image 755
The D Merged Avatar asked Jul 31 '13 04:07

The D Merged


People also ask

How do I stop terminal scrolling?

Luckily you can disable this behavior: Gnome terminal: *Edit -> Profile preferences -> Scrolling", uncheck Scroll on output. Terminator: Right click on terminal screen, Preferences -> Profile -> (for each profile) -> Scrolling, uncheck Scroll on output.

How do I make my Linux output scrollable?

Scrolling on the Output To do this, press the Menu button. Select Preferences. Select your current profile from the Profiles section. Then select Scrolling.

How do I scroll in terminal without a mouse?

For most users, you should be able to scroll up and down, one line at a time using Shift+UpArrow or Shift+DownArrow. To jump an entire page at a time, try Shift+PageUp or Shift+PageDown. If these commands don't work, it's likely your terminal is using different keybindings.

How to go to top of terminal in Linux?

As an aside, CTRL-ALT PgUp or PgDn will let you scroll one line at a time.


2 Answers

Instead of printing whole lines (as with println or printing %n), write backspace characters (\b) or bare carriage returns without linefeeds (\r) to move the cursor back in front of what you want to print over.

like image 190
chrylis -cautiouslyoptimistic- Avatar answered Sep 28 '22 15:09

chrylis -cautiouslyoptimistic-


Since you have tagged the question with bash, I am presuming that you are running linux/cygwin. Hence it's likely that the terminal is ANSI compatible. In that case, you could also use ANSI escape sequences...

e.g.:

echo -e "Hello world\rHi"
# Produces "Hillo world" since the line is not cleared before printing "Hi".
echo -e "Hello world\r\e[KHi"
#produces "Hi" (prints "Hello world", goes to 1st column, clears to last column, prints "Hi")
# For more details, refer above link.

If you need to run it in loop (which is the most obvious case), make sure that either

  1. You don't put a newline after the message string, OR
  2. You revert the vertical cursor movement (due to "\n") using ANSI escape sequence - "\e[A" (or "\e[<N>A", where N=number of lines.)

NOTE:

This code given above are bash commands. Sorry, I am not much familiar with java syntax, but to my knowledge,

  1. echo would get translated to System.out.println
  2. echo without last newline would be achievable with System.out.printf
  3. & \e refers to <ESC> character (ascii = 27), can be achieved with \033 in java string.
like image 22
anishsane Avatar answered Sep 28 '22 15:09

anishsane