From 24aa1a8dd77864c6f21391a5dba9c5dbd288e469 Mon Sep 17 00:00:00 2001 From: isuke Date: Sun, 11 Feb 2018 22:19:24 +0900 Subject: [PATCH] feat(main): support emoji --- git-consistent | 33 +++++++++++++++++++++++++++------ package.json | 1 + yarn.lock | 10 ++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/git-consistent b/git-consistent index 6c45ef8..bf591c1 100755 --- a/git-consistent +++ b/git-consistent @@ -7,8 +7,9 @@ const yaml = require('js-yaml') const execSync = require('child_process').execSync const program = require('commander') const prompt = require('prompt-sync')() -const spellcheck = require('nodehun-sentences'); -const nodehun = require('nodehun'); +const spellcheck = require('nodehun-sentences') +const nodehun = require('nodehun') +const emoji = require('node-emoji') const templateFileName = '.gitcommit_template' const definitionsFileName = '.git_consistent' @@ -62,15 +63,35 @@ const createProgram = (program, definitions, terms) => { .parse(process.argv) } +const formatChoices = (choices) => { + const nameMaxLength = _(choices).map('name').map((n) => { return n.length }).max() + const emojiMaxLength = _(choices).map('emoji').map((n) => { return n.length }).max() + + aaa= _.map(choices, (choice) => { + const n = _.padEnd(choice.name, nameMaxLength) + const e = emojiMaxLength === 0 ? '' : _.padEnd(choice.emoji, 3) + return [n, e, choice.description].join(" ") + }) + return aaa.join("\n") +} + const select = (term, definition) => { const none = "(none)" const header = `Select ${term}: ` - const choices = _.map(definition.values, function (value) { return `${value.name}\t${value.description}` }) - if (!definition.required) choices.unshift(`\t${none}`) - const command = `echo '${_.replace(choices.join("\n"), "'", '"')}' | fzf --reverse --cycle --header="${header}"` + const choices = _.map(definition.values, function (value) { + const name = value.name + const description = value.description + let emojiStr = '' + + if (/^:.+:$/.test(name) && emoji.hasEmoji(name)) emojiStr = emoji.get(name) + return { name: name, emoji: emojiStr, description: description } + }) + if (!definition.required) choices.unshift({ name: '', emoji: '', description: none }) + + const command = `echo '${_.replace(formatChoices(choices), "'", '"')}' | fzf --reverse --cycle --header="${header}"` const selectedValue = execSync(command, { stdio: ['inherit', 'pipe', 'inherit'], shell: true }).toString().trim() - const value = selectedValue.split("\t")[0] + const value = selectedValue.split(" ")[0] const v = value === none ? '' : value console.log(`${header}${value}`) return v diff --git a/package.json b/package.json index 4268840..1d99747 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dictionary-en-us": "^2.0.0", "js-yaml": "^3.10.0", "lodash": "^4.17.5", + "node-emoji": "^1.8.1", "nodehun": "^2.0.11", "nodehun-sentences": "^1.0.4", "prompt-sync": "^4.1.5" diff --git a/yarn.lock b/yarn.lock index 61ae5fc..566efde 100644 --- a/yarn.lock +++ b/yarn.lock @@ -41,10 +41,20 @@ js-yaml@^3.10.0: argparse "^1.0.7" esprima "^4.0.0" +lodash.toarray@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561" + lodash@^4.14.0, lodash@^4.17.5: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" +node-emoji@^1.8.1: + version "1.8.1" + resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.8.1.tgz#6eec6bfb07421e2148c75c6bba72421f8530a826" + dependencies: + lodash.toarray "^4.4.0" + nodehun-sentences@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/nodehun-sentences/-/nodehun-sentences-1.0.4.tgz#a4aef2d7707d7d568133806e957a6a4ab9eb9106"