Skip to content

Commit 794302d

Browse files
authored
Merge pull request #41 from yistarostin/28-more-friendly-task-id
Fix Issue 28: more friendly task
2 parents 08fe322 + 5562c25 commit 794302d

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/cli/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@ commander
7373
commander
7474
.command("contest [contest-id]")
7575
.action(commands.contest)
76+
.option("-t, --title", "show contest id")
7677
.option("-i, --id", "show contest id")
78+
.option("-u, --url", "show contest id")
7779
.description("get contest information");
7880

7981
commander
8082
.command("task [contest-id] [task-id]")
8183
.action(commands.task)
84+
.option("-l, --label", "show task label")
85+
.option("-t, --title", "show task title")
8286
.option("-i, --id", "show task id")
87+
.option("-u, --url", "show task url")
8388
.description("get task");
8489

8590
commander

src/commands.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export async function session() {
2222
console.log(await atcoder.checkSession() ? "OK" : "not login");
2323
}
2424

25-
export async function contest(contest_id: string | undefined, options: { id?: boolean }) {
25+
export async function contest(contest_id: string | undefined, options: {title?: boolean, id?: boolean, url?: boolean}) {
2626
const f_id = options.id === true;
27-
const format = ({id, title, url}: Contest) => formatAsShellOutput([[f_id ? SGR(id, 37) : null, SGR(title, 32, 1), url].filter(e => e !== null) as Array<string>]);
27+
const f_title = options.title === true;
28+
const f_url = options.url === true;
29+
const format = ({id, title, url}: Contest) => formatAsShellOutput([((f_id || f_title || f_url) ? [f_id ? SGR(id, 37) : null, SGR(title, 32, 1), url].filter(e => e !== null) : [SGR(id, 37), SGR(title, 32, 1), url]) as Array<string>]);
2830
if (contest_id === undefined) {
2931
// idが与えられていない場合、プロジェクトファイルを探してコンテスト情報を表示
3032
try {
@@ -46,9 +48,14 @@ export async function contest(contest_id: string | undefined, options: { id?: bo
4648
}
4749
}
4850

49-
export async function task(contest_id: string | undefined, task_id: string | undefined, options: { id?: boolean }) {
51+
export async function task(contest_id: string | undefined, task_id: string | undefined, options: {label?: boolean, title?: boolean, id?: boolean, url?: boolean}) {
5052
const f_id = options.id === true;
51-
const format = ({id, label, title, url}: Task) => formatAsShellOutput([[f_id ? SGR(id, 37) : null, SGR(label, 32), SGR(title, 32, 1), url].filter(e => e !== null) as Array<string>]);
53+
const f_title = options.title === true;
54+
const f_url = options.url === true;
55+
const f_label = options.label === true;
56+
57+
//in case all flags are false (no print options specified), the hole (label, title, id, url string) is printed
58+
const format = ({id, label, title, url}: Task) => formatAsShellOutput([((f_label || f_id || f_url || f_label) ? [f_label ? SGR(label, 32) : null, f_title ? SGR(title, 32) : null, f_id ? SGR(id, 37) : null, f_url ? url : null].filter(e => e !== null) : [SGR(label, 32), SGR(title, 32), SGR(id, 37), url]) as Array<string>]);
5259
if (contest_id === undefined && task_id === undefined) {
5360
// idが与えられていない場合、プロジェクトファイルを探す
5461
try {
@@ -77,7 +84,7 @@ export async function task(contest_id: string | undefined, task_id: string | und
7784
}
7885
}
7986

80-
export async function tasks(contest_id: string | undefined, options: { id?: boolean }) {
87+
export async function tasks(contest_id: string | undefined, options: {id?: boolean}) {
8188
const f_id = options.id === true;
8289
const format = (tasks: Array<Task>) => formatAsShellOutput(tasks.map(({id, label, title, url}) => [f_id ? SGR(id, 37) : null, SGR(label, 32), SGR(title, 32, 1), url].filter(e => e !== null) as Array<string>));
8390
if (contest_id === undefined) {
@@ -101,7 +108,7 @@ export async function tasks(contest_id: string | undefined, options: { id?: bool
101108
}
102109
}
103110

104-
export async function url(contest_id: string | undefined, task_id: string | undefined, options: { check?: boolean }) {
111+
export async function url(contest_id: string | undefined, task_id: string | undefined, options: {check?: boolean}) {
105112
const f_check = options.check === true;
106113
if (contest_id !== undefined && task_id !== undefined) {
107114
if (f_check) {
@@ -187,7 +194,7 @@ export async function format(format_string: string, contest_id: string, task_id?
187194
}
188195
}
189196

190-
export async function submit(filename: string | undefined, facade_options: Array<string>, options: { task?: string, contest?: string, skipFilename?: boolean }) {
197+
export async function submit(filename: string | undefined, facade_options: Array<string>, options: {task?: string, contest?: string, skipFilename?: boolean}) {
191198
let contest_id = options.contest;
192199
let task_id = options.task;
193200
const f_skip_filename = options.skipFilename === true;
@@ -246,7 +253,7 @@ export async function configDir() {
246253
console.log(await getConfigDirectory());
247254
}
248255

249-
export async function config(key: string | undefined, value: string | undefined, options: { D?: boolean }) {
256+
export async function config(key: string | undefined, value: string | undefined, options: {D?: boolean}) {
250257
if (options.D) {
251258
await deleteGlobalConfig(key);
252259
}
@@ -338,7 +345,7 @@ async function getTemplateFromOption(template?: string | boolean): Promise<Templ
338345
});
339346
}
340347

341-
export async function setup(contest_id: string, options: { choice: Choices, force?: boolean, contestDirnameFormat?: string, taskDirnameFormat?: string, template?: string | boolean, tests?: boolean }) {
348+
export async function setup(contest_id: string, options: {choice: Choices, force?: boolean, contestDirnameFormat?: string, taskDirnameFormat?: string, template?: string | boolean, tests?: boolean}) {
342349
try {
343350
const template = await getTemplateFromOption(options.template);
344351
const {contest} = await init(contest_id, template, options);
@@ -349,7 +356,7 @@ export async function setup(contest_id: string, options: { choice: Choices, forc
349356
}
350357
}
351358

352-
export async function add(options: { choice?: Choices, force?: boolean, taskDirnameFormat?: string, template?: string | boolean, tests?: boolean }) {
359+
export async function add(options: {choice?: Choices, force?: boolean, taskDirnameFormat?: string, template?: string | boolean, tests?: boolean}) {
353360
try {
354361
const {path, data} = await findProjectJSON();
355362
const {contest, tasks} = data;
@@ -389,7 +396,7 @@ export async function add(options: { choice?: Choices, force?: boolean, taskDirn
389396
}
390397
}
391398

392-
export async function selectTasks(tasks: Array<Task>, choice: Choices, force: boolean = false): Promise<Array<{ index: number, task: Task }>> {
399+
export async function selectTasks(tasks: Array<Task>, choice: Choices, force: boolean = false): Promise<Array<{index: number, task: Task}>> {
393400
switch (choice) {
394401
case "inquire":
395402
return await inquireTasks(tasks, force);
@@ -407,7 +414,7 @@ export async function selectTasks(tasks: Array<Task>, choice: Choices, force: bo
407414
}
408415
}
409416

410-
export async function inquireTasks(tasks: Array<Task>, force: boolean = false): Promise<Array<{ index: number, task: Task }>> {
417+
export async function inquireTasks(tasks: Array<Task>, force: boolean = false): Promise<Array<{index: number, task: Task}>> {
411418
const inquirer = await import("inquirer");
412419
// まだディレクトリが作成されていない問題を一つだけ選択状態にしておく
413420
const next = getNextTask2Install(tasks);
@@ -422,15 +429,15 @@ export async function inquireTasks(tasks: Array<Task>, force: boolean = false):
422429
disabled: !force && task.directory !== undefined ? () => "already installed" : () => false,
423430
checked: next !== null && index === next.index
424431
}))
425-
}]) as { tasks: Array<{ index: number, task: Task }> }).tasks;
432+
}]) as {tasks: Array<{index: number, task: Task}>}).tasks;
426433
}
427434

428435
/**
429436
* まだディレクトリが作成されていない問題のうち、最も上のものを得る
430437
* すべての問題のディレクトリが作成済みの場合はnullを返す
431438
* @param tasks
432439
*/
433-
function getNextTask2Install(tasks: Array<Task>): { index: number, task: Task } | null {
440+
function getNextTask2Install(tasks: Array<Task>): {index: number, task: Task} | null {
434441
for (let i = 0; i < tasks.length; i++) {
435442
if (tasks[i].directory === undefined) {
436443
return {index: i, task: tasks[i]};

0 commit comments

Comments
 (0)