├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── license ├── package.json ├── readme.md ├── screenshot.gif ├── source ├── cli.tsx ├── index.tsx └── ui.tsx ├── test.tsx └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | steps: 15 | - uses: actions/checkout@v4 16 | - uses: actions/setup-node@v4 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | /distribution 4 | /.tsimp 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "emoj", 3 | "version": "4.1.0", 4 | "description": "Find relevant emoji from text on the command-line", 5 | "license": "MIT", 6 | "repository": "sindresorhus/emoj", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "bin": "./distribution/cli.js", 15 | "engines": { 16 | "node": ">=18" 17 | }, 18 | "scripts": { 19 | "build": "tsc", 20 | "prepublish": "npm run build", 21 | "pretest": "npm run build", 22 | "test": "xo && ava" 23 | }, 24 | "files": [ 25 | "distribution" 26 | ], 27 | "keywords": [ 28 | "cli-app", 29 | "cli", 30 | "emoji", 31 | "emojis", 32 | "emoj", 33 | "emoticon", 34 | "search", 35 | "find", 36 | "matching", 37 | "relevant", 38 | "neural", 39 | "networks" 40 | ], 41 | "dependencies": { 42 | "clipboardy": "^4.0.0", 43 | "conf": "^12.0.0", 44 | "emojilib": "^3.0.12", 45 | "ink": "^5.0.0", 46 | "ink-text-input": "^6.0.0", 47 | "mem": "^9.0.2", 48 | "meow": "^13.2.0", 49 | "react": "^18.3.1", 50 | "skin-tone": "^4.0.0", 51 | "unicode-emoji-json": "^0.6.0" 52 | }, 53 | "devDependencies": { 54 | "@sindresorhus/tsconfig": "^5.0.0", 55 | "ava": "^6.1.3", 56 | "eslint-config-xo-react": "^0.27.0", 57 | "eslint-plugin-react": "^7.34.1", 58 | "eslint-plugin-react-hooks": "^4.6.2", 59 | "tsimp": "^2.0.11", 60 | "typescript": "^5.4.5", 61 | "xo": "^0.58.0" 62 | }, 63 | "xo": { 64 | "extends": [ 65 | "xo-react" 66 | ], 67 | "rules": { 68 | "react/prop-types": "off", 69 | "react/state-in-constructor": "off", 70 | "@typescript-eslint/prefer-nullish-coalescing": "off", 71 | "@typescript-eslint/no-unsafe-assignment": "off", 72 | "@typescript-eslint/no-unsafe-call": "off", 73 | "@typescript-eslint/no-unsafe-argument": "off", 74 | "@typescript-eslint/naming-convention": "off", 75 | "@typescript-eslint/no-unsafe-return": "off", 76 | "@typescript-eslint/restrict-plus-operands": "off" 77 | } 78 | }, 79 | "ava": { 80 | "extensions": { 81 | "ts": "module", 82 | "tsx": "module" 83 | }, 84 | "nodeArguments": [ 85 | "--import=tsimp" 86 | ] 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # emoj 2 | 3 | > Find relevant emoji from text on the command-line 4 | 5 | 6 | 7 | Uses a local emoji database. 8 | 9 | ## Install 10 | 11 | Ensure you have [Node.js 18+](https://nodejs.org) installed, and then run the following: 12 | 13 | ```sh 14 | npm install --global emoj 15 | ``` 16 | 17 | Works best on macOS and Linux. Older Linux distributions don't support color emoji in the terminal, but newer ones (like Ubuntu 18.04 and Fedora 28) do. On Linux, I would recommend installing [Emoji One](https://github.com/eosrei/emojione-color-font#install-on-linux) for full emoji coverage. [Doesn't really work on Windows.](https://github.com/sindresorhus/emoj/issues/5) 18 | 19 | ## Usage 20 | 21 | ``` 22 | $ emoj --help 23 | 24 | Usage 25 | $ emoj [text] 26 | 27 | Example 28 | $ emoj 'i love unicorns' 29 | 🦄 🎠 🐴 🐎 ❤ ✨ 🌈 30 | 31 | Options 32 | --copy -c Copy the first emoji to the clipboard 33 | --skin-tone -s Set and persist the default emoji skin tone (0 to 5) 34 | 35 | Run it without arguments to enter the live search 36 | Use the up/down keys during live search to change the skin tone 37 | ``` 38 | 39 | ## Related 40 | 41 | - [alfred-emoj](https://github.com/sindresorhus/alfred-emoj) - Alfred plugin 42 | -------------------------------------------------------------------------------- /screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/emoj/ff308a070227a74680f86ad23cb11da6e1f77909/screenshot.gif -------------------------------------------------------------------------------- /source/cli.tsx: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import meow from 'meow'; 3 | import React from 'react'; 4 | import {render} from 'ink'; 5 | import clipboardy from 'clipboardy'; 6 | import skinTone from 'skin-tone'; 7 | import Conf from 'conf'; 8 | import ui from './ui.js'; 9 | import emoj from './index.js'; 10 | 11 | const cli = meow(` 12 | Usage 13 | $ emoj [text] 14 | 15 | Example 16 | $ emoj 'i love unicorns' 17 | 🦄 🎠 🐴 🐎 ❤ ✨ 🌈 18 | 19 | Options 20 | --copy -c Copy the first emoji to the clipboard 21 | --skin-tone -s Set and persist the default emoji skin tone (0 to 5) 22 | --limit -l Maximum number of emojis to display (default: 7) 23 | 24 | Run it without arguments to enter the live search 25 | Use the up/down keys during live search to change the skin tone 26 | Use the left/right or 1..9 keys during live search to select the emoji 27 | `, { 28 | importMeta: import.meta, 29 | flags: { 30 | copy: { 31 | type: 'boolean', 32 | shortFlag: 'c', 33 | }, 34 | skinTone: { 35 | type: 'number', 36 | shortFlag: 's', 37 | }, 38 | limit: { 39 | type: 'number', 40 | shortFlag: 'l', 41 | }, 42 | }, 43 | }); 44 | 45 | const config = new Conf({ 46 | projectName: 'emoj', 47 | defaults: { 48 | skinNumber: 0, 49 | }, 50 | }); 51 | 52 | if (cli.flags.skinTone !== undefined) { 53 | config.set('skinNumber', Math.max(0, Math.min(5, cli.flags.skinTone ?? 0))); 54 | } 55 | 56 | const skinNumber = config.get('skinNumber'); 57 | const limit = Math.max(1, cli.flags.limit ?? 7); 58 | 59 | // TODO: skin-tone package should export this. 60 | const skinToneNames = [ 61 | 'none', 62 | 'white', 63 | 'creamWhite', 64 | 'lightBrown', 65 | 'brown', 66 | 'darkBrown', 67 | ] as const; 68 | 69 | if (cli.input.length > 0) { 70 | let emojis = await emoj(cli.input[0]); 71 | 72 | emojis = emojis 73 | .slice(0, limit) 74 | .map(emoji => skinTone(emoji, skinToneNames[skinNumber])); 75 | 76 | console.log(emojis.join(' ')); 77 | 78 | if (cli.flags.copy) { 79 | clipboardy.writeSync(emojis[0]); 80 | } 81 | } else { 82 | let app: any; // eslint-disable-line prefer-const 83 | 84 | const onSelectEmoji = emoji => { 85 | clipboardy.writeSync(emoji); 86 | app.unmount(); 87 | }; 88 | 89 | // Uses `React.createElement` instead of JSX to avoid transpiling this file. 90 | app = render(React.createElement(ui, {skinNumber, limit, onSelectEmoji})); 91 | 92 | await app.waitUntilExit(); 93 | } 94 | -------------------------------------------------------------------------------- /source/index.tsx: -------------------------------------------------------------------------------- 1 | import {createRequire} from 'node:module'; // eslint-disable-line import/order 2 | 3 | /// import keywordSet from 'emojilib'; 4 | // import data from 'unicode-emoji-json'; 5 | 6 | const require = createRequire(import.meta.url); 7 | 8 | const keywordSet = require('emojilib'); 9 | const unicodeEmojiJson = require('unicode-emoji-json'); 10 | 11 | // This value was picked experimentally. 12 | // Substring search returns a lot of noise for shorter search words. 13 | const MIN_WORD_LENGTH_FOR_SUBSTRING_SEARCH = 4; 14 | 15 | // We keep this async in case we need to to become async in the future 16 | export default async function getEmojilibEmojis(searchQuery: string): Promise { 17 | const regexSource = searchQuery.toLowerCase().split(/\s/g) 18 | .map(v => v.replaceAll(/\W/g, '')) 19 | .filter(v => v.length > 0) 20 | .map(v => v.length < MIN_WORD_LENGTH_FOR_SUBSTRING_SEARCH ? `^${v}$` : v) 21 | .join('|'); 22 | 23 | if (regexSource.length === 0) { 24 | return []; 25 | } 26 | 27 | const regex = new RegExp(regexSource); 28 | const emojis: string[] = []; 29 | 30 | for (const emojiCharacter of Object.keys(unicodeEmojiJson)) { 31 | const emojiData = unicodeEmojiJson[emojiCharacter]; 32 | const emojiKeywords = keywordSet[emojiCharacter] ?? []; 33 | 34 | const matches = regex.test(emojiData.name) || emojiKeywords.some(keyword => regex.test(keyword)); 35 | if (matches) { 36 | emojis.push(emojiCharacter); 37 | } 38 | } 39 | 40 | return emojis; 41 | } 42 | -------------------------------------------------------------------------------- /source/ui.tsx: -------------------------------------------------------------------------------- 1 | import React, {useState, useCallback, useEffect} from 'react'; 2 | import { 3 | Box, 4 | Text, 5 | useApp, 6 | useInput, 7 | } from 'ink'; 8 | import TextInput from 'ink-text-input'; 9 | import skinTone from 'skin-tone'; 10 | import mem from 'mem'; 11 | import emoj from './index.js'; 12 | 13 | // From https://usehooks.com/useDebounce/ 14 | const useDebouncedValue = (value: T, delay: number): T => { // eslint-disable-line @typescript-eslint/comma-dangle 15 | const [debouncedValue, setDebouncedValue] = useState(value); 16 | 17 | useEffect(() => { 18 | const timer = setTimeout(() => { 19 | setDebouncedValue(value); 20 | }, delay); 21 | 22 | return () => { 23 | clearTimeout(timer); 24 | }; 25 | }, [value, delay]); 26 | 27 | return debouncedValue; 28 | }; 29 | 30 | // Limit it to 7 results so not to overwhelm the user 31 | // This also reduces the chance of showing unrelated emojis 32 | const fetch = mem(async (string, upperBound) => { 33 | const array = await emoj(string); 34 | return array.slice(0, upperBound); 35 | }); 36 | 37 | const STAGE_CHECKING = 0; 38 | const STAGE_SEARCH = 1; 39 | const STAGE_COPIED = 2; 40 | 41 | function QueryInput({query, placeholder, onChange}) { 42 | return ( 43 | 44 | 45 | ›{' '} 46 | 47 | 48 | 49 | 50 | ); 51 | } 52 | 53 | function CopiedMessage({emoji}) { 54 | return ( 55 | 56 | {`${emoji} has been copied to the clipboard`} 57 | 58 | ); 59 | } 60 | 61 | const skinToneNames = [ 62 | 'none', 63 | 'white', 64 | 'creamWhite', 65 | 'lightBrown', 66 | 'brown', 67 | 'darkBrown', 68 | ] as const; 69 | 70 | function Search({query, emojis, skinNumber, selectedIndex, onChangeQuery}) { 71 | const list = emojis.map((emoji, index) => ( 72 | 73 | 74 | {' '} 75 | {skinTone(emoji, skinToneNames[skinNumber])} 76 | {' '} 77 | 78 | 79 | )); 80 | 81 | return ( 82 | 83 | 88 | 89 | {list} 90 | 91 | 92 | ); 93 | } 94 | 95 | function Emoj({skinNumber: initialSkinNumber, limit, onSelectEmoji}) { 96 | const {exit} = useApp(); 97 | const [stage, setStage] = useState(STAGE_CHECKING); 98 | const [query, setQuery] = useState(''); 99 | const [emojis, setEmojis] = useState([]); 100 | const [skinNumber, setSkinNumber] = useState(initialSkinNumber); 101 | const [selectedIndex, setSelectedIndex] = useState(0); 102 | const [selectedEmoji, setSelectedEmoji] = useState(); 103 | 104 | useEffect(() => { 105 | if (selectedEmoji && stage === STAGE_COPIED) { 106 | onSelectEmoji(selectedEmoji); 107 | } 108 | }, [selectedEmoji, stage, onSelectEmoji]); 109 | 110 | const changeQuery = useCallback(query => { 111 | setSelectedIndex(0); 112 | setEmojis([]); 113 | setQuery(query); 114 | }, []); 115 | 116 | useEffect(() => { 117 | setStage(STAGE_SEARCH); 118 | }, []); 119 | 120 | const debouncedQuery = useDebouncedValue(query, 200); 121 | 122 | useEffect(() => { 123 | if (debouncedQuery.length <= 1) { 124 | return undefined; 125 | } 126 | 127 | let isCanceled = false; 128 | 129 | const run = async () => { 130 | const emojis = await fetch(debouncedQuery, limit); 131 | 132 | // Don't update state when this effect was canceled to avoid 133 | // results that don't match the search query 134 | if (!isCanceled) { 135 | setEmojis(emojis); 136 | } 137 | }; 138 | 139 | run(); // eslint-disable-line @typescript-eslint/no-floating-promises 140 | 141 | return () => { 142 | isCanceled = true; 143 | }; 144 | }, [debouncedQuery]); 145 | 146 | useInput((input, key) => { 147 | if (key.escape || (key.ctrl && input === 'c')) { 148 | exit(); 149 | return; 150 | } 151 | 152 | if (key.return) { 153 | if (emojis.length > 0) { 154 | setSelectedEmoji(skinTone(emojis[selectedIndex], skinToneNames[skinNumber])); 155 | setStage(STAGE_COPIED); 156 | } 157 | 158 | return; 159 | } 160 | 161 | // Select emoji by typing a number 162 | // Catch all 10 keys, but handle only the same amount of keys 163 | // as there are currently emojis 164 | const numberKey = Number(input); 165 | if (input && numberKey >= 0 && numberKey <= 9) { 166 | if (numberKey >= 1 && numberKey <= emojis.length) { 167 | setSelectedEmoji(skinTone(emojis[numberKey - 1], skinToneNames[skinNumber])); 168 | setStage(STAGE_COPIED); 169 | } 170 | 171 | return; 172 | } 173 | 174 | // Filter out all ansi sequences except the up/down keys which change the skin tone 175 | // and left/right keys which select emoji inside a list 176 | const isArrowKey = key.upArrow || key.downArrow || key.leftArrow || key.rightArrow; 177 | 178 | if (!isArrowKey || query.length <= 1) { 179 | return; 180 | } 181 | 182 | if (key.upArrow && skinNumber < 5) { 183 | setSkinNumber(skinNumber + 1); 184 | } 185 | 186 | if (key.downArrow && skinNumber > 0) { 187 | setSkinNumber(skinNumber - 1); 188 | } 189 | 190 | if (key.rightArrow) { 191 | if (selectedIndex < emojis.length - 1) { 192 | setSelectedIndex(selectedIndex + 1); 193 | } else { 194 | setSelectedIndex(0); 195 | } 196 | } 197 | 198 | if (key.leftArrow) { 199 | if (selectedIndex > 0) { 200 | setSelectedIndex(selectedIndex - 1); 201 | } else { 202 | setSelectedIndex(emojis.length - 1); 203 | } 204 | } 205 | }); 206 | 207 | return ( 208 | <> 209 | {stage === STAGE_COPIED && } 210 | {stage === STAGE_SEARCH && ( 211 | 218 | )} 219 | 220 | ); 221 | } 222 | 223 | export default Emoj; 224 | -------------------------------------------------------------------------------- /test.tsx: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import emoj from './source/index.js'; 3 | 4 | test('main', async t => { 5 | const [unicornEmoji] = await emoj('unicorn'); 6 | t.is(unicornEmoji, '🦄'); 7 | }); 8 | 9 | test('local database emojis', async t => { 10 | const result1 = await emoj('crossed'); 11 | const result2 = await emoj('drool'); 12 | t.true(result1.includes('🤞')); 13 | t.true(result2.includes('🤤')); 14 | }); 15 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@sindresorhus/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "distribution", 5 | "strict": false, 6 | "jsx": "react" 7 | }, 8 | "include": [ 9 | "source" 10 | ] 11 | } 12 | --------------------------------------------------------------------------------