Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
feat(app): Better feedback on killed processes.
Browse files Browse the repository at this point in the history
Now we show the names of all processes killed, along with the PID of the
processes that we did kill.
  • Loading branch information
robotlolita committed May 16, 2015
1 parent 2e3d2fc commit 8487075
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var sequence = require('control.monads').sequence;
var sanitise = JSON.stringify;
var toArray = [].slice.call.bind([].slice);

var chars = " -_abcdefghijklmnopqrstuvwxyz1234567890";
var flipped = " -_ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz⇂zƐㄣϛ9ㄥ860";
var chars = " -_.abcdefghijklmnopqrstuvwxyz1234567890";
var flipped = " -_'ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz⇂zƐㄣϛ9ㄥ860";


main(process.argv, process.pid);
Expand All @@ -17,37 +17,49 @@ main(process.argv, process.pid);
// :: [String], Number -> () *Eff*
function main(args, pid) {
if (args.length <3) {
show('Usage: fuck [you] PROCESS_NAME');
console.log('Usage: fuck [you] PROCESS_NAME');
process.exit(1);
}

var processName = last(args).get();
var pattern = last(args).get();
var processes = ps.list();
var killed = processes.map(function(data) {
return data.filter(match(pid, processName))
.map(kill)
});

killed.chain(sequence(Task))
.fork(
function(e) {
show('(; ̄Д ̄) . o O( It’s not very effective... )');
},
function(xs) {
if (xs.length > 0)
show('(╯°□°)╯︵', flip(processName), ' (x', xs.length, ')');
else
show('(; ̄Д ̄) . o O( I’ve got nothing on ' + processName + '! )');
}
);
var killed = processes.map(function(data) {
return data.filter(match(pid, pattern))
.map(kill);
});

killed.chain(sequence(Task)).map(collectUnique).fork(
function onError(e) {
console.log('');
shock('It’s not very effective...');
},
function onSuccess(xs) {
console.log('');
if (xs.length > 0)
xs.forEach(function(process){
rage(flip(process.name), '(x', process.pids.length, ': ', process.pids.join(', '), ')');
});
else
shock('I’ve got nothing on ', pattern);
}
);
}


// :: [String] -> () *Eff*
function show(xs) {
console.log(xs.join('') + '\n');
}

// :: String... -> () *Eff*
function show() {
console.log('\n ' + toArray(arguments).join('') + '\n');
function rage() {
return show([' (╯°□°)╯︵'].concat(toArray(arguments)));
}

// :: String... -> () *Eff*
function shock() {
return show([' (; ̄Д ̄) . o O( '].concat(toArray(arguments)).concat([' )']));
}

// :: String -> String
function flip(name) {
Expand All @@ -70,6 +82,35 @@ function kill(process) {
});
}

// :: [{ name: String, pid: Number }] -> [{ name: String, pids: [Number] }]
function collectUnique(processes) {
return processes.reduce(doCollect, []);

function doCollect(processes, process) {
return findIndex(processes, function(a){ return a.name === process.name }).cata({
Nothing: function() {
return processes.concat([{ name: process.name, pids: [process.pid] }]);
},
Just: function(index) {
var item = processes[index];

return processes.slice(0, index)
.concat([{ name: process.name, pids: item.pids.concat([process.pid]) }])
.concat(processes.slice(index + 1));
}
});
}
}

// :: ['a], ('a -> Boolean) -> Maybe(Number)
function findIndex(xs, predicate) {
for (var i = 0; i < xs.length; ++i) {
if (predicate(xs[i])) return Maybe.Just(i);
}
return Maybe.Nothing();
}


// :: Number, String -> { name: String, pid: Number } -> Boolean
function match(pid, pattern){ return function(process) {
return toRegExp(pattern).test(process.name)
Expand Down

0 comments on commit 8487075

Please sign in to comment.