orgdo
Version:
Command-line tool to manage the Todo lists
179 lines (178 loc) • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const os = require("os");
const utits_1 = require("./utits");
const COLORS = {
canceled: chalk_1.default.red,
done: chalk_1.default.green,
todo: chalk_1.default.white,
describe: chalk_1.default.gray,
doing: chalk_1.default.yellow,
high: chalk_1.default.bgRed.black,
low: chalk_1.default.bgBlue.black,
medium: chalk_1.default.bgGreen.black,
blod: chalk_1.default.bold,
italic: chalk_1.default.italic,
strikethrough: chalk_1.default.strikethrough,
code: chalk_1.default.cyan,
tag: chalk_1.default.magenta
};
function renderTasksStatistic(tasks) {
if (tasks.length === 0) {
return "";
}
const table = newTaskSummeryTable(tasks);
return renderSummaryTable(table) + os.EOL;
}
exports.renderTasksStatistic = renderTasksStatistic;
function renderSummaryTable(table) {
const columns = Object.keys(table.all);
const arr = [];
let titleLine = "";
let dashLine = "";
const sizes = columns.map(key => {
const size = Math.max(key.length, ...Object.keys(table).map(k => table[k][key].toString().length));
titleLine += `| ${utits_1.paddingLeft(key, size)} `;
dashLine += `| ${"-".repeat(size)} `;
return size;
});
arr.push(titleLine + "|");
arr.push(dashLine + "|");
const groupNames = Object.keys(table);
groupNames.slice(1).push("all");
groupNames.forEach(name => {
const row = table[name];
const line = columns.reduce((a, c, i) => {
a += `| ${utits_1.paddingLeft(row[c].toString(), sizes[i])} `;
return a;
}, "");
arr.push(line + "|");
});
return arr.join(os.EOL);
}
function renderTasks(tasks) {
const taskstr = tasks.map(task => renderTask(task)).join(os.EOL);
return taskstr ? os.EOL + taskstr + os.EOL : "";
}
exports.renderTasks = renderTasks;
function renderTask(task) {
let ret = "";
const indent = " ";
ret += renderSymbol(task) + indent.slice(0, -1);
ret += COLORS[task.status](renderMarkd(task.name));
ret += " " + renderId(task);
if (task.describe) {
ret += os.EOL;
ret += COLORS.describe(renderMarkd(task.describe)
.split(os.EOL)
.map(l => indent + l)
.join(os.EOL));
}
const timestr = renderTimes(task);
const tagstr = renderTags(task);
if (timestr || tagstr) {
ret += os.EOL + indent;
if (timestr) {
ret += timestr;
}
if (tagstr) {
ret += (timestr ? " " : "") + tagstr;
}
}
ret += os.EOL;
return ret;
}
function renderId(task) {
return COLORS[task.priority]("#" + task.id);
}
function renderMarkd(str) {
return str
.replace(/`(.*)`/g, COLORS.code("`$1`"))
.replace(/~(.*)~/g, COLORS.strikethrough("~$1~"))
.replace(/_(.*)_/g, COLORS.italic("_$1_"))
.replace(/\*(.*)\*/g, COLORS.blod("*$1*"));
}
function renderSymbol(task) {
let symbol = "";
switch (task.status) {
case "todo":
symbol = "☐";
break;
case "done":
symbol = "✔";
break;
case "canceled":
symbol = "✘";
break;
case "doing":
symbol = "❍";
break;
}
return COLORS[task.status](symbol);
}
function renderTimeTag(kind, time) {
return COLORS.tag(`$${kind}(${utits_1.shortTimeStr(utits_1.dateFormat(time))})`);
}
function renderTimes(task) {
const times = [];
if (task.start || task.started) {
if (task.started) {
times.push(renderTimeTag("started", task.started));
}
else {
times.push(renderTimeTag("start", task.start));
}
}
if (task.complete || task.completed) {
if (task.completed) {
times.push(renderTimeTag("completed", task.completed));
}
else {
times.push(renderTimeTag("complete", task.complete));
}
}
return times.join(" ");
}
function renderTags(task) {
return task.tags
.map(tag => {
return COLORS.tag("@" + tag);
})
.join(" ");
}
function newTaskSummeryTable(tasks) {
const table = {
all: newTaskSummeryTableRow("all")
};
for (const task of tasks) {
table.all[task.status]++;
task.tags.forEach(tag => {
if (!table[tag]) {
table[tag] = newTaskSummeryTableRow(tag);
}
table[tag][task.status]++;
});
}
Object.keys(table).forEach(key => {
const row = table[key];
const finishedNum = row.canceled + row.done;
const unfinishedNum = row.todo + row.doing;
const allNum = unfinishedNum + finishedNum;
row.all = allNum;
const percent = Math.round((100 * finishedNum) / allNum);
row.percent = percent === 0 ? "0" : "" + percent + "%";
});
return table;
}
function newTaskSummeryTableRow(name) {
return {
group: name,
todo: 0,
doing: 0,
done: 0,
canceled: 0,
all: 0,
percent: "0"
};
}