Skip to content

Instantly share code, notes, and snippets.

@ntkog
Created February 8, 2016 08:53
Show Gist options
  • Save ntkog/1547d93e2b9247db7524 to your computer and use it in GitHub Desktop.
Save ntkog/1547d93e2b9247db7524 to your computer and use it in GitHub Desktop.
Filter and fix commands from a asciicast doc file
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/asciinema-player.css" />
<script src="/asciinema-player.js"></script>
</head>
<body>
<div id="player-container"></div>
<script>
function generateCommandList(source) {
var clue = new RegExp(/\[37;1m/);
var commands = [];
function removeBackspaces (str) {
function lookAhead(arr,idx) {
var count = 1;
var x = idx;
while (arr[x] === '') {
count++;
x++;
}
return count;
}
var matches = [];
var result = '';
var BS = /\x08\x1b\[K/g;
var segments = str.split(BS);
var j = 0;
var list = []
while(j < segments.length) {
var skip = j == segments.length - 1 ? 0 : lookAhead(segments,j+1);
if(skip === 0) {
list.push(segments[j]);
//Force to step out of the loop
j = segments.length;
} else {
list.push(segments[j].substring(0, segments[j].length - skip));
j = j + skip;
}
}
return list.join("");
}
function buildCommand (arr, beginIdx) {
function getTimelineCue(idx) {
return source.slice(0,idx)
.map(function(o) {
return o[0];
})
.reduce(function (prev,cur,ix,v){
return prev+cur;
}) - 1;
}
var command = arr
.map(function(tc){
return tc[1];
})
.concat([])
.join("");
return {
start: getTimelineCue(beginIdx),
command: removeBackspaces(command.split("\r\n")[0].split("0m ")[2])
};
}
for(i = 0, len = source.length, lastMatch = 0; i <= len; i++) {
if (clue.test(source[i]) ) {
if (i == 0) {
//First command is on the first line , Skip it till next one
} else {
commands.push(buildCommand(source.slice(lastMatch,i-1), i));
//Update lastMatch
lastMatch = i;
}
}
}
return commands;
}
var file = "/vagrant5.json";
asciinema_player.core.CreatePlayer('player-container', file, { speed: 1, autoPlay: true});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment