Konsole/Terminal Clear screen when end of screen .. [Solved]

Discussions Regarding Software

Moderator: Moderators

User avatar
linuxfluesterer
Old Dear Hen
Posts: 873
Joined: Thu Sep 20, 2012 19:47
Location: Germany

Konsole/Terminal Clear screen when end of screen .. [Solved]

Post by linuxfluesterer » Sun Mar 04, 2018 20:26

Hallo guys.
A simple question.
Do you know, there is a Linux command or a parameter to a Linux command for xterm (Terminal) or Konsole, which displays lines from top to down and when the end of the screen is reached, the screen will be cleared before the next line will be displayed? E.g.
1.
2.
3.
4.
...
25.
-----clear screen---
26.
27.

Is it also possible to slow down displaying every text line? Is it possible to slow down printing lines and clearing screen at the bottom of screen?

Thank you in advance.

-Linuxfluesterer (I love KDE...)
Last edited by linuxfluesterer on Wed Mar 07, 2018 14:46, edited 1 time in total.
Take away Facebook from me and let there be real people again...

User avatar
sabayonino
Sagely Hen
Posts: 3339
Joined: Sun Sep 21, 2008 1:12
Location: Italy
Contact:

Re: Konsole/Terminal Clear screen when end of screen reached

Post by sabayonino » Mon Mar 05, 2018 0:49

"less" and "more" commands can help you to scroll the output

and CTRL+l to clean the screen (or clear command)

Code: Select all

less /var/log/Xorg.0.log 

Code: Select all

more /var/log/Xorg.0.log 

User avatar
linuxfluesterer
Old Dear Hen
Posts: 873
Joined: Thu Sep 20, 2012 19:47
Location: Germany

Re: Konsole/Terminal Clear screen when end of screen reached

Post by linuxfluesterer » Mon Mar 05, 2018 12:26

sabayonino wrote:"less" and "more" commands can help you to scroll the output

and CTRL+l to clean the screen (or clear command)

Code: Select all

less /var/log/Xorg.0.log 

Code: Select all

more /var/log/Xorg.0.log 
Thank you for reply, Sabayonino.
But the point is, I want to have displayed the text lines until end of screen and then automatically followed by a cleared screen, followed by the next lines. I will not press any key by myself to clear the screen or receive the next lines. Just simply top to down, then a clear screen and beginning the next lines from top again.

Like on a system board on a console of a star ship. Later I would like to realize this in a graphical environment to display my own system messages or similar. Maybe this could be realized with qt/qml to create a plasmoid?
But first I'm ok with a solution in xterm and / or Konsole.

-Linuxfluesterer (I love KDE...)
Take away Facebook from me and let there be real people again...

flexoron
Simple Hen
Posts: 56
Joined: Tue Sep 26, 2017 23:17

Re: Konsole/Terminal Clear screen when end of screen reached

Post by flexoron » Mon Mar 05, 2018 22:08

Code: Select all

ex tstpager <<'EX'
i
#!/bin/bash
xlines=`tput lines`
tail -f tstfile | awk '
BEGIN { system("clear"); }
{
  if (++cl < '$xlines') { print $0; system("sleep 1"); }
  else { system("sleep 3"); system("clear"); print $0; cl=1; system("sleep 1"); }
}'
.
w
EX

touch tstfile
chmod 700 tstpager
./tstpager
In another shell:

Code: Select all

for i in  {1..50}; do echo $i >> tstfile; done
If you want resize the terminal then you have to restart
or enhance awk with signal-handling (SIGWINCH)
or use perl.

User avatar
linuxfluesterer
Old Dear Hen
Posts: 873
Joined: Thu Sep 20, 2012 19:47
Location: Germany

Re: Konsole/Terminal Clear screen when end of screen reached

Post by linuxfluesterer » Wed Mar 07, 2018 14:45

Hallo Flexoron.
Sorry for late reply. I was busy a little. So then, thank you for your suggestion. I tried it with a file containing the following lines:

Code: Select all

#!/bin/bash
xlines=`tput lines`
tail -f tstfile | awk '
BEGIN { system("clear"); }
{
  if (++cl < '$xlines') { print $0; system("sleep 1"); }
  else { system("sleep 3"); system("clear"); print $0; cl=1; system("sleep 1"); }
}'
But the execution stopped to display lines after about 6 lines. And I don't understand the ex tstpager <<'EX' expression.

