stuartlangridge / gnome-shell-clock-override

Override the Gnome Shell clock with a new time format or text of your choice. Works with new versions of Shell such as 3.18
MIT License
90 stars 34 forks source link

Fuzzy clock #6

Open Cj-Malone opened 7 years ago

Cj-Malone commented 7 years ago

Hey, I wanted a fuzzy clock in Gnome, but I couldn't find any documentation for extensions and I ended up just altering this extension to show a fuzzy clock.

If you don't know its just a clock rounded to the 5min mark, shown in full words not numbers. It looks like this: Fuzzy clock

Here is my patch, it's not ready to be merged as it now ignores all preferences (if true then fuzzy), but here is the code. If you want to use it you'd have to add a preference and the UI, but I don't know how to do that.

diff --git a/extension.js b/extension.js
index d9b34a9..8b63426 100644
--- a/extension.js
+++ b/extension.js
@@ -7,6 +7,31 @@ const Convenience = Me.imports.convenience;

 var settings;

+var HOURS = [ "ONE",
+    "TWO",
+    "THREE",
+    "FOUR",
+    "FIVE",
+    "SIX",
+    "SEVEN",
+    "EIGHT",
+    "NINE",
+    "TEN",
+    "ELEVEN",
+    "TWELVE" ];
+var FUZZY_RULES = [ "%s O'CLOCK",
+    "FIVE past %s",
+    "TEN past %s",
+    "QUARTER past %s",
+    "TWENTY past %s",
+    "TWENTY FIVE past %s",
+    "HALF past %s",
+    "TWENTY FIVE to %s",
+    "TWENTY to %s",
+    "QUARTER to %s",
+    "TEN to %s",
+    "FIVE to %s" ];
+
 function init() {
     settings = Convenience.getSettings();
 }
@@ -51,31 +76,48 @@ function overrider(lbl) {
     var t = lbl.get_text();
     var FORMAT = settings.get_string("override-string");
     var desired = FORMAT;
-    if (FORMAT.indexOf("%") > -1) {
-        var now = GLib.DateTime.new_now_local();
-        if (FORMAT.indexOf("%f") > -1) {
-            var hour = now.get_hour();
-            // convert from 0-23 to 1-12
-            if (hour > 12) {
-                hour -= 12;
-            }
-            if (hour == 0) {
-                hour = 12;
-            }
-            var clockFaceCodePoint = 0x1f550 + (hour - 1);
-            var minute = now.get_minute();
-            if (minute >= 30) {
-                clockFaceCodePoint += 12;
-            }
-            var repl;
-            if (String.fromCodePoint) {
-                repl = String.fromCodePoint(clockFaceCodePoint)
-            } else {
-                repl = fromCodePoint(clockFaceCodePoint);
+    var now = GLib.DateTime.new_now_local();
+    if (true) {
+        var hour = now.get_hour();
+        if (hour > 12) {
+            hour -= 12;
+        }
+        hour -= 1;
+
+        var minute = now.get_minute();
+        minute = Math.floor((minute + 2) / 5);
+
+        if (minute > 6) {
+            hour += 1;
+        }
+
+        desired = FUZZY_RULES[minute].format(HOURS[hour]);
+    } else {
+        if (FORMAT.indexOf("%") > -1) {
+            if (FORMAT.indexOf("%f") > -1) {
+                var hour = now.get_hour();
+                // convert from 0-23 to 1-12
+                if (hour > 12) {
+                    hour -= 12;
+                }
+                if (hour == 0) {
+                    hour = 12;
+                }
+                var clockFaceCodePoint = 0x1f550 + (hour - 1);
+                var minute = now.get_minute();
+                if (minute >= 30) {
+                    clockFaceCodePoint += 12;
+                }
+                var repl;
+                if (String.fromCodePoint) {
+                    repl = String.fromCodePoint(clockFaceCodePoint)
+                } else {
+                    repl = fromCodePoint(clockFaceCodePoint);
+                }
+                desired = desired.replace("%f", repl);
             }
-            desired = desired.replace("%f", repl);
+            desired = now.format(desired);
         }
-        desired = now.format(desired);
     }
     if (t != desired) {
         last = t;
diff --git a/metadata.json b/metadata.json
index f838e7f..f5e35f1 100644
--- a/metadata.json
+++ b/metadata.json
@@ -3,7 +3,8 @@
   "description": "Change what the clock displays",
   "shell-version": [
     "3.16",
-    "3.18"
+    "3.18",
+    "3.24"
   ],
   "settings-schema": "org.gnome.shell.extensions.clock-override",
   "url": "https://github.com/stuartlangridge/gnome-shell-clock-override",
da2x commented 6 years ago

Hi, this would be useful. Thank you for working on it. I’ll accept this if you rebase this and add localization support. Please also use the pull request functionality so we can discuss any issues through code review. Thanks you!