Skip to content

Instantly share code, notes, and snippets.

@tokland
tokland / functional_virtual_dom_simple_demo.html
Last active March 19, 2017 21:13
Simple demo using immutable states with virtual-dom
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es">
<head>
<title>Simple functional app using VirtualDom</title>
<script src="http://wzrd.in/standalone/virtual-dom@latest"></script>
<script src="simple_virtual_dom.js"></script>
<script src="incrementor.js"></script>
</head>
<body>
<div id="incrementor">
@tokland
tokland / data-entry-contentscript.js
Created June 12, 2017 10:14
Group subsections tabs for DHIS2 dataentry
/*
Group subsections tabs (with names "section@subsection1", "section@subsection2") into a single tab "section"
Save as a contentscript in DHIS2 app customJS/CSS (https://www.dhis2.org/download/appstore/customjscss.zip)
*/
var init = function(contentEl) {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var displayingTabs = contentEl.children().size() > 0;
var observer = new MutationObserver(function(mutations, observer) {
if (!displayingTabs && contentEl.children().size() > 0) {
@tokland
tokland / Gemfile
Last active June 21, 2017 07:48
Get report hours from toggl.com
source "https://ruby.taobao.org"
ruby "2.3.0"
gem "activesupport"
gem "togglv8"
gem "awesome_print"
gem "trollop"
group :development do
@tokland
tokland / xfce4-save-session.sh
Created October 30, 2017 10:03
Xfce4 save session
#!/bin/sh
exec dbus-send \
--session \
--dest=org.xfce.SessionManager \
--print-reply /org/xfce/SessionManager \
org.xfce.Session.Manager.Checkpoint string:""
#!/bin/bash
#
# Record a video of a mod/s3m/xm tracker song played using milkytracker.
#
# Dependencies with versions used in parenthesis:
#
# schismtracker / milkytracker
# pulseaudio
# vnc2flv
# turbovnc
@tokland
tokland / video-resize.sh
Last active December 25, 2017 22:31
Resize videos to a fixed size with avconv
#!/bin/sh
#
# Resize videos to a given size.
# Depedencies: libav
debug() {
echo "$@" >&2
}
to_num() { local NUM_STRING=$1
@tokland
tokland / Counter.js
Last active May 20, 2018 09:21
Avoid setState in stateful React components
/* Simple counter example. Decorate any method that returns new state, including
lifecycle component methods. */
import React from 'react';
import { action } from './action';
class Counter extends React.PureComponent {
constructor(props) {
super(props);
this.state = { value : 0 };
}
module StringMap = Map.Make({ type t = string; let compare = compare; });
let mapFromList = (pairs: list((string, 'a))): StringMap.t('a) =>
List.fold_left((map, (k, v)) => StringMap.add(k, v, map), StringMap.empty, pairs);
let mapGet = (key: string, defaultValue: 'a, mapping: StringMap.t('a)): 'a =>
switch (StringMap.find(key, mapping)) {
| exception Not_found => defaultValue
| value => value
};
@tokland
tokland / 1simple-counter.js
Last active July 8, 2018 17:07
Declarative React Component
import React from 'react';
import { build } from "./declarative-component";
const initialState = { value: 0 };
const actions = {
decrement: (num) => state => ({ ...state, value: state.value - 1 }),
increment: (num) => state => ({ ...state, value: state.value + 1 }),
};
@tokland
tokland / external-command.rb
Created August 18, 2010 18:01 — forked from careo/external-command.rb
Capture stderr data on EventMachine.popen
require 'rubygems'
require 'eventmachine'
$stdout.sync = true
$stderr.sync = true
EM.run {
EM.add_periodic_timer(0.1) {
$stdout.write "stdout\n"