So, I had luck, that in another forum (superuser) I received the following code, which is useful and seems correct:

Code: Select all

#!/bin/bash

c=0
while IFS= read -r line; do
    echo "$line"
    (( c % $(tput lines) == 0)) && clear
    sleep .1       # individual delay
    ((c++))
done < /home/linuxfluesterer/my_systemprotocol.txt
Honestly I can rather follow the second code better than yours. However, thank you again. I will mark this topic as Solved now.

-Linuxfluesterer (I love KDE ...)
Take away Facebook from me and let there be real people again...

flexoron
Simple Hen
Posts: 56
Joined: Tue Sep 26, 2017 23:17

Re: Konsole/Terminal Clear screen when end of screen .. [Sol

Post by flexoron » Wed Mar 07, 2018 22:17

NOT SOLVED

Hi,
No problem.
The ex call is just for generating the script.

Maybe your 'tstfile' has 6 lines only. So the output stops and waits for more.
With this command you feed the tstfile:

Code: Select all

for i in  {1..50}; do echo $i >> tstfile; done
and the tstpager, too.

I must say that your 'superuser' solution does not show all messages:

Why?...Because
c=0 and after echo "$line" you immediately clear the screen:
0%$(tput lines) == 0 && clear

This happens every time when the clear-screen refresh cycle starts again.
You always lose a message in between, I mean you don't see it on screen.

Hint: Do this to see the first and other messages but then the script
overflows the screen(the round robin sequence is imprecise).

(( c % $(tput lines) == 0)) && clear
echo "$line"

Btw Resizing the window does mess up the output. Seems a good idea but it doesn't work.
Good luck!
Last edited by flexoron on Wed Mar 14, 2018 1:32, edited 1 time in total.

flexoron
Simple Hen
Posts: 56
Joined: Tue Sep 26, 2017 23:17

Re: Konsole/Terminal Clear screen when end of screen .. [Sol

Post by flexoron » Thu Mar 08, 2018 23:16

Hi again
Here is a script which does what you specified in the beginning:
possible to slow down displaying every text line
possible to slow down printing lines and clearing screen at the bottom of screen

Code: Select all

#!/bin/bash
for i in "[email protected]" ; do
case $i in
    -cm=*) cmd="${i#*=}";   shift ;;
    -s1=*) sslp1="${i#*=}"; shift ;;
    -s2=*) sslp2="${i#*=}"; shift ;;
esac
done
eval ${cmd:-'journalctl -b'} | awk -v cl=`tput cols` -v ln=`tput lines` -v slp1="${sslp1:-1}" -v slp2="${sslp2:-3}" '
BEGIN { fmt="%-."cl"s\n"; system("clear"); }
{
  if (++cl < ln) { printf fmt,$0; system("sleep "slp1""); }
  else { system("sleep "slp2""); system("clear"); printf fmt,$0; cl=1; system("sleep "slp1""); }
}'
sorry, guests, I see this when viewing as a guest and no javascripts allowed...pls allow browser js-scripts when viewing code
for i in [email protected]; do

but this is the right code (without the backslash)
for i in "$\@" ; do


Lets call it pagertest.
The default is printing "journalctl -b" with 1 second delay and 3 seconds before clearing the screen.
Try:

Code: Select all

# ./pagertest
Now with another command and different delays:

Code: Select all

# ./pagertest -cm="cat /home/linuxfluesterer/my_systemprotocol.txt " -s1=2 -s2=4
Commands must write text to stdout!
Examples:

Code: Select all

# ./pagertest -cm="journalctl -f" -s1=1 -s2=1                          # follow the system-log
# ./pagertest -cm="tail -f /var/log/mylogfile.log" -s1=1 -s2=1 # follow my logfile
If you want resizing the window you have to stop and restart:

Code: Select all

# Ctrl Z
# kill %1
# resize window
# ./pagertest
Last edited by flexoron on Wed Mar 14, 2018 1:39, edited 2 times in total.

flexoron
Simple Hen
Posts: 56
Joined: Tue Sep 26, 2017 23:17

