[gnome-nibbles/arnaudb/command-line: 2/6] Add --speed option.



commit 123a0da96a97941117f1f5e9d356cf3a37b466a4
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jul 8 11:31:45 2020 +0200

    Add --speed option.

 data/gnome-nibbles.6   |  3 +++
 src/gnome-nibbles.vala | 20 +++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/data/gnome-nibbles.6 b/data/gnome-nibbles.6
index fb5e160..8c01fae 100644
--- a/data/gnome-nibbles.6
+++ b/data/gnome-nibbles.6
@@ -33,6 +33,9 @@ Set number of nibbles (4-6)
 .B \-p, \-\-players=NUMBER
 Set number of players (1-4)
 .TP
+.B \-p, \-\-speed=NUMBER
+Set worms speed (4-1: 4 for slowest and 1 for fastest)
+.TP
 .B \-v, \-\-version
 Print release version and exit
 .P
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 4cedc5f..44db603 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -34,6 +34,7 @@ private class Nibbles : Gtk.Application
 
     private static int nibbles = int.MIN;
     private static int players = int.MIN;
+    private static int speed   = int.MIN;
     private const OptionEntry[] option_entries =
     {
         /* Translators: command-line option description, see 'gnome-nibbles --help' */
@@ -48,6 +49,12 @@ private class Nibbles : Gtk.Application
         /* Translators: in the command-line options description, text to indicate the user should specify 
number of players, see 'gnome-nibbles --help' */
                                                                                     N_("NUMBER") },
 
+        /* Translators: command-line option description, see 'gnome-nibbles --help' */
+        { "speed",          's', OptionFlags.NONE, OptionArg.INT,   ref speed,      N_("Set worms speed 
(4-1, 4 for slow)"),
+
+        /* Translators: in the command-line options description, text to indicate the user should specify 
the worms speed, see 'gnome-nibbles --help' */
+                                                                                    N_("NUMBER") },
+
         /* Translators: command-line option description, see 'gnome-nibbles --help' */
         { "version",        'v', OptionFlags.NONE, OptionArg.NONE,  null,           N_("Show release 
version"),                 null },
         {}
@@ -91,6 +98,12 @@ private class Nibbles : Gtk.Application
             stderr.printf (_("There could only be between 1 and 4 players.") + "\n");
             return Posix.EXIT_FAILURE;
         }
+        if (speed   != int.MIN && (speed   < 1 || speed   > 4))
+        {
+            /* Translators: command-line error message, displayed for an invalid given worms speed; see 
'gnome-nibbles -s 5' */
+            stderr.printf (_("Speed should be between 4 (slow) and 1 (fast).") + "\n");
+            return Posix.EXIT_FAILURE;
+        }
 
         /* Activate */
         return -1;
@@ -126,7 +139,9 @@ private class Nibbles : Gtk.Application
                                                            "Menu"   });
         bool nibbles_changed = nibbles != int.MIN;
         bool players_changed = players != int.MIN;
-        if (nibbles_changed || players_changed)
+        if (nibbles_changed
+         || players_changed
+         || speed != int.MIN)
         {
             GLib.Settings settings = new GLib.Settings ("org.gnome.Nibbles");
             if (nibbles_changed && players_changed)
@@ -145,6 +160,9 @@ private class Nibbles : Gtk.Application
             }
             else // (nibbles_changed)
                 settings.set_int ("ai", nibbles - settings.get_int ("players"));
+
+            if (speed != int.MIN)
+                settings.set_int ("speed", speed);
         }
 
         window = new NibblesWindow ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]