├── index.js ├── executeCommand.js ├── ask.js ├── package.json ├── options.js ├── process.js ├── .gitignore └── readme.md /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | require('./process') 4 | const ask = require('./ask') 5 | const { setOptions } = require('./options') 6 | 7 | const question = process.argv.slice(2).join(" "); 8 | ask(question).then(response => { 9 | setOptions(response || []) 10 | }) 11 | 12 | -------------------------------------------------------------------------------- /executeCommand.js: -------------------------------------------------------------------------------- 1 | const { exec } = require('child_process'); 2 | 3 | function executeCommand(command) { 4 | console.log(`-------------------------------------------------------\n`); 5 | exec(`bash -c '${command}'`, (error, stdout, _) => { 6 | if (error) return console.log('\x1b[31m%s\x1b[0m', error.stack); 7 | console.log(`${stdout}`); 8 | }); 9 | } 10 | 11 | module.exports = executeCommand -------------------------------------------------------------------------------- /ask.js: -------------------------------------------------------------------------------- 1 | async function ask(question) { 2 | try { 3 | const response = await fetch(`https://api.functionj.com/v1/ask?question=${question}`); 4 | if (!response.ok) { 5 | if (response.status === 404) console.log('Resource not found') 6 | else if (response.status === 400) console.log('Please retry'); 7 | else console.log('\x1b[31m%s\x1b[0m', response.statusText); 8 | return false 9 | } else { 10 | return await response.json(); 11 | } 12 | } catch (error) { 13 | console.error('\x1b[31m%s\x1b[0m', 'An error occurred:', error); 14 | } 15 | } 16 | 17 | module.exports = ask 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-j", 3 | "version": "1.1.4", 4 | "repository": { 5 | "type": "git", 6 | "url": "https://github.com/ThisIsHermanCheng/function-J.git" 7 | }, 8 | "description": "Transform plain text instructions into ready-to-use terminal commands. Say goodbye to memorizing complex commands.", 9 | "main": "index.js", 10 | "bin": { 11 | "fj": "./index.js" 12 | }, 13 | "keywords": [ 14 | "ai", 15 | "terminal assistant", 16 | "command", 17 | "terminal" 18 | ], 19 | "author": "Function J", 20 | "license": "ISC", 21 | "dependencies": { 22 | "keypress": "^0.2.1", 23 | "openai": "^4.10.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /options.js: -------------------------------------------------------------------------------- 1 | const options = { 2 | list: [], 3 | index: 0, 4 | selected: '' 5 | } 6 | 7 | function setOptions(newValue) { 8 | options.list = newValue; 9 | displayOptions(); 10 | } 11 | 12 | function setIndex(newValue) { 13 | options.index = newValue; 14 | displayOptions(); 15 | } 16 | 17 | function displayOptions() { 18 | options.selected = options.list[options.index]; 19 | 20 | process.stdout.clearLine(); 21 | process.stdout.cursorTo(0); 22 | 23 | for (let i = 0; i < options.list.length; i++) { 24 | 25 | i === options.index ? 26 | process.stdout.write(`\x1b[42m${options.list[i]}\x1b[0m`) 27 | : process.stdout.write(options.list[i]); 28 | 29 | if (i < options.list.length - 1) { 30 | process.stdout.write(' | '); 31 | } 32 | } 33 | } 34 | 35 | 36 | module.exports = { 37 | options, 38 | setOptions, 39 | setIndex, 40 | } -------------------------------------------------------------------------------- /process.js: -------------------------------------------------------------------------------- 1 | const readline = require('readline'); 2 | const keypress = require('keypress'); 3 | const { setIndex, options } = require('./options') 4 | const executeCommand = require('./executeCommand') 5 | 6 | const rl = readline.createInterface({ 7 | input: process.stdin, 8 | output: process.stdout, 9 | }); 10 | 11 | process.stdout.write(`\x1B[?25l`); 12 | process.on('exit', () => process.stdout.write('\x1B[?25h\n')); 13 | 14 | keypress(process.stdin); 15 | process.stdin.setRawMode(true); 16 | 17 | let inputting = false 18 | 19 | process.stdin.on('keypress', (_, key) => { 20 | if (!key) return 21 | if (key.name === 'tab') setIndex((options.index + 1) % 3) 22 | else if (key.name === 'return') { 23 | 24 | if (inputting) return 25 | 26 | const regex = /\{([^}]+)\}/g; 27 | const matches = []; 28 | let match; 29 | while ((match = regex.exec(options.selected)) !== null) { 30 | matches.push(match[1]); 31 | } 32 | 33 | if (matches.length == 0) { 34 | rl.close(); 35 | executeCommand(options.selected) 36 | inputting = false 37 | } else { 38 | console.log("-------------------------------------------------------") 39 | console.log("Please input the following values") 40 | 41 | inputting = true 42 | currentIndex = 0 43 | 44 | function askQuestion() { 45 | rl.question(matches[currentIndex] + ': ', (answer) => { 46 | options.selected = options.selected.replace(`{${matches[currentIndex]}}`, answer) 47 | currentIndex++; 48 | if (currentIndex < matches.length) { 49 | askQuestion(); 50 | } else { 51 | executeCommand(options.selected) 52 | rl.close(); 53 | } 54 | }); 55 | } 56 | 57 | 58 | askQuestion(); 59 | 60 | } 61 | 62 | 63 | } 64 | }); 65 | 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Function J - Free Terminal Assistant Powered by AI 2 | 3 | Say goodbye to memorizing complex commands 4 | 5 | ## Overview 6 | 7 | Function J is a free terminal assistant powered by AI. It is designed to help you convert plain text instructions into ready-to-use commands. With Function J, you can quickly transform natural language into executable commands, making your terminal tasks more efficient and accessible. 8 | 9 | ## Features 10 | 11 | - **Natural Language to Command Conversion:** Simply type your desired action in plain English, and Function J will quickly provide you with a list of relevant terminal commands. 12 | - **Command Choices:** When Function J generates commands, it presents you with multiple options to choose from, ensuring that you select the command that best suits your needs. 13 | - **Interactive Selection:** Use the tab key to navigate through the list of generated commands and press enter to execute your chosen command. 14 | - **Intuitive Usage:** Designed for users who dislike memorizing long and complex terminal commands or those who frequently perform repetitive tasks. 15 | 16 | ## Installation 17 | 18 | To install Function J, use npm, the Node.js package manager: 19 | 20 | ```bash 21 | npm install -g function-j 22 | ``` 23 | 24 | ## Getting Started 25 | 26 | To start using Function J, open your terminal and simply prefix your instructions with 'fj'. 27 | 28 | For example: 29 | 30 | ``` 31 | fj show containers 32 | ``` 33 | 34 | Function J will provide a list of command options. Select the most suitable command using the `tab` key and execute it by pressing `enter`. 35 | 36 | ``` 37 | docker ps | docker container ls | docker ps -a 38 | ``` 39 | 40 | For some cases, user input is needed within the command options containing curly braces. 41 | 42 | ``` 43 | fj creat file 44 | ``` 45 | 46 | ``` 47 | touch {file_name} | echo > {file_name} | cat > {file_name} 48 | ------------------------------------------------------- 49 | Please input the following values 50 | file_name: 51 | ``` 52 | 53 | ## Demo 54 | 55 | ![Animation](https://github.com/ThisIsHermanCheng/function-J/assets/45646023/009da273-44d8-4f5c-afcb-d6c26001c885) 56 | 57 | [👉 Try it online 👈](https://functionj.com) 58 | 59 | ## Why Use Function J? 60 | 61 | **_Dislike Memorizing Commands_** 62 | 63 | Tired of trying to remember lengthy and intricate terminal commands. 64 | Frequently Forget Commands: Find themselves forgetting commands, even if they've used them multiple times. 65 | 66 | **_Prefer Easy Access to Commands_** 67 | 68 | Don't want to spend time searching online for command syntax. 69 | Question Command Options: Want clarity on command parameters like '-h' and what they stand for (help, host, header, etc.). 70 | 71 | **_Keyboard-Friendly Shortcut_** 72 | 73 | The choice of 'fj' as the prefix for Function J was intentional. These keys are conveniently located on the keyboard, making it effortless to access this powerful terminal assistant. 74 | 75 | ![keyboard](https://github.com/ThisIsHermanCheng/function-J/assets/45646023/8483cceb-828b-450f-84fe-e9057027ff2b) 76 | 77 | --- 78 | 79 | Now, say goodbye to the hassle of memorizing complex commands and enjoy a more straightforward way to interact with your terminal using Function J! 80 | 81 | Happy terminal navigating with Function J! 🚀 82 | --------------------------------------------------------------------------------