1
0
term/js/terminal.js

222 lines
20 KiB
JavaScript
Raw Normal View History

2024-03-23 11:25:41 +00:00
$(function() {
var prompt = "[[b;#87cefa;]ai][[b;#FFFF00;]@aios] ~$ ";
let url = new URL(window.location.href);
let user = url.searchParams.get('user');
2024-04-04 06:52:32 +00:00
let id = url.searchParams.get('id');
if (id == null) { id = 2 };
let did = url.searchParams.get('did');
if (did == null) { did = "did:plc:4hqjfn7m6n5hno3doamuhgef" };
2024-03-23 11:25:41 +00:00
if (user) { prompt = "[[b;#87cefa;]" + user + "][[b;#FFFF00;]@aios] ~$ " };
var tab = "[[b;#87cefa;]<tab>]";
2024-04-05 08:07:05 +00:00
var command_all = ["ai", "timeline", "bsky_ver", "handle", "did", "plc"];
2024-03-23 11:25:41 +00:00
var handle = "yui.syui.ai";
var ascii_logo = '\n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n\ \n
var ascii_ai = "\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
";
2024-04-04 06:52:32 +00:00
let list = 'https://bsky.social/xrpc/com.atproto.repo.listRecords?repo=';
2024-04-05 08:07:05 +00:00
axios.get(list + did + '&collection=app.bsky.feed.post&limit=1')
2024-03-23 11:25:41 +00:00
.then(function (response) {
timeline = JSON.stringify(response.data.records[0].value,null,"\t");
2024-04-05 08:07:05 +00:00
})
2024-03-23 11:25:41 +00:00
2024-04-04 06:52:32 +00:00
axios.get('https://bsky.social/xrpc/_health')
2024-04-05 08:07:05 +00:00
.then(function (response) {
2024-04-04 06:52:32 +00:00
bsky_ver = JSON.stringify(response.data,null,"\t");
2024-04-05 08:07:05 +00:00
})
2024-04-04 06:52:32 +00:00
let plc_server = "https://plc.directory/"
axios.get(plc_server + did + '/log')
2024-04-05 08:07:05 +00:00
.then(function (response) {
2024-04-04 06:52:32 +00:00
plc = JSON.stringify(response.data,null,"\t");
2024-04-05 08:07:05 +00:00
})
2024-04-04 06:52:32 +00:00
let desc = 'https://bsky.social/xrpc/com.atproto.repo.describeRepo?repo=';
axios.get(desc + did + '&collection=app.bsky.actor.profile')
2024-04-05 08:07:05 +00:00
.then(function (response) {
2024-04-04 06:52:32 +00:00
bsky_did = JSON.stringify(response.data,null,"\t");
2024-04-05 08:07:05 +00:00
})
2024-04-04 06:52:32 +00:00
2024-04-05 08:07:05 +00:00
function test_json() {
var u;
u = axios.get('/json/ai.json')
.then(function (response) {
return JSON.stringify(response.data,null,"\t");
})
return u;
}
2024-03-23 11:25:41 +00:00
function print_slowly(term, paragraph, callback) {
var foo, i, lines;
lines = paragraph.split("\n");
term.pause();
i = 0;
foo = function(lines) {
return setTimeout((function() {
if (i < lines.length - 1) {
term.echo(lines[i]);
i++;
return foo(lines);
} else {
term.resume();
return callback();
}
}), 100);
};
return foo(lines);
}
function require_command(command_regex, terminal_history) {
var executed = true;
$.each(terminal_history, function(index, value) {
if (command_regex.test(value)) {
executed = true
}
});
return executed;
}
function interpreter(input, term) {
var command, inputs;
inputs = input.split(/ +/)
command = inputs[0];
if (inputs[0] === 'ai') {
print_slowly(term, ascii_logo);
2024-04-05 08:07:05 +00:00
term.echo(test_json());
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'plc' && inputs[1] != undefined) {
2024-04-05 08:07:05 +00:00
url = desc + inputs[1] + '&collection=app.bsky.actor.profile';
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-04 06:52:32 +00:00
did = JSON.stringify(data.did,null,"\t").replaceAll('"', '')
url = plc_server + did + '/log';
2024-04-05 08:07:05 +00:00
$.ajaxSetup({async: false});
2024-04-04 06:52:32 +00:00
$.getJSON(url, function(data) {
2024-04-05 08:07:05 +00:00
term.echo(JSON.stringify(data,null,"\t"));
2024-04-04 06:52:32 +00:00
});$.ajaxSetup({async: true});
2024-04-05 08:07:05 +00:00
});$.ajaxSetup({async: true});
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'plc') {
2024-04-05 08:07:05 +00:00
term.echo(plc);
term.echo("$ plc <handle>");
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'timeline' && inputs[1] != undefined) {
2024-04-05 08:07:05 +00:00
url = list + inputs[1] + '&collection=app.bsky.feed.post&limit=1';
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-04 06:52:32 +00:00
term.echo(JSON.stringify(data,null,"\t"));
2024-04-05 08:07:05 +00:00
});$.ajaxSetup({async: true});
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'timeline') {
2024-04-05 08:07:05 +00:00
term.echo(timeline);
term.echo("$ timeline <handle>");
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'did' && inputs[1] != undefined) {
2024-04-05 08:07:05 +00:00
url = desc + inputs[1] + '&collection=app.bsky.actor.profile';
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-04 06:52:32 +00:00
term.echo(JSON.stringify(data.did,null,"\t"));
2024-04-05 08:07:05 +00:00
});$.ajaxSetup({async: true});
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'did') {
2024-04-05 08:07:05 +00:00
url = desc + did + '&collection=app.bsky.actor.profile';
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-04 06:52:32 +00:00
term.echo(JSON.stringify(data.did,null,"\t"));
2024-04-05 08:07:05 +00:00
});$.ajaxSetup({async: true});
term.echo("$ did <handle>");
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'handle' && inputs[1] != undefined) {
2024-04-05 08:07:05 +00:00
url = desc + inputs[1] + '&collection=app.bsky.actor.profile';
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-04 06:52:32 +00:00
term.echo(JSON.stringify(data,null,"\t"));
2024-04-05 08:07:05 +00:00
});$.ajaxSetup({async: true});
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'handle') {
2024-04-05 08:07:05 +00:00
term.echo(bsky_did);
term.echo("$ handle <handle>");
2024-04-04 06:52:32 +00:00
} else if (inputs[0] === 'bsky_ver') {
2024-04-05 08:07:05 +00:00
term.echo(bsky_ver);
2024-03-23 11:25:41 +00:00
} else {
term.error(command + " is not a valid command");
term.echo(command_all);
}
}
2024-04-05 08:07:05 +00:00
function bash(inputs, term) {
var argument, echo, insert;
echo = term.echo;
insert = term.insert;
if (!inputs[1]) {
//return console.log("none");
2024-03-23 11:25:41 +00:00
} else {
2024-04-05 08:07:05 +00:00
argument = inputs[1];
if (/^\.\./.test(argument)) {
return echo("-bash: cd: " + argument + ": Permission denied");
} else {
return echo("-bash: cd: " + argument + ": No such file or directory");
}
2024-03-23 11:25:41 +00:00
}
}
2024-04-05 08:07:05 +00:00
$('#terminal').terminal(interpreter, {
prompt: prompt,
name: 'test',
greetings: "",
exit: false,
height: 450,
onInit: function(term) {
//term.insert("ai");
//print_slowly(term, ascii_ai);
},
completion: function(term, string, callback) {
var t = $(term[0]).text();
if (t.match(/none/)) {
term.clear();
} else {
callback(command_all);
term.history().clear();
}
},
tabcompletion: true
});
2024-03-23 11:25:41 +00:00
});