Skip to content

Instantly share code, notes, and snippets.

View metametadata's full-sized avatar

Yuri Govorushchenko metametadata

View GitHub Profile
@metametadata
metametadata / react-bootstrap.cljs
Last active June 6, 2017 07:56
Fixing of caret/cursor jumps in text inputs of cljsjs.react-bootstrap
(ns app.components.react-bootstrap
(:require [cljsjs.react-bootstrap]
[reagent.core :as r]))
(def Alert (r/adapt-react-class (.-Alert js/ReactBootstrap)))
(def Button (r/adapt-react-class (.-Button js/ReactBootstrap)))
; ...
(def FormControl (r/adapt-react-class (.-FormControl js/ReactBootstrap))) ; WARNING: use FormControlFixed for text input controls instead
(defn FormControlFixed
@metametadata
metametadata / asserts.cljc
Last active June 8, 2017 02:50
Portable Clojure/ClojureScript wrappers for (is (thrown-with-msg? ...)) test assertions
#?(:clj
(defn- cljs-env?
"Take the &env from a macro, and tell whether we are expanding into cljs.
Source: http://v.gd/rmKNdf"
[env]
(boolean (:ns env))))
#?(:clj
(defmacro is-exception-thrown
"(is (thrown-with-msg? ...)) for specified exceptions in Clojure/ClojureScript."
(ns frontend.counter
(:require [frontend.ui :as ui]
[cljs.core.match :refer-macros [match]]
[reagent.core :as r]))
(defn init
"Creates a model intance."
[x]
x)
@metametadata
metametadata / gist:0097efa1a081ce6d7bb3
Last active June 8, 2017 02:51
listen to clipboard in Clojure using core.async
;...
(:import java.awt.Toolkit)
(:import (java.awt.datatransfer DataFlavor))
;...
(defn- printer
[in]
(go (while true (println (<! in)))))
(defn- clipboard-listener
@metametadata
metametadata / gist:7ea557f8a2f1bdddd574
Created June 18, 2015 11:48
sleep in clojurescript
(defn sleep [msec]
(let [deadline (+ msec (.getTime (js/Date.)))]
(while (> deadline (.getTime (js/Date.))))))
@metametadata
metametadata / spawn-in-lumo.cljs
Last active July 14, 2017 18:51
Example of using NodeJS spawn in Lumo
(def child-process (js/require "child_process"))
(def on-exit (js/require "signal-exit"))
(defn -spawn
"Thin wrapper around NodeJS function.
Spawns the process in the background. Returns the created ChildProcess instance."
[command args options]
(let [name (str command " " (str/join " " args))]
(println "ᐅ" name)
(-> (.spawn child-process
@metametadata
metametadata / core.cljs
Last active September 29, 2017 15:38
a taste of core.async with <?
"
Naming conventions:
go - executes code which contains special blocking operations (e.g. <!), returns a channel, throws global uncaught exceptions
<! - reads from channel without any special treatment of returned error values
<? - reads from maybe-channel, if there's an error value - throws it
<?go - the same as go but \"swallows\" exceptions and returns a maybe-channel
<foo/<?foo:
1) channel/maybe-channel
2) a function returning a channel (e.g. <?fetch-items)
@metametadata
metametadata / reporter.clj
Created November 3, 2017 15:29
Prettifying Clojure test reports
; Prettify test reports
(ns unit.reporter
(:require
[clojure.test :refer [report with-test-out]]))
; Change the report multimethod to ignore namespaces that don't contain any tests.
; taken from: http://blog.jayfields.com/2010/08/clojuretest-introduction.html
(defmethod report :begin-test-ns [m]
(with-test-out
(when (some #(:test (meta %)) (vals (ns-interns (:ns m))))
@metametadata
metametadata / jenkins.groovy
Created February 6, 2018 03:28
Read port from "Jenkins URL" field that is set in http://<jenkins>/configure.def
def jenkinsPort = new URI(jenkins.model.JenkinsLocationConfiguration.get().getUrl()).port
@metametadata
metametadata / ip.py
Created February 6, 2018 03:31
Cross-platform local IP detection.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Prints the machine's IP address.
# Inspired by https://stackoverflow.com/a/28950776/4839573. Tested in Linux and MacOS.
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Doesn't even have to be reachable.