Displaying changing content from script in a popup window?

Hi all, :wave:

I´m wondering if anyone could help me with a certain “problem”.

I´ve written a script for a countdown timer which after starting shows me the time left.
The most basic and important parts I found somewhere on the stackexchange (if I remember correctly); I just modified and expanded them according to my purposes. :blush:

Basically it works like this:

  • I enter the stop-time
  • the script shows how many days, hours, minutes and seconds are left
  • I enter the respective values (unaltered or altered, just to my liking)

As a result the remaining time counts down to zero, which is displayed in the last line of the output within the terminal.

That´s nice and I´m basically satisfied with the script so far. :smiley:

Yet it would be an added benefit if there were the possibility of sending the last line of the output of the script (from the terminal that is) to a dedicated popup window (which i could then set to be displayed on every desktop).

That way I wouldn´t have to switch back to desktop 1 (in my case) from where I started the script to look for the time remaining. :wink:

In the past I´ve used the command notify-send -t 0 for e.g. weather messages.
But the content of the output was fixed (so no problem there) whereas in my countdown script the output changes every second. :thinking:

Now I don´t really know if it´s at all possible to use notify-send for displaying changing content (and if so how to go about configuring it).

Does anybody know if there´s a solution to the issue in question :question:

Many thanks in advance and many greetings.
Rosika :slightly_smiling_face:

BTW:

The countdown script goes like this:

#!/bin/bash

echo "Zeitdauer ausrechnen:" # calculate time-span for countdown
echo
echo "Stopp-Zeit eingeben:"  # enter stop time
echo "Beispiel: 12:33"  # just an example
read Value1 # Stopp-Zeit
Value2=$(date +%R) # present time in hrs and mins
D1=$(date -d "$Value1" '+%s'); D2=$(date -d "$Value2" '+%s') # conversion of the times in secs since 1970−01−01 00:00:00 UTC
seconds=$(($D1-$D2)) # difference in secs
TZ=UTC printf "%d days %(%H hours %M minutes %S seconds)T\n" $((seconds/86400)) $seconds # result of calculation
sleep 1
echo "Stunden"
read hour
echo "Minuten"
read min
echo "Sekunden"
read sec
        while [ $hour -ge 0 ]; do
                 while [ $min -ge 0 ]; do
                         while [ $sec -ge 0 ]; do
                                 echo -ne "$hour:$min:$sec\033[0K\r"  # that´s the (changing) part which should be displayed externally
                                 let "sec=sec-1"
                                 sleep 1
                         done
                         sec=59
                         let "min=min-1"
                 done
                 min=59
                 let "hour=hour-1"
         done

I myself never used it, but winked towards Zenity sometimes.
Maybe is this what you are looking for?

1 Like

Hi László, :wave:

thank you very much for the suggestion and for providing a link,

I´ll look into it and report back as soon as I found out something of interest.

Many greetings.
Rosika :slightly_smiling_face:

1 Like

@kovacslt:

Hi again, :wave:

I´ve been digging around for some solution for a while now and I´ve also read the Vincent Danen article but I seems despite the fact that zenity is a pretty versatile tool for many purposes it may have difficulties in displaying varying output.

On running clock in zenity menu - Unix & Linux Stack Exchange I ran into a user´s query which seems to come close to what I have in mind. :blush:

Yet, the answer he received was quite sobering:

I don’t think you can do that (a menu that shows the real time) with zenity.

and

You cannot do it with zenity alone in the general case.

But one helper went on with:

Using X11 tools you could run a clock in the title bar of your zenity dialog window.

Perhaps I could look into that possibility. :relaxed:

In the meantime: thanks a lot and many greetings.
Rosika :slightly_smiling_face:

1 Like

Sorry to read that. Zenity looked so promising…

1 Like

I know you like bash scripts, but I always found them a bit cumbersome, so here’s a Perl script that should do what you want (Perl is installed by default on any Linux system):

#!/usr/bin/perl

sub s2t { 

# turns a number in seconds into a string HH:MM:SS 

    my $s = shift;
    my $m = int($s/60);
    $s    = $s % 60;
    my $h = int($m/60);
    $m    = $m % 60;
    return "$h:".($m < 10 ? '0' : '')."$m".($s < 10 ? ':0' : ':').$s
}

print "Enter alarm time (e.g. 13:44)\n\n";

my $t = <STDIN>;
chomp($t); # remove newline at the end

die "\nfaulty entry '$t'\n" unless $t =~ /(\d\d?)\D(\d\d?)/;

my ($h,$m) = ($1,$2);

my $msg = "** Time's up! It's NOW now. **"; 
print "\n\nDo you want to display an additional message at the end?
Default is:\n\n$msg

'NOW' is replaced by the time, the timer stopped.

Just hit return to use the default, otherwise enter some text.

