-
-
Save chrisdickinson/59cb34aa3ea03aa27653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
metadata_port=$(cat ~/share/redis-metadata.conf | grep port | awk '{print $2}') | |
how_many_jobs() { | |
output=$(jobs | wc -l | sed -e 's/^ *//' | sed -e 's/^0$//') | |
if [ "$output" ]; then | |
echo "$output:" | |
fi | |
} | |
git_in_repo() { | |
git branch 2> /dev/null | head -n1| awk '{print "⭠ "}' | |
} | |
git_branch() { | |
git branch 2> /dev/null | grep '^\*' | awk '{print $2}' | |
} | |
github_stats() { | |
repo=$(git remote -v 2>/dev/null | grep origin | grep push | grep github | awk '{print $2}') | |
if [ "$repo" ]; then | |
redis-cli -p $metadata_port publish want-metadata $repo 2>/dev/null 1>/dev/null | |
output=`redis-cli --raw -p $metadata_port get $repo 2>/dev/null` | |
if [ "$output" ]; then | |
echo " $output" | |
fi | |
fi | |
} | |
meta_steps() { | |
output=`redis-cli --raw -p $metadata_port get fitbit/steps/today 2>/dev/null` | |
if [ "$output" ]; then | |
echo " $output" | |
fi | |
} | |
PS1='$(how_many_jobs)${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\] in [\[\033[01;34m\]\w\[\033[00m\]] $(git_in_repo)\[\033[01;35m\]$(git_branch)\[\033[00m\]$(github_stats)$(meta_steps)\n\$ ' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('redis') | |
, https = require('https') | |
, concat = require('concat-stream') | |
, template = require('templatinglanguage') | |
, fitbit = require('fitbit-js') | |
, colors = require('colors') | |
, url = require('url') | |
, fs = require('fs') | |
var token = fs.readFileSync(home('~/.github-token'), 'utf8').trim() | |
, redis_conf = fs.readFileSync(home('~/share/redis-metadata.conf'), 'utf8') | |
, fitbit_auth = JSON.parse(fs.readFileSync(home('~/.fitbit-key-secret.json'), 'utf8')) | |
, fitbit_client = JSON.parse(fs.readFileSync(home('~/share/fitbit-app.json'), 'utf8')) | |
, redis_port | |
, templates | |
, inflight | |
fitbit = fitbit( | |
fitbit_client.key | |
, fitbit_client.secret | |
).apiCall | |
var format = template('[{{ watchers }} '+'\u2605'.yellow+' / {{ open_issues_count }} '+'\u26A0 '.yellow+']') | |
, format_steps = template('{{ activities-steps.0.value }} '+'steps'.blue) | |
, fitbit_inflight | |
// we only get 150 reqs/hour from fitbit | |
setInterval(function() { | |
if(fitbit_inflight) { | |
return | |
} | |
fitbit_inflight = true | |
fitbit('GET', '/user/-/activities/steps/date/today/1d.json', {token: fitbit_auth}, function(err, response, data) { | |
fitbit_inflight = false | |
if(err) { | |
return | |
} | |
client.set('fitbit/steps/today', format_steps(data)) | |
}) | |
}, 30000) | |
inflight = {} | |
templates = {} | |
redis_port = +redis_conf.split('\n').filter(function(x) { return x.indexOf('port') === 0 })[0].split(' ')[1] | |
var pubsub = redis.createClient(redis_port) | |
, client = redis.createClient(redis_port) | |
pubsub.subscribe('want-metadata') | |
pubsub.on('message', function(channel, message) { | |
var original = message | |
if(message.indexOf('://') === -1) { | |
message = 'ssh://'+message.replace(':', '/') | |
} | |
var id = url.parse(message).pathname.slice(1).replace(/.git$/, '') | |
fetch_github(id, function(err, data) { | |
client.set(original, data) | |
}) | |
}) | |
function github_get_repo(repo_id, ready) { | |
inflight[repo_id] = true | |
var opts = url.parse('https://api.github.com/repos/'+repo_id) | |
, request | |
, status | |
opts.method = 'GET' | |
opts.headers = {} | |
opts.headers.authorization = 'Token '+token | |
request = https.request(opts, got_response) | |
request.on('error', function(){}) | |
request.end() | |
function got_response(response) { | |
status = response.statusCode | |
response.setEncoding('utf8') | |
response | |
.on('error', function(){}) | |
.pipe(concat(got_data)) | |
.on('error', function(){}) | |
} | |
function got_data(err, data) { | |
if(status !== 200) { | |
return ready(new Error(status)) | |
} | |
inflight[repo_id] = false | |
if(err) return ready(err) | |
try { | |
return ready(null, JSON.parse(data)) | |
} catch(err) { | |
return ready(err) | |
} | |
} | |
} | |
function fetch_github(repo_id, ready) { | |
if(inflight[repo_id]) { | |
return | |
} | |
var now = Date.now() | |
client.get('last:'+repo_id, got_last) | |
function got_last(err, data) { | |
data = data|0 | |
if(now - data < 5000) { | |
return | |
} | |
github_get_repo(repo_id, got_repo_data) | |
} | |
function got_repo_data(err, data) { | |
if(err) { return } | |
client.set('last:'+repo_id, Date.now()) | |
client.set('meta:'+repo_id, JSON.stringify(data)) | |
ready(null, format(data || {})) | |
} | |
} | |
function get_template(repo_id, ready) { | |
return ready(function(context, ready) { format.render(context, ready) }) | |
;(function(context, ready) { | |
return ready(null, context.watchers + '\u2665' + (context.watchers === 1 ? '' : 's')) | |
}) | |
} | |
function home(str) { | |
return str.replace(/^~/, process.env.HOME) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment