Skip to content

Commit

Permalink
feat(main): support emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
isuke committed Feb 11, 2018
1 parent 81ffbbd commit 24aa1a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
33 changes: 27 additions & 6 deletions git-consistent
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 24aa1a8

Please sign in to comment.