dbordak/telephone-line

Any interest in creating a hud segment?

Closed this issue · 5 comments

The following hud-segment is stolen from https://github.com/milkypostman/powerline/blob/master/powerline.el#L268. Would it be possible to adapt it for telephone-line?

(defun pl/make-xpm (name color1 color2 data)
  "Return an XPM image with NAME using COLOR1 for enabled and COLOR2 for disabled bits specified in DATA."
  (when window-system
    (create-image
     (concat
      (format "/* XPM */
static char * %s[] = {
\"%i %i 2 1\",
\". c %s\",
\"  c %s\",
"
              (downcase (replace-regexp-in-string " " "_" name))
              (length (car data))
              (length data)
              (or (pl/hex-color color1) "None")
              (or (pl/hex-color color2) "None"))
      (let ((len  (length data))
            (idx  0))
        (apply 'concat
               (mapcar #'(lambda (dl)
                           (setq idx (+ idx 1))
                           (concat
                            "\""
                            (concat
                             (mapcar #'(lambda (d)
                                         (if (eq d 0)
                                             (string-to-char " ")
                                           (string-to-char ".")))
                                     dl))
                            (if (eq idx len)
                                "\"};"
                              "\",\n")))
                       data))))
     'xpm t :ascent 'center)))

(defun pl/percent-xpm
  (height pmax pmin winend winstart width color1 color2)
  "Generate percentage xpm of HEIGHT for PMAX to PMIN given WINEND and WINSTART with WIDTH and COLOR1 and COLOR2."
  (let* ((height- (1- height))
         (fillstart (round (* height- (/ (float winstart) (float pmax)))))
         (fillend (round (* height- (/ (float winend) (float pmax)))))
         (data nil)
         (i 0))
    (while (< i height)
      (setq data (cons
                  (if (and (<= fillstart i)
                           (<= i fillend))
                      (append (make-list width 1))
                    (append (make-list width 0)))
                  data))
      (setq i (+ i 1)))
    (pl/make-xpm "percent" color1 color2 (reverse data))))

(pl/memoize 'pl/percent-xpm)

;;;###autoload
(defun powerline-hud (face1 face2 &optional width)
  "Return an XPM of relative buffer location using FACE1 and FACE2 of optional WIDTH."
  (unless width (setq width 2))
  (let ((color1 (if face1 (face-background face1) "None"))
        (color2 (if face2 (face-background face2) "None"))
        (height (or powerline-height (frame-char-height)))
        pmax
        pmin
        (ws (window-start))
        (we (window-end)))
    (save-restriction
      (widen)
      (setq pmax (point-max))
      (setq pmin (point-min)))
    (pl/percent-xpm height pmax pmin we ws
(* (frame-char-width) width) color1 color2)))

Just added this as telephone-line-hud-segment (it's built on top of a separator object, so it should also work as a separator using telephone-line-hud if you want... but that's kinda silly :P)

Oh right, do keep in mind, though, that it'll negatively affect scrolling performance. Normally, separators are initialized on (or near) startup (the first time they're rendered, they're saved into a cache) -- but since this one shows a whole ton of different possible images, each one is added to the cache the first time you, for example, scroll 50% down a buffer on which you can see 20% of the screen. It's all still cached, but it'll be slower the first time you scroll through a file after a cold start.

(it'll also be significantly more demanding than anything else in terms of memory due to said cache; it's O(n^3), but n in this case is the vertical height of your font, so it is still bounded pretty low. I'll need to see if I can limit the number of entries in the hash)

Thank you for this! Just tried it out and it works like a charm!

I did not notice any degradation of scrolling performance. It was perhaps imperceptible if there was any. Also, I have nlinum and nlinum-relative installed which slow down scrolling too, so hud segment's slowdown might be masked by them.

I'll keep an eye on its memory usage and report if I notice anything abnormal.

Going to close this now; feel free to reopen if any of those potential issues turn out to be disruptive.