\tUse NOW for the time the timer stopped 
\tand THEN for when it started.\n\n";

my $custom_message = <STDIN>;

my ($sec,$current_minute, $current_hour) = ( localtime )[0,1,2];
my $then = $current_hour.':'.($current_minute < 10 ? '0' : '').$current_minute;
print "Timer started at $then\n\n";

$sec = 60*( (($current_minute <= $m) ? 
    ($m - $current_minute) :  
    (60 + $m - $current_minute) ) +
    60*(($current_hour <= $h) ? 
        ($h - $current_hour) :  
        (24 + $h - $current_hour) )) - $sec;

$| = 1; # turn on autoflush
while($sec >= 0) {
    print "\rTime to go until $t: ".s2t($sec)." ";
    sleep(1);
    $sec--;
}
$| = 0; # turn off autoflush
print "\n";

my $now = join(':',(localtime )[2,1]);

chomp $custom_message;

$custom_message and $msg = $custom_message;

$now =~ s/\D(\d)$/:0$1/;
$msg =~ s/NOW/$now/g;
$msg =~ s/THEN/$then/g;
$msg =~ s/([\n\s'\("\\])/\\$1/g;

exec "notify-send -t 0 $msg";

I had this (with exception of the notify-send) lying around, anyway.

2 Likes

Funny thing. This goes a bit more into the direction I would’ve gone to. If you really want a GUI, I would consider a scripting language like Bash underpowered for the task. It’d be easier to just use a proper general purpose programming language, where you just import a simple GUI library and write the app with that.

Sounds harder than using Bash, but if you go out of Bash’s comfortable zone, you will quickly get into very harsh and unforgiving territory. It’s honestly just better to use a “normal” general purpose programming language. Bash is so popular, simply because it’s available on all the popular Linux distributions. So, you can write one Bash script and run it anywhere. Bash is also almost always used for DevOps tasks and less for app-like tasks, like having an end-user timer.

Here is an example of a tiny GUI program written in Nim:

It has like 4 different ways of doing something in a GUI window. Yet, the whole program only took 128 lines for me:

You only need 1 timer thing. I doubt one would need more than 60 lines for that.

Again, seems complicated, but I’m pretty sure it’s easier than trying to do this in Bash.

2 Likes

Well, the actual code is nothing…

Of the 66 lines, 19 are empty, and 13 are just text strings to write on the terminal.
If you want to get rid of the ticking down countdown, you lose another 11, which leaves you with 23 lines of functional code (and it could be still much shorter, especially if you don’t want the custom messages).

Of course, adding a small GUI would be easy with Tk, but it requires installation. I thought, something that would just run out of the box (like a bash script) would be easier.

You definitely made me curious about Nim.

:partying_face: :beers: :partying_face: :confetti_ball: :dancer:

1 Like

Hi László, :wave:

don´t worry.
Thanks anyway for your help. That´s very nice of you.

Many greetings.
Rosika :slightly_smiling_face:

1 Like

Hi Mina, :wave:

Yes, indeed I like them but only because I at least have a slight chance of understanding what´s going on there. :blush:

But thanks very much for your perl script. :hearts:
I only wish I could understand some of it…

As a matter of fact I tried it out right away (that is after having read a bit about it on:
Perl › Wiki › ubuntuusers.de )

I so hope you haven´t put all that effort in creating such a wonderful piece of work just on my behalf. I would be inconsolable. :cry:

This gives me a bit of hope anyway…

Your perl script works very well, which is really great.
Here´s a snapshot of it:

As you can see I indeed got a notify-send popup, but only after the countdown finished.
So I guess it´s basically designed as a reminder as it pops up at a certain time.

That´s a fine thing indeed but I was looking for a small window which displays the time as it´s running down to zero.
So the countdown should be visible at any given time. :thinking:
Of course the value changes every second.

I´m afraid that´s hardly possible to achieve. At least I couldn´t do it anyway. :frowning_face:

But thanks so much Mina. Your help is certainly appreciated.

Many greetings from Rosika :slightly_smiling_face:

Hi @Akito, :wave:

thanks so much for your post. :hearts:

I have to admit I had never heard of Nim before.
Thanks also for the link; I am impressed indeed. :astonished:

Well, I see you´re a professional programmer and I haven´t got the slightest chance of even trying to keep up with what you have achieved.

I´ve looked around a bit for Nim and it seems that even its installation is quite difficult
(Install Nim programming language on Ubuntu 20.04 / Debian 10 - Linux Windows and android Tutorials )

But my admiration for you people here on the forum continues to rise perpetually. :slightly_smiling_face:

Many greetings.
Rosika :smiley:

That’s quite pessimistic and untrue. Just look at your post history to see all the Linux stuff you have managed to achieve.

That’s some random guide.
Here’s the official and easy way to do it:

Installation using choosenim

choosenim is an installer for the Nim programming language. It allows you to easily switch between versions of Nim, whether that is the latest stable release or the latest development version.

To install the latest stable release of Nim using choosenim, just run the following in your terminal, then follow the onscreen instructions:

curl https://nim-lang.org/choosenim/init.sh -sSf | sh

Well, it’s just my favourite tool. However, what I explained earlier, can be achieved with any general purpose programming language.

That said, for the sake of simplicity and saving time, you can just copy the source for the mini program I had written.

Then, you just remove the unnecessary stuff and all you add is a timer. That’s it. So the only thing you have to do on your own is the timer and that’s really easy.

You also cannot make up excuses, because you already successfully(!) were able to get along with bash very often, which means you will get along with this one, too. :wink:

1 Like

@Akito:

Hi once more. :wave:

You´re too kind. Thanks for the heads-up. :slightly_smiling_face:

Yet I´m realistic enough to know where I stand…

In the meantime I´ve done the following:

  • sudo apt-get install build-essential

as my Debian 10 VM lacked the GCC compiler. Then:

  • curl https://nim-lang.org/choosenim/init.sh -sSf | sh

success! :+1:

  • adding export PATH=/home/rosika2/.nimble/bin:$PATH to ~/.bashrc file

I even logged out and in again, yet curiously enough the newly added path variable still seems not to be available :thinking::

rosika2@debian ~> echo $PATH
/usr/local/bin /usr/bin /bin /usr/local/games /usr/games

Many greetings.
Rosika :slightly_smiling_face:

This should work. Try using the command manually for the current session. If that works, it should also work in the ~/.bashrc file. You could, however, also put it in /etc/bash.bashrc, which is the same thing, but is used by all users on the system.

1 Like

I should have read carefully what you’ve written in the first place …

Always a good idea.

I thought that was what you wanted. However, notify-send creates a static window, so it’s definitely not the right tool. Of course, one could set it up the script so, that it would call notify-send every minute (or second) with the -t parameter set to 60,000 or 1,000 (milliseconds) respectively so that the notification vanishes after this time, but I guess, that would be really annoying.

Otherwise, a small GUI program, as @akito suggested, would do the job, but then desktop integration would be a hassle, as every desktop is different. A KDE-widget is an easy thing to do, but completely useless for people who use other DE.

Hmm, funniest thing:

rosika2@debian ~> export PATH=/home/rosika2/.nimble/bin:$PATH
rosika2@debian ~> nim -v
Nim Compiler Version 1.4.8 [Linux: amd64]
Compiled at 2021-05-25
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 44e653a9314e1b8503f0fa4a8a34c3380b26fff3
active boot switches: -d:release

So what you suggested, works. But only for the current terminal session of course.

I´ve now placed the export command in /etc/bash.bashrc, again logged out an in, and
the new PATH variable still isn´t saved. :thinking:

Perhaps I should try ~/.profile likey they suggested. Still odd though…

Many greetings.
Rosika :slightly_smiling_face:

Hi @Mina, :wave:

Please don´t be so hard on yourself. I am very grateful for any kind of help and still want to learn something new.
So thanks anyway a lot for your help. :hearts: :slightly_smiling_face:

O.K., thanks for the confirmation.

Yes, I can imagine.

Thanks anyway for your thoughts and comments.
As always they´re highly appreciated.

Many greetings from Rosika :slightly_smiling_face:

It was really no work at all.

Still, don’t bother with learning it. Despite being present on practically every Linux system, Perl is essentially dead. It’s far more powerful than other scripting languages (bash and sorts, even tcl) and way easier to learn and to maintain than Python in order to create meaningful projects, it has completely gone out of fashion and will probably soon completely vanish into oblivion.

It might be my favourite scripting language, but that’s because I learned it when it was still the glue that holds the internet together (15+ years ago).

1 Like

Hello @Akito, :wave:

this is something that bothers me a great deal, because I don´t understand it. :frowning_face:

I´ve tried everything now:
I put export PATH=/home/rosika2/.nimble/bin:$PATH into the following files (only in one file at a time of course):

  • ~/.bashrc # didn´t work
  • /etc/bash.bashrc # didn´t work
  • ~/.profile # didn´t work.

Upon having a closer look at the ~/.profile file I realized the syntax might not be entirely correct.

The “$PATH” entry was displayed in red. That cannot be right. :frowning_face:

So taking the already existing entries as an example I put the whole definition in inverted commas. Now the entry is displayed in yellow (which seems right I guess). :slightly_smiling_face:

Here are the respective screenshots for reference:

and now:

I even rebooted the system but the new path still is ignored: :thinking:

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

I really don´t understand it.

Many greetings
Rosika :slightly_smiling_face: