├── .npmrc ├── .gitattributes ├── .gitignore ├── screenshot.gif ├── tsconfig.json ├── .editorconfig ├── test.tsx ├── .github └── workflows │ └── main.yml ├── license ├── readme.md ├── source ├── index.tsx ├── cli.tsx └── ui.tsx └── package.json /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | /distribution 4 | /.tsimp 5 | -------------------------------------------------------------------------------- /screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/emoj/HEAD/screenshot.gif -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@sindresorhus/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "distribution", 5 | "jsx": "react" 6 | }, 7 | "include": [ 8 | "source" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | Use the left/right or 1..9 keys during live search to select the emoji 38 | Press Tab to copy emoji and continue searching (Enter/1-9 to exit) 39 | ``` 40 | 41 | ## Related 42 | 43 | - [alfred-emoj](https://github.com/sindresorhus/alfred-emoj) - Alfred plugin 44 | -------------------------------------------------------------------------------- /source/index.tsx: -------------------------------------------------------------------------------- 1 | import {createRequire} from 'node:module'; 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') as Record; 9 | const unicodeEmojiJson = require('unicode-emoji-json') as Record; 10 | 11 | // This value was picked experimentally. 12 | // Substring search returns a lot of noise for shorter search words. 13 | const minWordLengthForSubstringSearch = 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 < minWordLengthForSubstringSearch ? `^${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 | if (!emojiData) { 33 | continue; 34 | } 35 | 36 | const emojiKeywords = keywordSet[emojiCharacter] ?? []; 37 | 38 | const matches = regex.test(emojiData.name) || emojiKeywords.some((keyword: string) => regex.test(keyword)); 39 | if (matches) { 40 | emojis.push(emojiCharacter); 41 | } 42 | } 43 | 44 | return emojis; 45 | } 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "emoj", 3 | "version": "4.2.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 | "@types/react": "^19.1.12", 56 | "ava": "^6.1.3", 57 | "eslint-config-xo-react": "^0.27.0", 58 | "eslint-plugin-react": "^7.34.1", 59 | "eslint-plugin-react-hooks": "^4.6.2", 60 | "tsimp": "^2.0.11", 61 | "typescript": "^5.4.5", 62 | "xo": "^0.58.0" 63 | }, 64 | "xo": { 65 | "extends": [ 66 | "xo-react" 67 | ], 68 | "rules": { 69 | "react/prop-types": "off", 70 | "react/state-in-constructor": "off", 71 | "@typescript-eslint/indent": "off" 72 | } 73 | }, 74 | "ava": { 75 | "extensions": { 76 | "ts": "module", 77 | "tsx": "module" 78 | }, 79 | "nodeArguments": [ 80 | "--import=tsimp" 81 | ] 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /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, {type SkinToneType} 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 | Press Tab to copy emoji and continue searching (Enter/1-9 to exit) 28 | `, { 29 | importMeta: import.meta, 30 | flags: { 31 | copy: { 32 | type: 'boolean', 33 | shortFlag: 'c', 34 | }, 35 | skinTone: { 36 | type: 'number', 37 | shortFlag: 's', 38 | }, 39 | limit: { 40 | type: 'number', 41 | shortFlag: 'l', 42 | }, 43 | }, 44 | }); 45 | 46 | const config = new Conf({ 47 | projectName: 'emoj', 48 | defaults: { 49 | skinNumber: 0, 50 | }, 51 | }); 52 | 53 | if (cli.flags.skinTone !== undefined) { 54 | config.set('skinNumber', Math.max(0, Math.min(5, cli.flags.skinTone ?? 0))); 55 | } 56 | 57 | const skinNumber = config.get('skinNumber'); 58 | const limit = Math.max(1, cli.flags.limit ?? 7); 59 | 60 | const skinToneNames: SkinToneType[] = [ 61 | 'none', 62 | 'white', 63 | 'creamWhite', 64 | 'lightBrown', 65 | 'brown', 66 | 'darkBrown', 67 | ]; 68 | 69 | if (cli.input.length > 0) { 70 | const searchQuery = cli.input.join(' '); 71 | let emojis = await emoj(searchQuery); 72 | 73 | emojis = emojis 74 | .slice(0, limit) 75 | .map(emoji => skinTone(emoji, skinToneNames[skinNumber]!)); 76 | 77 | console.log(emojis.join(' ')); 78 | 79 | if (cli.flags.copy && emojis[0]) { 80 | clipboardy.writeSync(emojis[0]); 81 | } 82 | } else { 83 | let app: ReturnType; // eslint-disable-line prefer-const 84 | 85 | const onSelectEmoji = (emoji: string) => { 86 | clipboardy.writeSync(emoji); 87 | app.unmount(); 88 | }; 89 | 90 | // Uses `React.createElement` instead of JSX to avoid transpiling this file. 91 | app = render(React.createElement(ui, {skinNumber, limit, onSelectEmoji})); 92 | 93 | await app.waitUntilExit(); 94 | } 95 | -------------------------------------------------------------------------------- /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 clipboardy from 'clipboardy'; 10 | import skinTone, {type SkinToneType} from 'skin-tone'; 11 | import mem from 'mem'; 12 | import emoj from './index.js'; 13 | 14 | // From https://usehooks.com/useDebounce/ 15 | const useDebouncedValue = (value: T, delay: number): T => { // eslint-disable-line @typescript-eslint/comma-dangle 16 | const [debouncedValue, setDebouncedValue] = useState(value); 17 | 18 | useEffect(() => { 19 | const timer = setTimeout(() => { 20 | setDebouncedValue(value); 21 | }, delay); 22 | 23 | return () => { 24 | clearTimeout(timer); 25 | }; 26 | }, [value, delay]); 27 | 28 | return debouncedValue; 29 | }; 30 | 31 | // Limit it to 7 results so not to overwhelm the user 32 | // This also reduces the chance of showing unrelated emojis 33 | const fetch = mem(async (string: string, upperBound: number) => { 34 | const array = await emoj(string); 35 | return array.slice(0, upperBound); 36 | }); 37 | 38 | const stageChecking = 0; 39 | const stageSearch = 1; 40 | const stageCopied = 2; 41 | 42 | type QueryInputProperties = { 43 | readonly query: string; 44 | readonly placeholder: string; 45 | readonly onChange: (value: string) => void; 46 | }; 47 | 48 | function QueryInput({query, placeholder, onChange}: QueryInputProperties) { 49 | return ( 50 | 51 | 52 | ›{' '} 53 | 54 | 55 | 56 | 57 | ); 58 | } 59 | 60 | type CopiedMessageProperties = { 61 | readonly emoji: string | undefined; 62 | }; 63 | 64 | function CopiedMessage({emoji}: CopiedMessageProperties) { 65 | return ( 66 | 67 | {`${emoji} has been copied to the clipboard`} 68 | 69 | ); 70 | } 71 | 72 | const skinToneNames: SkinToneType[] = [ 73 | 'none', 74 | 'white', 75 | 'creamWhite', 76 | 'lightBrown', 77 | 'brown', 78 | 'darkBrown', 79 | ]; 80 | 81 | type SearchProperties = { 82 | readonly query: string; 83 | readonly emojis: string[]; 84 | readonly skinNumber: number; 85 | readonly selectedIndex: number; 86 | readonly isCopiedIndicatorVisible: boolean; 87 | readonly onChangeQuery: (value: string) => void; 88 | }; 89 | 90 | function Search({query, emojis, skinNumber, selectedIndex, isCopiedIndicatorVisible, onChangeQuery}: SearchProperties) { 91 | const list = emojis.map((emoji: string, index: number) => ( 92 | 93 | 94 | {' '} 95 | {skinTone(emoji, skinToneNames[skinNumber]!)} 96 | {' '} 97 | 98 | 99 | )); 100 | 101 | return ( 102 | 103 | 108 | 109 | {list} 110 | {isCopiedIndicatorVisible && ( 111 | 112 | )} 113 | 114 | 115 | ); 116 | } 117 | 118 | type EmojProperties = { 119 | readonly skinNumber: number; 120 | readonly limit: number; 121 | readonly onSelectEmoji: (emoji: string) => void; 122 | }; 123 | 124 | function Emoj({skinNumber: initialSkinNumber, limit, onSelectEmoji}: EmojProperties) { 125 | const {exit} = useApp(); 126 | const [stage, setStage] = useState(stageChecking); 127 | const [query, setQuery] = useState(''); 128 | const [emojis, setEmojis] = useState([]); 129 | const [skinNumber, setSkinNumber] = useState(initialSkinNumber); 130 | const [selectedIndex, setSelectedIndex] = useState(0); 131 | const [selectedEmoji, setSelectedEmoji] = useState(); 132 | const [isCopiedIndicatorVisible, setIsCopiedIndicatorVisible] = useState(false); 133 | 134 | useEffect(() => { 135 | if (selectedEmoji && stage === stageCopied) { 136 | onSelectEmoji(selectedEmoji); 137 | } 138 | }, [selectedEmoji, stage, onSelectEmoji]); 139 | 140 | const changeQuery = useCallback((query: string) => { 141 | setSelectedIndex(0); 142 | setEmojis([]); 143 | setQuery(query); 144 | }, []); 145 | 146 | useEffect(() => { 147 | setStage(stageSearch); 148 | }, []); 149 | 150 | const debouncedQuery = useDebouncedValue(query, 200); 151 | 152 | useEffect(() => { 153 | if (debouncedQuery.length <= 1) { 154 | return undefined; 155 | } 156 | 157 | let isCanceled = false; 158 | 159 | const run = async () => { 160 | const emojis = await fetch(debouncedQuery, limit); 161 | 162 | // Don't update state when this effect was canceled to avoid 163 | // results that don't match the search query 164 | if (!isCanceled) { 165 | setEmojis(emojis); 166 | } 167 | }; 168 | 169 | run(); // eslint-disable-line @typescript-eslint/no-floating-promises 170 | 171 | return () => { 172 | isCanceled = true; 173 | }; 174 | }, [debouncedQuery, limit]); 175 | 176 | const copyEmoji = useCallback((emojiIndex: number, shouldExit = true) => { 177 | const emoji = emojis[emojiIndex]; 178 | if (emoji) { 179 | const styledEmoji = skinTone(emoji, skinToneNames[skinNumber]!); 180 | if (shouldExit) { 181 | setSelectedEmoji(styledEmoji); 182 | setStage(stageCopied); 183 | } else { 184 | // Copy and continue 185 | clipboardy.writeSync(styledEmoji); 186 | setIsCopiedIndicatorVisible(true); 187 | setTimeout(() => { 188 | setIsCopiedIndicatorVisible(false); 189 | }, 1500); 190 | } 191 | } 192 | }, [emojis, skinNumber]); 193 | 194 | const handleSkinToneChange = useCallback((direction: 'up' | 'down') => { 195 | if (direction === 'up' && skinNumber < 5) { 196 | setSkinNumber(skinNumber + 1); 197 | } else if (direction === 'down' && skinNumber > 0) { 198 | setSkinNumber(skinNumber - 1); 199 | } 200 | }, [skinNumber]); 201 | 202 | const handleIndexChange = useCallback((direction: 'left' | 'right') => { 203 | if (direction === 'right') { 204 | setSelectedIndex(selectedIndex < emojis.length - 1 ? selectedIndex + 1 : 0); 205 | } else { 206 | setSelectedIndex(selectedIndex > 0 ? selectedIndex - 1 : emojis.length - 1); 207 | } 208 | }, [selectedIndex, emojis.length]); 209 | 210 | useInput((input, key) => { 211 | if (key.escape || (key.ctrl && input === 'c')) { 212 | exit(); 213 | return; 214 | } 215 | 216 | // Tab key: Copy and continue 217 | if (key.tab && emojis.length > 0) { 218 | copyEmoji(selectedIndex, false); 219 | return; 220 | } 221 | 222 | // Enter: Copy and exit 223 | if (key.return && emojis.length > 0) { 224 | copyEmoji(selectedIndex); 225 | return; 226 | } 227 | 228 | // Number keys: Copy and exit 229 | const numberKey = Number(input); 230 | if (input && numberKey >= 1 && numberKey <= emojis.length) { 231 | copyEmoji(numberKey - 1); 232 | return; 233 | } 234 | 235 | if (query.length <= 1) { 236 | return; 237 | } 238 | 239 | if (key.upArrow) { 240 | handleSkinToneChange('up'); 241 | } else if (key.downArrow) { 242 | handleSkinToneChange('down'); 243 | } else if (key.rightArrow) { 244 | handleIndexChange('right'); 245 | } else if (key.leftArrow) { 246 | handleIndexChange('left'); 247 | } 248 | }); 249 | 250 | return ( 251 | <> 252 | {stage === stageCopied && } 253 | {stage === stageSearch && ( 254 | 262 | )} 263 | 264 | ); 265 | } 266 | 267 | export default Emoj; 268 | --------------------------------------------------------------------------------