forked from cory/tildefriends
Cory McWilliams
41a12fd1a7
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3193 ed5197a5-7fde-0310-b194-c3ffbd925b24
75 lines
2.0 KiB
JavaScript
75 lines
2.0 KiB
JavaScript
"use strict";
|
|
|
|
core.register("onSessionBegin", index);
|
|
core.register("onSessionEnd", index);
|
|
|
|
function index() {
|
|
return Promise.all([core.getPackages(), core.getUsers()]).then(function(values) {
|
|
let packages = values[0];
|
|
let users = values[1];
|
|
let usersByApp = {};
|
|
let servicesByApp = {};
|
|
for (let i in users) {
|
|
let user = users[i];
|
|
let key = "/~" + user.packageOwner + "/" + user.packageName;
|
|
if (user.key.substring(0, "service_".length) == "service_") {
|
|
if (!servicesByApp[key]) {
|
|
servicesByApp[key] = [];
|
|
}
|
|
servicesByApp[key].push(user);
|
|
} else {
|
|
if (!usersByApp[key]) {
|
|
usersByApp[key] = [];
|
|
}
|
|
usersByApp[key].push(user.name);
|
|
}
|
|
}
|
|
|
|
terminal.cork();
|
|
terminal.clear();
|
|
terminal.print("Available applications [active users]:");
|
|
packages.sort(function(x, y) {
|
|
return Math.sign(x.owner.localeCompare(y.owner)) * 10 + Math.sign(x.name.localeCompare(y.name)) * 1;
|
|
}).forEach(function(app) {
|
|
let users = usersByApp["/~" + app.owner + "/" + app.name];
|
|
let services = servicesByApp["/~" + app.owner + "/" + app.name];
|
|
let message = [];
|
|
if (users || services) {
|
|
message.push(" [");
|
|
if (users) {
|
|
let counts = {};
|
|
for (let i = 0; i < users.length; i++) {
|
|
counts[users[i]] = (counts[users[i]] || 0) + 1;
|
|
}
|
|
let names = Object.keys(counts).sort();
|
|
for (let i = 0; i < names.length; i++) {
|
|
var name = names[i];
|
|
if (message.length > 1) {
|
|
message.push(", ");
|
|
}
|
|
message.push({class: "orange", value: name});
|
|
if (counts[name] > 1) {
|
|
message.push({class: "base01", value: "(x" + counts[name] + ")"});
|
|
}
|
|
}
|
|
}
|
|
if (services) {
|
|
if (users) {
|
|
message.push(", ");
|
|
}
|
|
message.push("⚒".repeat(services.length));
|
|
}
|
|
message.push("]");
|
|
}
|
|
terminal.print(
|
|
"* ",
|
|
{href: "/~" + app.owner + "/" + app.name},
|
|
message);
|
|
});
|
|
terminal.uncork();
|
|
});
|
|
}
|
|
|
|
index().catch(function(error) {
|
|
terminal.print("ERROR:", error);
|
|
}); |