[gnome-nibbles/arnaudb/command-line: 6/10] Add --level option.



commit ac91574970f58d3c8e22beb42d885bbf09547b26
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jul 8 13:49:58 2020 +0200

    Add --level option.

 data/gnome-nibbles.6    |  3 +++
 src/gnome-nibbles.vala  | 15 ++++++++++++++-
 src/nibbles-window.vala | 11 +++++++++--
 3 files changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/data/gnome-nibbles.6 b/data/gnome-nibbles.6
index 90fa606..b4ccea8 100644
--- a/data/gnome-nibbles.6
+++ b/data/gnome-nibbles.6
@@ -33,6 +33,9 @@ Disable fake bonuses; cannot be set with \-\-enable-fakes
 .B \-e, \-\-enable-fakes
 Enable fake bonuses; cannot be set with \-\-disable-fakes
 .TP
+.B \-l, \-\-level=NUMBER
+Start at given level (1-26)
+.TP
 .B \-n, \-\-nibbles=NUMBER
 Set number of nibbles (4-6)
 .TP
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index b849017..a9cea7c 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -34,6 +34,7 @@ private class Nibbles : Gtk.Application
 
     private static bool disable_fakes   = false;
     private static bool enable_fakes    = false;
+    private static int level            = int.MIN;
     private static int nibbles          = int.MIN;
     private static int players          = int.MIN;
     private static int speed            = int.MIN;
@@ -45,6 +46,12 @@ private class Nibbles : Gtk.Application
         /* Translators: command-line option description, see 'gnome-nibbles --help' */
         { "enable-fakes",   'e', OptionFlags.NONE, OptionArg.NONE,  null,           N_("Enable fake 
bonuses"),                  null },
 
+        /* Translators: command-line option description, see 'gnome-nibbles --help' */
+        { "level",          'l', OptionFlags.NONE, OptionArg.INT,   ref level,      N_("Start at given level 
(1-26)"),
+
+        /* Translators: in the command-line options description, text to indicate the user should specify 
the start level, see 'gnome-nibbles --help' */
+                                                                                    N_("NUMBER") },
+
         /* Translators: command-line option description, see 'gnome-nibbles --help' */
         { "nibbles",        'n', OptionFlags.NONE, OptionArg.INT,   ref nibbles,    N_("Set number of 
nibbles (4-6)"),
 
@@ -94,6 +101,12 @@ private class Nibbles : Gtk.Application
             return Posix.EXIT_SUCCESS;
         }
 
+        if (level   != int.MIN && (level   < 1 || level  > 26))
+        {
+            /* Translators: command-line error message, displayed for an invalid start level request; see 
'gnome-nibbles -l 0' */
+            stderr.printf (_("Start level should only be between 1 and 26.") + "\n");
+            return Posix.EXIT_FAILURE;
+        }
         if (nibbles != int.MIN && (nibbles < 4 || nibbles > 6))
         {
             /* Translators: command-line error message, displayed for an invalid number of nibbles; see 
'gnome-nibbles -n 1' */
@@ -189,7 +202,7 @@ private class Nibbles : Gtk.Application
                 settings.set_boolean ("fakes", true);
         }
 
-        window = new NibblesWindow ();
+        window = new NibblesWindow (level == int.MIN ? 0 : level);
         add_window (window);
     }
     internal bool on_f1_pressed (Gdk.ModifierType state)
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index 144fc29..f76902d 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -60,6 +60,8 @@ private class NibblesWindow : ApplicationWindow
 
     /* Game being played */
     private NibblesGame? game = null;
+    public  int cli_start_level { private get; internal construct; }
+    private int start_level { private get { return cli_start_level == 0 ? settings.get_int ("start-level") : 
cli_start_level; }}
 
     /* Used for handling the game's scores */
     private Games.Scores.Context scores_context;
@@ -89,6 +91,11 @@ private class NibblesWindow : ApplicationWindow
         { "back",           back_cb         }   // called on Escape pressed; disabled only during countdown 
(TODO pause?)
     };
 
+    internal NibblesWindow (int cli_start_level)
+    {
+        Object (cli_start_level: cli_start_level);
+    }
+
     construct
     {
         add_action_entries (menu_entries, this);
@@ -120,7 +127,7 @@ private class NibblesWindow : ApplicationWindow
         key_controller.key_pressed.connect (key_press_event_cb);
 
         /* Create game */
-        game = new NibblesGame (settings.get_int ("start-level"),
+        game = new NibblesGame (start_level,
                                 settings.get_int ("speed"),
                                 settings.get_boolean ("fakes"),
                                 NibblesView.WIDTH,
@@ -268,7 +275,7 @@ private class NibblesWindow : ApplicationWindow
 
         if (game.is_paused)
             set_pause_button_label (/* paused */ false);
-        game.reset (settings.get_int ("start-level"));
+        game.reset (start_level);
 
         view.new_level (game.current_level);
         view.connect_worm_signals ();


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