emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] master 3f5871a 04/11: Customizable speed


From: Artur Malabarba
Subject: [elpa] master 3f5871a 04/11: Customizable speed
Date: Sat, 07 Mar 2015 20:43:02 +0000

branch: master
commit 3f5871a4f83509aed8cc0877df20ee90a2a607dc
Author: Artur Malabarba <address@hidden>
Commit: Artur Malabarba <address@hidden>

    Customizable speed
---
 spinner.el |   55 +++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/spinner.el b/spinner.el
index 721f9c2..ec6a17c 100644
--- a/spinner.el
+++ b/spinner.el
@@ -51,20 +51,15 @@ vector, the spinner itself.")
   "Spinner curently being displayed on the mode-line.")
 (make-variable-buffer-local 'spinner-current)
 
-(defun spinner-stop ()
-  "Stop the current buffer's spinner."
-  (when (timerp spinner--timer)
-    (cancel-timer spinner--timer))
-  (setq spinner--timer nil
-        spinner-current nil)
-  (setq mode-line-format
-        (remove 'spinner--mode-line-construct mode-line-format)))
+(defvar spinner--counter 0
+  "Current frame of the spinner.")
+(make-variable-buffer-local 'spinner--counter)
 
 (defconst spinner--mode-line-construct
   '((spinner-current
      (" "
       (:eval (elt spinner-current
-                  (% (cadr (current-time))
+                  (% spinner--counter
                      (length spinner-current)))))
      (spinner--timer
       (:eval (spinner-stop)))))
@@ -75,10 +70,24 @@ vector, the spinner itself.")
   "Holds the timer being used on the current buffer.")
 (make-variable-buffer-local 'spinner--timer)
 
-(defun spinner-start (&optional type)
+(defvar spinner-frames-per-second 5
+  "Default speed at which spinners spin, in frames per second.
+Applications can override this value.")
+
+
+;;; The main function
+;;;###autoload
+(defun spinner-start (&optional type fps)
   "Start a mode-line spinner of given TYPE.
-Spinners are buffer local.  Call `spinner-stop' in the same buffer
-to stop it.
+Spinners are buffer local. It is added to the mode-line in the
+buffer where `spinner-start' is called.
+
+Return value is a function which can be called anywhere to stop
+this spinner.  You can also call `spinner-stop' in the same
+buffer where the spinner was created.
+
+FPS, if given, is the number of desired frames per second.
+Default is `spinner-frames-per-second'.
 
 If TYPE is nil, use the first element of `spinner-types'.
 If TYPE is `random', use a random element of `spinner-types'.
@@ -99,6 +108,7 @@ is chosen as the spinner type."
                      spinner-types)))
          ((symbolp type) (cdr (assq type spinner-types)))
          (t (error "Unknown spinner type: %s" type))))
+  (setq spinner--counter 0)
   
   ;; Maybe add to mode-line.
   (unless (memq 'spinner--mode-line-construct mode-line-format)
@@ -113,14 +123,31 @@ is chosen as the spinner type."
     (cancel-timer spinner--timer))
   (let ((buffer (current-buffer))
         ;; Create the timer as a lex variable so it can cancel itself.
-        (timer (run-at-time t 1 #'ignore)))
+        (timer (run-at-time t
+                            (/ 1.0 (or fps spinner-frames-per-second))
+                            #'ignore)))
     (timer-set-function
      timer (lambda ()
              (if (buffer-live-p buffer)
                  (with-current-buffer buffer
+                   (setq spinner--counter (1+ spinner--counter))
                    (force-mode-line-update))
                (ignore-errors (cancel-timer timer)))))
-    (setq spinner--timer timer))) 
+    (setq spinner--timer timer)
+    ;; Return a stopping function.
+    (lambda () (when (buffer-live-p buffer)
+            (with-current-buffer buffer
+              (spinner-stop))))))
+
+(defun spinner-stop ()
+  "Stop the current buffer's spinner."
+  (when (timerp spinner--timer)
+    (cancel-timer spinner--timer))
+  (setq spinner--timer nil
+        spinner-current nil)
+  (setq mode-line-format
+        (remove 'spinner--mode-line-construct mode-line-format)))
 
 (provide 'spinner)
+
 ;;; spinner.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]