Yad Clock script

Forum Forums General Software Yad Clock script

Tagged: 

  • This topic has 18 replies, 5 voices, and was last updated Jun 21-8:19 pm by Brian Masinick.
Viewing 4 posts - 16 through 19 (of 19 total)
  • Author
    Posts
  • #84898
    Moderator
    Brian Masinick
      Helpful
      Up
      0
      ::

      For what it’s worth, in post #84602 I added the attachment xclock.txt, which has correct “punctuation” for the quoted strings.
      You can, if you wish, download this one liner, rename it to xclock.bash, then chmod +x xclock.bash and it will execute.
      Alternatively you can use the short function that my friend Dave wrote that’s almost identical in features to a similar bash script that I wrote, and functionally equivalent to the xclock tool with the digital arguments. They all work, they are fast.

      One other note: you can of course, if you haven’t already done so, set up Icewm so that it automatically displays a clock on the taskbar; I’ve been writing all of these clock scripts simply to practice code script writing (one of my hobbies). I take a simple idea and try to implement it in as many tool languages as I can, and that’s how I learn the basics of additional languages. No, I’m DEFINITELY NOT an expert in any language; I’m probably at my best in Bash at this point; earlier in my career I did program in Fortran, PL/1, C, and I wrote lots of scripts in shell, either sh, ksh, or bash. I’m only doing this stuff as a hobby, also to help so that I don’t forget it all!

      --
      Brian Masinick

      #84902
      Moderator
      Brian Masinick
        Helpful
        Up
        0
        ::

        Here’s another Yad clock solution that works perfectly:

        #!/bin/sh
        while :
        do
          date +"%a %d %b %r"
          sleep 1
        done  | { trap "pkill -f $0" EXIT ; yad --title=clock --geometry=230x50+420+16 --form --cycle-read --field=": " --no-buttons --undecorated  ;}
        

        --
        Brian Masinick

        #84962
        Moderator
        Brian Masinick
          Helpful
          Up
          0
          ::

          When I was first looking for templates and/or scripts for my “clock writing exercises”, I created a few posts on different forums to find out ways to write some fairly short, simple digital clock scripts.

          On the Puppy Linux forum, I received all kinds of suggestions.
          One of our “friends” , misco_2083 even wrote a C program that can do some pretty cool stuff.
          I modified that one to suit my simple needs; I’ll provide the source code of my version below.
          I will also warn and advise you that in order to use it you will probably have to install quite a few packages that compromise the lean efficiency and the init systems we use; I did compile it elsewhere, copy it here and it “works” (in quotes) but spits out messages because it’s missing those compromising packages.

          Our Puppy Linux friends had many other helpful suggestions too. If any of you share my curiousity in coding with Yad, Bash, Python, Conky and other tools, you may want to scan the link I provide in the source code; the C code is in this thread, but in the 2-3 pages of comments there are many other very good ideas too.

          /* sclock.c */
          /* Provided on https://forum.puppylinux.com/viewtopic.php?p=59787#p59787 */
          /* by misco_2083 */
          /* requires libgtk-3-dev on Debian, pulls in a lot of extra packages */
          /* if you use it on antiX */
          /* commented and adapted by Brian Masinick */
          
          #include <stdio.h>
          #include <time.h>
          #include <gtk/gtk.h>
          
          static gboolean
          on_timeout (gpointer user_data)
          {
            GtkLabel *label = GTK_LABEL (user_data);
            time_t timer;
            char buff[12];
            struct tm* tm_info;
          
            time (&timer);
            tm_info = localtime(&timer);
          
            strftime(buff, 12, "%r", tm_info);
          
            gchar *markup = g_strdup_printf ("<b><span font='Sans Bold 16' foreground='green'>%s</span></b>", buff); 
            gtk_label_set_markup (GTK_LABEL (label), markup);
          
            g_free (markup);
          
            return G_SOURCE_CONTINUE;
          }
          
          void destroy_handler (GtkApplication* app, gpointer data)
          {
              g_application_quit(G_APPLICATION (data));
          }
          
          static gboolean
          key_handler (GtkWidget *w, GdkEventKey *ev, gpointer data)
          {
            GtkApplication *app = GTK_APPLICATION (data);
            switch (ev->keyval)
              {
              case GDK_KEY_Escape:
                  destroy_handler(NULL, app);
                  return TRUE;
              }
            return FALSE;
          }
          
          static gboolean
          button_handler (GtkWidget *w, GdkEventButton *ev, gpointer data)
          {
            if (ev->button == 1)
              {
                gtk_window_begin_move_drag(GTK_WINDOW(w),
                    ev->button,
                    ev->x_root,
                    ev->y_root,
                    ev->time);
                return TRUE;
              }
            if (ev->button == 3)
              {
                GtkApplication *app = GTK_APPLICATION (data);
                GtkWidget *item, *popup_menu;
          
                popup_menu = gtk_menu_new ();
                gtk_menu_set_reserve_toggle_size (GTK_MENU (popup_menu), FALSE);
                item = gtk_menu_item_new_with_label ("Exit");
                gtk_widget_show (item);
                gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), item);
                g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (destroy_handler), app);
                gtk_menu_popup_at_pointer (GTK_MENU (popup_menu), NULL);
                return TRUE;
              }
            return FALSE;
          }
          
          static void
          activate (GtkApplication *app,
                    gpointer        user_data)
          {
            GtkWidget           *window;
            GtkWidget           *grid;
            GtkWidget           *label;
            GtkStyleProvider    *style_provider;
            char                *css_text;
          
            window = gtk_application_window_new (app);
            gtk_window_set_title (GTK_WINDOW (window), "Digital Clock");
            gtk_window_set_default_size (GTK_WINDOW (window), 220, 20);
            gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
            gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
            gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
          
            grid = gtk_grid_new ();
            gtk_grid_set_column_homogeneous (GTK_GRID (grid),
                                          TRUE);
            gtk_grid_set_row_homogeneous (GTK_GRID (grid),
                                          FALSE);
            gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
          
            label = gtk_label_new(NULL);
            g_timeout_add (1000, on_timeout, label);
          
            gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
          
            gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
          
            gtk_container_add (GTK_CONTAINER (window), grid);
            gtk_container_set_border_width (GTK_CONTAINER(window),5);
          
            style_provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
            gtk_style_context_add_provider (gtk_widget_get_style_context (window),
                                            style_provider,
                                            GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
          
            css_text = g_strdup_printf ("window,\n"
                                        "window\n"
                                        "{\n"
                                        "  background: black;\n"
                                        "}\n");
          
            gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (style_provider),
                                             css_text, -1, NULL);
            g_free (css_text);
          
            g_signal_connect (G_OBJECT(window), "destroy",
                              G_CALLBACK (destroy_handler), app);
            g_signal_connect (G_OBJECT (window), "key-press-event",
                              G_CALLBACK (key_handler), app);
            g_signal_connect (G_OBJECT (window), "button-press-event",
                              G_CALLBACK (button_handler), app);
            gtk_widget_show_all (window);
          }
          
          int
          main (int argc,
                char **argv)
          {
            GtkApplication *app;
            int status;
          
            app = gtk_application_new ("org.gtk.simple_time",
                                       G_APPLICATION_FLAGS_NONE);
            g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
            status = g_application_run (G_APPLICATION (app), argc, argv);
            g_object_unref (app);
          
            return status;
          }
          

          --
          Brian Masinick

          #84970
          Moderator
          Brian Masinick
            Helpful
            Up
            0
            ::

            Here’s a different spin on the YadClock that looks pretty decent, can easily change color, size, font, etc.

            #!/bin/sh
            urxvt -title Clock -font 9x15bold +scrollBar -bg black -fg yellow -underlineColor yellow -geometry 25x1 -e bash -c '
            while : ; do
              echo -en $(date +"%b %d, %Y  %r")" \r"
              sleep 1
            done' | { yad --title="clock" --geometry=220x40+420+16 --form --cycle-read --field=": " --no-buttons --undecorated ; pkill -f "$0"  
            ;}
            

            --
            Brian Masinick

          Viewing 4 posts - 16 through 19 (of 19 total)
          • You must be logged in to reply to this topic.