1
0
term/js/terminal.js

276 lines
21 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 17:14:12 +00:00
var command_all = ["ai","ls","ai.json"];
2024-03-23 11:25:41 +00:00
var handle = "yui.syui.ai";
2024-04-05 17:14:12 +00:00
var file_all = "ai.json";
var ai_help = "\n\
\nCommands:\
\n ai\
\n\
\nFlags:\
\n\t\t-h, --help\
\n\
\nOptions:\
\n\ta\t\t\t\t: logo\
\n\tv\t\t\t\t: pds version\
\n\tp\t\t\t\t: ai profile\
\n\tdid\t<handle> : did\
\n\tplc\t<handle> : plc\
\n\trecord <handle> : timeline\
\n\
\nex:\
\n\t$ ai v\t: {\"version\":\"0.0.1\"}\
\n\t\
\nhttps://git.syui.ai/ai/bot\
\n\t\
";
2024-03-23 11:25:41 +00:00
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 17:14:12 +00:00
function bsky_record() {
var u;
u = axios.get(list + did + '&collection=app.bsky.feed.post&limit=1')
.then(function (response) {
return JSON.stringify(response.data.records[0].value,null,"\t");
})
return u;
}
2024-03-23 11:25:41 +00:00
2024-04-05 17:14:12 +00:00
function bsky_ver() {
var u;
u = axios.get('https://bsky.social/xrpc/_health')
.then(function (response) {
return JSON.stringify(response.data,null,"\t");
})
return u;
}
2024-04-04 06:52:32 +00:00
let plc_server = "https://plc.directory/"
2024-04-05 17:14:12 +00:00
function bsky_plc() {
var u;
u = axios.get(plc_server + did + '/log')
.then(function (response) {
return JSON.stringify(response.data,null,"\t");
})
return u;
}
2024-04-04 06:52:32 +00:00
let desc = 'https://bsky.social/xrpc/com.atproto.repo.describeRepo?repo=';
2024-04-05 17:14:12 +00:00
function bsky_desc() {
var u;
u = axios.get(desc + did + '&collection=app.bsky.actor.profile')
.then(function (response) {
return JSON.stringify(response.data,null,"\t");
})
return u;
}
function bsky_did() {
var u;
u = axios.get(desc + handle + '&collection=app.bsky.actor.profile')
.then(function (response) {
return JSON.stringify(response.data.did,null,"\t").replaceAll('"', '');
})
return u;
}
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];
2024-04-05 17:14:12 +00:00
option = inputs[1];
if (did == null) { did = "did:plc:4hqjfn7m6n5hno3doamuhgef" };
if (handle == null) { handle = "did:plc:4hqjfn7m6n5hno3doamuhgef" };
if (command === 'ai' && inputs[1] === undefined) {
term.echo(ai_help);
} else if (command === 'ls') {
term.echo(file_all);
} else if (command === 'cat') {
if (option === 'ai.json') {
term.echo(test_json());
} else {
term.echo(file_all);
}
} else if (command === 'ai' && option === 'a') {
2024-03-23 11:25:41 +00:00
print_slowly(term, ascii_logo);
2024-04-05 17:14:12 +00:00
} else if (command === 'ai' && option === 'p') {
term.echo(ascii_ai);
2024-04-05 08:07:05 +00:00
term.echo(test_json());
2024-04-05 17:14:12 +00:00
} else if (command === 'ai' && option === 'plc') {
if (inputs[2] != undefined) { did = inputs[2]; }
url = desc + did + '&collection=app.bsky.actor.profile';
2024-04-05 08:07:05 +00:00
$.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-05 17:14:12 +00:00
} else if (command === 'ai' && option === 'record') {
if (inputs[2] != undefined) { did = inputs[2]; }
url = list + did + '&collection=app.bsky.feed.post&limit=1';
2024-04-05 08:07:05 +00:00
$.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 17:14:12 +00:00
});$.ajaxSetup({async: true});
} else if (command === 'ai' && option === 'did') {
if (inputs[2] != undefined) { handle = inputs[2]; }
url = desc + handle + '&collection=app.bsky.actor.profile';
2024-04-05 08:07:05 +00:00
$.ajaxSetup({async: false});
$.getJSON(url, function(data) {
2024-04-05 17:14:12 +00:00
term.echo(JSON.stringify(data.did,null,"\t").replaceAll('"', ''));
});$.ajaxSetup({async: true});
} else if (command === 'ai' && option === 'handle') {
if (inputs[2] != undefined) { did = inputs[2]; }
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-05 17:14:12 +00:00
term.echo(JSON.stringify(data,null,"\t"));
});$.ajaxSetup({async: true});
} else if (command === 'ai' && option === 'v') {
url = 'https://bsky.social/xrpc/_health';
2024-04-05 08:07:05 +00:00
$.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 17:14:12 +00:00
});$.ajaxSetup({async: true});
2024-03-23 11:25:41 +00:00
} else {
2024-04-05 17:14:12 +00:00
term.echo(ai_help);
2024-03-23 11:25:41 +00:00
}
}
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
});