Skip to content

Commit

Permalink
feat(amend): auto-completed provides list of 5 last entries of the user
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Apr 19, 2019
1 parent 24ecd03 commit 87309f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
46 changes: 34 additions & 12 deletions source/lib/auto-complete/completions/amend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/env node
'use strict';

const util = require('util');

const config = require('./../../../config.js');
const miteApi = require('./../../mite-api')(config.get());

const NOTE_MAX_LENGTH = (process.stdout.columns || 80) - 20;

/**
* https://www.npmjs.com/package/tabtab#3-parsing-env
*
Expand All @@ -12,17 +19,32 @@
* @param {string} env.line - the current complete input line in the cli
* @returns {Promise<Array<string>>}
*/
module.exports = async ({ words }) => {
if (words < 3) {
return [
{
name: '--help',
description: 'show help message'
},
{
name: '--editor',
description: 'open $EDITOR for editing the entry’s note'
module.exports = async ({ line, words }) => {
const defaults = [
(words < 3 ? {
name: '--help',
description: 'show help message'
} : undefined),
(line.indexOf('--editor') === -1 ? {
name: '--editor',
description: 'open $EDITOR for editing the entry’s note'
} : undefined)
];

// try to find the latest entries created by the current user and propose the
// ids of these
return miteApi.getMyRecentTimeEntries()
.then(timeEntries => timeEntries.map(entry => {
let note = entry.note || '[no note]';
if (note.length > NOTE_MAX_LENGTH) {
note = note.substr(0, NOTE_MAX_LENGTH - 1) + '…';
}
];
}
return {
name: String(entry.id),
description: `${entry.date_at} ${note}`
};
}))
.then(options => {
return [].concat([], options, defaults);
});
};
14 changes: 14 additions & 0 deletions source/lib/mite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function miteApiWrapper(config) {
return util.promisify(this.mite.getMyself)().then(data => data.user);
},

getMyRecentTimeEntries: async function() {
return this.getMyself()
.then(me => {
const options = {
user_id: me ? me.id : undefined,
limit: 5,
sort: 'date_at',
direction: 'desc',
};
return util.promisify(this.mite.getTimeEntries)(options);
})
.then(items => items.map(item => item.time_entry));
},

sort: function(items, attribute) {
// @TODO add assertions
return items.sort((a, b) => {
Expand Down

0 comments on commit 87309f4

Please sign in to comment.