Skip to content

Instantly share code, notes, and snippets.

View walm's full-sized avatar

Andreas Wålm walm

  • Sweden
View GitHub Profile
class ProgressBar
def initialize(units=60)
@units = units.to_f
end
def print(completed, total)
norm = 1.0 / (total / @units)
progress = (completed * norm).ceil
pending = @units - progress
Kernel.print "[#{'=' * progress }#{' ' * ( pending )}] #{percentage(completed, total)}%\r"
@walm
walm / 1_server-stats-json.sh
Last active April 22, 2025 18:53
Simple Linux Server stats as JSON
#!/bin/sh
echo -n '{'
# memory as "mem": { "current": 800, "total": 1024, "load", 82 } where amount is in MB and load in %
free -m | awk 'NR==2{printf "\"mem\": { \"current\":%d, \"total\":%d, \"load\": %.2f }", $3,$2,$3*100/$2 }'
echo -n ','
# diska as "disk": { "current": 6, "total": 40, "used": 19 } where amount is in GB and used in %
df -h | awk '$NF=="/"{printf "\"disk\": { \"current\":%d, \"total\":%d, \"used\": %d }", $3,$2,$5}'
@walm
walm / ssh_config.sh
Created June 25, 2014 13:01
Dynamic SSH host config for docker containers
# usage: ssh <container-name>.doc
Host *.doc
ProxyCommand netcat $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' `echo %h | sed -e 's/.doc//'`) 22
StrictHostKeyChecking no
User root
IdentityFile /etc/ssh/keys/docker.key
@walm
walm / gist:f5488e536a0ba329fbd0
Last active October 10, 2017 10:26
c# async upload file
private static async Task<bool> UploadFile(string filePath, string actionUrl)
{
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
HttpContent filePathContent = new StringContent(filePath);
HttpContent fileStreamContent = new StreamContent(file);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(filePathContent, "filepath");
formData.Add(fileStreamContent, "file", Path.GetFileName(filePath));
@walm
walm / gist:7556087d029c7195b3da
Created October 16, 2014 06:29
Dropbox git
# taken from http://stackoverflow.com/questions/1960799/using-git-and-dropbox-together-effectively
~/project $ git init
~/project $ git add .
~/project $ git commit -m "first commit"
~/project $ cd ~/Dropbox/git
~/Dropbox/git $ git init --bare project.git
~/Dropbox/git $ cd ~/project
@walm
walm / gist:1520328bdf617ea46c70
Last active August 29, 2015 14:07
OSX Yosemite - Clean install
Use an 8GB+ USB stick and format it with Mac OS Extended (Journaled) and name Untitled
sudo /Applications/Install\ OS\ X\ Yosemite.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ Yosemite.app --nointeraction
@walm
walm / gist:352b49e708119bc8bbf4
Created October 20, 2014 06:39
Safari backspace as back

Enable

defaults write com.apple.Safari NSUserKeyEquivalents -dict-add Back "\U232b"

Disable

defaults delete com.apple.Safari NSUserKeyEquivalents

@walm
walm / gist:a70c311c5922cd4be9e7
Last active August 29, 2015 14:07
Bundle Identifier as environment var (Xcode build script)
# Apples build env's https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html
#
ROOT_PRODUCT_IDENTIFIER_PLIST=`/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" $INFOPLIST_FILE`
export ROOT_PRODUCT_IDENTIFIER=`eval echo $ROOT_PRODUCT_IDENTIFIER_PLIST`
@walm
walm / redbridge-api-request.rb
Created December 22, 2014 09:11
RedBridge API request sign
# port from https://portal.redbridge.se/faq/api/guide/
require 'openssl'
require 'digest/hmac'
require 'base64'
require 'cgi'
BASE_URL = "https://api.rbcloud.net/client/api"
API_KEY = "your api key"
SECRET_KEY = "your secret key"
@walm
walm / main.go
Created February 11, 2015 17:52
Unique values from slice of string
// in Go Play: http://play.golang.org/p/G5e-E8fe7h
package main
import "fmt"
func UniqStrs(slice []string) []string {
m := map[string]bool{}
for _, v := range slice {
m[v] = true
}