|
1 | | -import {Command} from 'commander'; |
| 1 | +import { Command } from 'commander'; |
2 | 2 | import colors from 'colors'; |
3 | 3 | import moment from 'moment'; |
4 | 4 | import Config from './include/file-config.js'; |
5 | 5 | import Cli from './include/cli.js'; |
6 | 6 | import Tasks from './include/tasks.js'; |
| 7 | +import Fs from './include/filesystem.js'; |
| 8 | +import Frame from './models/frame.js'; |
| 9 | +import inquirer from 'inquirer'; |
| 10 | + |
| 11 | +const listSize = 30; |
| 12 | + |
| 13 | +function column(str, n) { |
| 14 | + if (str.length > n) { |
| 15 | + str = str.substr(0, n - 1) + "…" |
| 16 | + } |
| 17 | + return str.padEnd(n); |
| 18 | +} |
| 19 | + |
| 20 | +function resumeFrame(tasks, frame) { |
| 21 | + tasks.resume(frame) |
| 22 | + .then(frame => console.log(`Starting project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue} ${frame.note?frame.note:''} at ${moment().format('HH:mm').green}`)) |
| 23 | + .catch(error => Cli.error(error)); |
| 24 | +} |
7 | 25 |
|
8 | 26 | function resume() { |
9 | 27 | const resume = new Command('resume', 'resume monitoring time for last stopped record') |
10 | | - .arguments('[project]') |
11 | | - .option('--verbose', 'show verbose output') |
12 | | - .action((aproject, options, program) => { |
| 28 | + .arguments('[project]') |
| 29 | + .option('--verbose', 'show verbose output') |
| 30 | + .option('--ask', 'ask the activity to resume from the last entries, ignoring project') |
| 31 | + .action((project, options, program) => { |
13 | 32 |
|
14 | | -Cli.verbose = program.opts().verbose; |
| 33 | + Cli.verbose = program.opts().verbose; |
15 | 34 |
|
16 | | -let config = new Config(process.cwd()).set('project', program.args[0]), |
17 | | - tasks = new Tasks(config); |
| 35 | + let config = new Config(process.cwd()).set('project', project), |
| 36 | + tasks = new Tasks(config); |
18 | 37 |
|
19 | | -if (!config.get('project')) |
20 | | - Cli.error('No project set'); |
| 38 | + if (!config.get('project')) |
| 39 | + Cli.error('No project set'); |
21 | 40 |
|
22 | | -tasks.resume() |
23 | | - .then(frame => console.log(`Starting project ${config.get('project').magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue} at ${moment().format('HH:mm').green}`)) |
24 | | - .catch(error => Cli.error(error)); |
25 | | -} |
26 | | -); |
27 | | -return resume; |
| 41 | + let lastFrames = Fs.all(config.frameDir).slice(-listSize); // last listSize frames (one page of inquirer) |
| 42 | + lastFrames = lastFrames.map((file) => |
| 43 | + Frame.fromFile(config, Fs.join(config.frameDir, file)) |
| 44 | + ); |
| 45 | + lastFrames = lastFrames.sort((a, b) => moment(a.stop || moment()).isBefore(moment(b.stop || moment())) ? 1 : -1); |
| 46 | + |
| 47 | + if (!options.ask) { |
| 48 | + let project = config.get('project'); |
| 49 | + let filteredFrames = lastFrames.filter(frame => (!project) || frame.project === project); |
| 50 | + resumeFrame(tasks, (filteredFrames.length > 0) ? filteredFrames[0] : undefined); |
| 51 | + } else { |
| 52 | + let lastFramesDetails = lastFrames |
| 53 | + .sort((a, b) => (a.start.isBefore(b.start) ? -1 : 1)) |
| 54 | + .map((frame) => { |
| 55 | + let issue = `${(frame.resource.type + " #" + frame.resource.id).padEnd(20).blue |
| 56 | + }${column(frame.title != null ? frame.title : "", 50)}`; |
| 57 | + return { |
| 58 | + name: |
| 59 | + ` ${frame.id} ${frame.start.clone().format("MMMM Do YYYY HH:mm").green} ${frame.stop ? "to " + frame.stop.clone().format("HH:mm").green : "(running)"}\t` + |
| 60 | + `${column(frame.project, 50).magenta}${issue}${frame.note != null ? frame.note : "" |
| 61 | + }`, |
| 62 | + value: frame, |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + inquirer |
| 68 | + .prompt([ |
| 69 | + { |
| 70 | + type: "list", |
| 71 | + name: "frame", |
| 72 | + message: "Frame?", |
| 73 | + default: lastFramesDetails.length - 1, |
| 74 | + choices: lastFramesDetails, |
| 75 | + pageSize: listSize, |
| 76 | + }, |
| 77 | + ]) |
| 78 | + .then((answer) => { |
| 79 | + resumeFrame(tasks, answer.frame); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + ); |
| 87 | + return resume; |
28 | 88 | } |
29 | 89 |
|
30 | 90 | export default resume; |
0 commit comments