March, 2015

Getting battery percentage in zsh

I’m on osx maverick still at home on my laptop and I spent part of today dicking around customizing my zsh shell. I wanted to be able to show my battery percentage in the shell and it’s really pretty easy.

First, the main shell function

function get_battery()
{
    current_battery=`system_profiler SPPowerDataType | grep -i "charge remaining" | awk '{print $4}'`

    max_battery=`system_profiler SPPowerDataType | grep -i "full charge capacity" | awk '{print $5}'`

    percent=`bc <<< "scale=4; ${current_battery}/${max_battery} * 100"`

    printf '%.0f' $percent 
}

This queries from the profiler the charge current and remaining, uses bc to get a floating point division, and then just shows the integer value of that (we could round it too but I was lazy).

Now, we just need to tie it into the prompt. I’m using the steef prompt by default and just tweaked it a bit:

battery_percentage="$(get_battery)%%"

directory_info="${_prompt_steeef_colors[5]}%~%f" 

datetime="%F{yellow}[%D{%a %I:%M:%S %p}]%f" # yellow [Day hh:mm:ss am/pm] 
Read more

, ,

Handling subclassed constraints with a DSL in java 8

I really like doing all of my domain modeling with clean DSL’s (domain specific languages). Basically I want my code to read like a sentence, and to hide all the magic behind things. When things read clearly even a non professional can determine if something is wrong. The ideal scenario is to have your code read like pseudocode since nobody really cares what the internals are, what matters is your general solution.

I found myself recently in a scenario where I have some methods that do work, and they all return a subtype of a response root. Something like:

class Response extends ResponseRoot {}

class Worker{
   Response doWork(Request request) { // }
}

And I need to create a dynamic callback given the context of the request. So basically I want

Consumer<ResponseRoot> callback = generateCallback(request);

Response response = doWork(request);

callback.accept(response);

However I am going to have a lot of this … Read more

,

Installing leinigen on windows

Figured I’d spend part of the afternoon and play with clojure but was immediately thwarted trying to install leiningen on windows via powershell. I tried the msi installer but it didn’t seem to do anything, so I went to my ~/.lein/bin folder and ran

.lein\bin> .\lein.bat self-install
Downloading Leiningen now...
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\Gow/etc/wgetrc
--2015-03-14 15:08:48--  https://github.com/technomancy/leiningen/releases/download/2.5.1/leiningen-2.5.1-standalone.zip
Resolving github.com... 192.30.252.131
Connecting to github.com|192.30.252.131|:443... connected.
ERROR: cannot verify github.com's certificate, issued by `/C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert SHA2 Extended Validation Server CA':
  Unable to locally verify the issuer's authority.
To connect to github.com insecurely, use `--no-check-certificate'.
Unable to establish SSL connection.

Failed to download https://github.com/technomancy/leiningen/releases/download/2.5.1/leiningen-2.5.1-standalone.zip

Hmm, thats weird. For some reason the cert isn’t validating with wget (that I have installed via Gow).

A quick google showed that this is a common problem using the gow wget, and I wasn’t about to use the unsecured certificate check. I opened up … Read more

Simplifying class matching with java 8

I’m knee deep in akka these days and its a great queueing framework, but unfortunately I’m stuck using java and not able to use scala (business decisions, not mine!) so pattern matching on incoming untyped events can be kind of nasty.

You frequently see stuff like this in receive methods:

public void onReceive(Object message){
 if(message instanceof Something){

 }
 else if (message instanceof SomethingElse){

 }
 .. etc
}

And while that technically works, I really hate it because it promotes a monolothic function doing too much work. It also encourages less disciplined devs to put logic into the if block. While this is fine for a few checks, what happens when you need to dispatch 10, or 20 different types? It’s not uncommon in actor based systems to have lots of small message types.

Also, because akka gives you your object as a type erased Object you can’t use normal dispatching … Read more

, ,

Auto scaling akka routers

I’m working on a project where I need to multiplex many requests through a finite set of open sockets. For example, I have 200 messages, but I can only have at max 10 sockets open. To accomplish this I’ve wrapped the sockets in akka actors and am using an akka routing mechanism to “share” the 10 open sockets through a roundrobin queue.

This works out great, since now the consumers (who are rabbit mq listeners) just post messages to a facacde on the resource, and akka will route the request and do the appropriate work for me.

However, I wanted to know of a clean way to be able to add more resources (or remove them). Say at runtime I am asked to add 10 more open connections, or that suddenly we need to scale down to 5 connections. I’d like the router to be able to manage that for … Read more

,