Skip to content

Instantly share code, notes, and snippets.

@swaroopch
Last active September 4, 2017 13:53
Show Gist options
  • Save swaroopch/4ff55e2cf58ca2a0bf4325c39b6e213e to your computer and use it in GitHub Desktop.
Save swaroopch/4ff55e2cf58ca2a0bf4325c39b6e213e to your computer and use it in GitHub Desktop.
Ethereum price ticker for Spacemacs mode line
;; Screenshot at https://twitter.com/swaroopch/status/903442255796633600
;; Inspired by https://www.reddit.com/r/ethtrader/comments/6wxizf/single_best_feature_of_the_new_macbook/
;; Code written for Spacemacs, will need adaptation to your Emacs setup
;; Spacemacs users: You may need to enable `spacemacs-misc` layer to ensure `request` package is installed.
(defun dotspacemacs/user-config ()
"Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."
;; Ethereum price ticker
;; Fetch
(defvar swa/ethereum-price
"(loading)"
"The current price of ETH in USD")
(require 'request)
(defun swa/fetch-ethereum-price-coinmarketcap ()
"https://coinmarketcap.com/api/"
(request
"https://api.coinmarketcap.com/v1/ticker/ethereum/"
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq swa/ethereum-price
(assoc-default 'price_usd (elt data 0)))))))
;; (defun swa/fetch-ethereum-price-coinbase ()
;; "https://developers.coinbase.com/api/v2#get-exchange-rates"
;; (request
;; "https://api.coinbase.com/v2/prices/ETH-USD/buy"
;; :parser 'json-read
;; :success (cl-function
;; (lambda (&key data &allow-other-keys)
;; (setq swa/ethereum-price
;; (->> data
;; (assoc-default 'data)
;; (assoc-default 'amount)))))))
;; Display in mode line
(spaceline-define-segment ethereum-ticker
"Live ticker of the current price of ETH in USD"
(when (bound-and-true-p spaceline-ethereum-ticker-p)
(format "ETH: %s" swa/ethereum-price))
:when active
:global-override ethereum-ticker)
;; Toggle mode line
(defvar swa/modeline-ethereum-ticker-p
nil
"Is ethereum ticker in modeline enabled?")
(defvar swa/modeline-ethereum-ticker-timer
nil
"Var holding the timer for ethereum ticker mode line.")
(spacemacs|add-toggle mode-line-ethereum-ticker
:status swa/modeline-ethereum-ticker-p
:on (progn
(setq swa/modeline-ethereum-ticker-p t)
(setq swa/modeline-ethereum-ticker-timer
(run-at-time 5 30 'swa/fetch-ethereum-price-coinmarketcap))
(add-to-list 'spacemacs-spaceline-additional-segments 'ethereum-ticker)
(apply #'spaceline-spacemacs-theme
spacemacs-spaceline-additional-segments))
:off (progn
(setq swa/modeline-ethereum-ticker-p nil)
(when swa/modeline-ethereum-ticker-timer
(cancel-timer swa/modeline-ethereum-ticker-timer)
(setq swa/modeline-ethereum-ticker-timer nil))
(setq spacemacs-spaceline-additional-segments
(remq 'ethereum-ticker spacemacs-spaceline-additional-segments))
(apply #'spaceline-spacemacs-theme
spacemacs-spaceline-additional-segments))
:documentation "Show ethereum-ticker in the mode-line."
:evil-leader "oTe")
(spacemacs/toggle-mode-line-ethereum-ticker-on)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment