Skip to content

Commit 24aa1a8

Browse files
committed
feat(main): support emoji
1 parent 81ffbbd commit 24aa1a8

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

git-consistent

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ const yaml = require('js-yaml')
77
const execSync = require('child_process').execSync
88
const program = require('commander')
99
const prompt = require('prompt-sync')()
10-
const spellcheck = require('nodehun-sentences');
11-
const nodehun = require('nodehun');
10+
const spellcheck = require('nodehun-sentences')
11+
const nodehun = require('nodehun')
12+
const emoji = require('node-emoji')
1213

1314
const templateFileName = '.gitcommit_template'
1415
const definitionsFileName = '.git_consistent'
@@ -62,15 +63,35 @@ const createProgram = (program, definitions, terms) => {
6263
.parse(process.argv)
6364
}
6465

66+
const formatChoices = (choices) => {
67+
const nameMaxLength = _(choices).map('name').map((n) => { return n.length }).max()
68+
const emojiMaxLength = _(choices).map('emoji').map((n) => { return n.length }).max()
69+
70+
aaa= _.map(choices, (choice) => {
71+
const n = _.padEnd(choice.name, nameMaxLength)
72+
const e = emojiMaxLength === 0 ? '' : _.padEnd(choice.emoji, 3)
73+
return [n, e, choice.description].join(" ")
74+
})
75+
return aaa.join("\n")
76+
}
77+
6578
const select = (term, definition) => {
6679
const none = "(none)"
6780
const header = `Select ${term}: `
68-
const choices = _.map(definition.values, function (value) { return `${value.name}\t${value.description}` })
69-
if (!definition.required) choices.unshift(`\t${none}`)
70-
const command = `echo '${_.replace(choices.join("\n"), "'", '"')}' | fzf --reverse --cycle --header="${header}"`
81+
const choices = _.map(definition.values, function (value) {
82+
const name = value.name
83+
const description = value.description
84+
let emojiStr = ''
85+
86+
if (/^:.+:$/.test(name) && emoji.hasEmoji(name)) emojiStr = emoji.get(name)
87+
return { name: name, emoji: emojiStr, description: description }
88+
})
89+
if (!definition.required) choices.unshift({ name: '', emoji: '', description: none })
90+
91+
const command = `echo '${_.replace(formatChoices(choices), "'", '"')}' | fzf --reverse --cycle --header="${header}"`
7192

7293
const selectedValue = execSync(command, { stdio: ['inherit', 'pipe', 'inherit'], shell: true }).toString().trim()
73-
const value = selectedValue.split("\t")[0]
94+
const value = selectedValue.split(" ")[0]
7495
const v = value === none ? '' : value
7596
console.log(`${header}${value}`)
7697
return v

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dictionary-en-us": "^2.0.0",
2424
"js-yaml": "^3.10.0",
2525
"lodash": "^4.17.5",
26+
"node-emoji": "^1.8.1",
2627
"nodehun": "^2.0.11",
2728
"nodehun-sentences": "^1.0.4",
2829
"prompt-sync": "^4.1.5"

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,20 @@ js-yaml@^3.10.0:
4141
argparse "^1.0.7"
4242
esprima "^4.0.0"
4343

44+
lodash.toarray@^4.4.0:
45+
version "4.4.0"
46+
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"
47+
4448
lodash@^4.14.0, lodash@^4.17.5:
4549
version "4.17.5"
4650
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
4751

52+
node-emoji@^1.8.1:
53+
version "1.8.1"
54+
resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.8.1.tgz#6eec6bfb07421e2148c75c6bba72421f8530a826"
55+
dependencies:
56+
lodash.toarray "^4.4.0"
57+
4858
nodehun-sentences@^1.0.4:
4959
version "1.0.4"
5060
resolved "https://registry.yarnpkg.com/nodehun-sentences/-/nodehun-sentences-1.0.4.tgz#a4aef2d7707d7d568133806e957a6a4ab9eb9106"

0 commit comments

Comments
 (0)