Score:0

Horizontally aligned terminal output

ly flag

I have a program that has the following outputs:

Pack 1:  
TimeStamp : 1001302  
Data : [1 21 343 2 34 ]  
Found : Yes  
Pack 2:  
TimeStamp : 1001303  
Data : [1 21 344 2 35 ]  
Found : Yes  
Pack 3:  
TimeStamp : 1001304  
Data : [1 21 346 2 36 ]  
Found : Yes  

and so on ... repeating

The outputs to the terminal are created using c++ fprintf with "\n", I need my terminal to show the Packs position fixed in place ( on the same row every time a new pack is printed) while the data inside the brackets are changing.

I don't mind having multiple output packs on the terminal at the same time, but I need them to be fixed in place and not jumping up and down the rows so I can read them while running the program.

Is there a way to ensure this in the terminal ?

I tried the following solutions:

  1. slowing down the print to terminal: ./out | watch -n 1 cat

This doesn't solve the problem. What I want is not slower streaming of data on my screen I don't want it to move up and down the lines on the terminal. this makes it move but at a slower rate.

  1. using grep to match a flag and print N lines after: ./out | grep -AN flag

This doesn't solve the problem too, my guess is the at it appends the lines from the bottom of the terminal, and pushes the text up and does that one line at a time too. and what you endup seeing is a scrample on screen.

Thanks!

Bodo avatar
pt flag
Please [edit] your question to add information, don't use comments for this purpose. You could implement a `curses` application or use escape sequences for cursor positioning. (The latter might be non-portable.)
Waleed avatar
ly flag
fixed it, thanks.
Bodo avatar
pt flag
You should add why the solutions don't solve your problem.
Artur Meinild avatar
vn flag
Why don't you output to a text file instead, so you can inspect the output in a way that you see fit? Or modify the application to clear the screen before each output, then only 1 pack will be displayed on screen.
cn flag
Use `sed` or a similar tool to prepend to the word "Pack" the escape sequences that move the cursor home and clear the display (`\e[H\e[J`).
Waleed avatar
ly flag
@egmont this kinda worked. But sometimes it doesn't work if there are a lot of lines at high frequency it messes up the message in the middle as if it pastes new data in the middle of the previous update. what I tried is ```/out | sed $'s/Pack/\e[J\e[H\\n&\e/'``` which translates into ```/out | sed $'s/[what to 'cut']/[what to put in its place]/'``` now `&` is what you cut .. the other codes are found here in the ANSI escape code found here [ANSI Escape Codes](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797).
cn flag
Are you sure the original output is intact? If you redirect it to a file and then perform the `sed` on it, is the result broken? You can run under `script`, with or without that `sed` in place, and examine the `typescript` log files to see whether they're broken. Maybe your app runs in parallel and the threads / processes collide? Note that piping the output changes the default stdout buffering strategy from line buffered to 4k (or 8k? something like that) buffering. This could very well be the cause of this behavior.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.