Re: Konsole/Terminal Clear screen when end of screen .. [Sol

Post by flexoron » Mon Mar 12, 2018 3:09

I think it is worth the effort providing correct solutions for Sabayon users.

This *superuser* forum you mention does not enjoy an outstanding reputation.
The opposite is true. Professionals laugh out loud and decline acceptance.

Just promoting snippets without rectification is kinda script kiddy behavior.

You should not flag something [Solved] until it is really solved!

User avatar
linuxfluesterer
Old Dear Hen
Posts: 873
Joined: Thu Sep 20, 2012 19:47
Location: Germany

Re: Terminal Clear screen when end of screen [Partly solved]

Post by linuxfluesterer » Mon Mar 12, 2018 12:54

flexoron wrote:I think it is worth the effort providing correct solutions for Sabayon users.

This *superuser* forum you mention does not enjoy an outstanding reputation.
The opposite is true. Professionals laugh out loud and decline acceptance.

Just promoting snippets without rectification is kinda script kiddy behavior.

You should not flag something [Solved] until it is really solved!
You are right. I should not flag something as solved, before it is really solved. I agree.
But, sorry to say, your first solution just works for me with the following behavior:
I generated a testfile tstfile with numers from 1 to 200: Then I start the following batch:

Code: Select all

#!/bin/bash
tstfile=/home/linuxfluesterer/tstfile
xlines=`tput lines`
tail -f $tstfile | awk '
BEGIN { system("clear"); }
{
  if (++cl < '$xlines') { print $0; system("sleep .1"); }
  else { system("sleep .3"); system("clear"); print $0; cl=1; system("sleep .1"); }
}'

exit
The result of your code leads into a cleared screen followed by the last 10 lines (191 .. 200). And then the batch file is waiting to be interrupted with CTRL-C.

Code: Select all

191
192
193
194
195
196
197
198
199
200
Sorry, but this is not what I wanted. Instead my solution, changed at one position is rather that what I call a solution for my problem. Here again the code, with the changed line:

Code: Select all

#!/bin/bash
inputfile=/home/linuxfluesterer/tstfile
clear
c=1           # I changed the value from 0 to 1
while IFS= read -r line; do
    echo "$line"
    (( c % $(tput lines) == 0)) && sleep .5 && clear 
    sleep .1
    ((c++))
done < $inputfile

exit
My result begins with a cleared screen, then followed by the first 42 lines (1 too much and because of one too much all lines are moved up for one line)
then cleared screen again, followed by the numbers 43 to (84, one too much again),
then cleared screen, followed by 85 to 126 (one too much again),
then cleared screen, followed by 127 to 168
then cleared screen, followed by the lines 169 to 200 in middle of screen.

Your second solution seems much more interested, though very, very complicated! I could try it today, and now, when I let it run, it seems to work though, how can I interrupt the output, the batch itself? Ctrl-C doesn't stop, Ctrl-Z doesn't stop also. With Ctrl-C I see my prompt and then the output is continuing. And: I would like to print out any textfile also this way.

As I said before, you are right, the superuser way is not correctly solved. So for now, I will mark it as [partly solved].

Thank you for your support, really.

-Linuxfluesterer (I love KDE...)
Take away Facebook from me and let there be real people again...

flexoron
Simple Hen
Posts: 56
Joined: Tue Sep 26, 2017 23:17

Re: Konsole/Terminal Clear screen when end of screen .. [Sol

Post by flexoron » Mon Mar 12, 2018 22:11

The second solution is the same as the first but just parametrized:

Parameter 1: -cm="a command writing to stdout"
Parameter 2: -s1=1 sleep time after a line has been printed
Parameter 3: -s2=2 sleep time before the screen gets cleared

Also Ive changed the length of the lines so it fits exactly inside the window without word-wrap.

My first solution uses hard-coded "tail -f tstfile".
Thats why you see the last lines printed and then waiting for more(for example: echo 201 >> tstfile).

The second solution takes a command so you don't have to change the script whenever you want different 'input' processed.

Examples:

Code: Select all

./pagertest -cm="cat /home/linuxfluesterer/tstfile" -s1=1 -s2=3
./pagertest -cm="cat anytextfile" -s1=1 -s2=3
./pagertest -cm="ps ax" -s1=1 -s2=1 
To stop the process you have to put it into the background(job-control) and kill it:

Code: Select all

# Ctrl-Z
# kill %1
do list 'jobs' if you have background processes already running

Code: Select all

# Ctrl-Z
# jobs
# kill %job-number
Last edited by flexoron on Wed Mar 14, 2018 1:40, edited 2 times in total.

Post Reply