├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── appveyor.yml ├── cli.js ├── contributing.md ├── demo.gif ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.md] 11 | trim_trailing_whitespace = false 12 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | "globals": { 13 | "document": false, 14 | "navigator": false, 15 | "window": false 16 | }, 17 | "rules": { 18 | "accessor-pairs": 2, 19 | "arrow-spacing": [ 20 | 2, 21 | { 22 | "before": true, 23 | "after": true 24 | } 25 | ], 26 | "block-spacing": [ 27 | 2, 28 | "always" 29 | ], 30 | "brace-style": [ 31 | 2, 32 | "1tbs", 33 | { 34 | "allowSingleLine": true 35 | } 36 | ], 37 | "comma-dangle": [ 38 | 2, 39 | "never" 40 | ], 41 | "comma-spacing": [ 42 | 2, 43 | { 44 | "before": false, 45 | "after": true 46 | } 47 | ], 48 | "comma-style": [ 49 | 2, 50 | "last" 51 | ], 52 | "constructor-super": 2, 53 | "curly": [ 54 | 2, 55 | "multi-line" 56 | ], 57 | "dot-location": [ 58 | 2, 59 | "property" 60 | ], 61 | "eol-last": 2, 62 | "eqeqeq": [ 63 | 2, 64 | "allow-null" 65 | ], 66 | "generator-star-spacing": [ 67 | 2, 68 | { 69 | "before": true, 70 | "after": true 71 | } 72 | ], 73 | "handle-callback-err": [ 74 | 2, 75 | "^(err|error)$" 76 | ], 77 | "indent": [ 78 | 2, 79 | 2, 80 | { 81 | "SwitchCase": 1 82 | } 83 | ], 84 | "key-spacing": [ 85 | 2, 86 | { 87 | "beforeColon": false, 88 | "afterColon": true 89 | } 90 | ], 91 | "keyword-spacing": [ 92 | 2, 93 | { 94 | "before": true, 95 | "after": true 96 | } 97 | ], 98 | "new-cap": [ 99 | 2, 100 | { 101 | "newIsCap": true, 102 | "capIsNew": false 103 | } 104 | ], 105 | "new-parens": 2, 106 | "no-array-constructor": 2, 107 | "no-caller": 2, 108 | "no-class-assign": 2, 109 | "no-cond-assign": 2, 110 | "no-const-assign": 2, 111 | "no-control-regex": 2, 112 | "no-debugger": 2, 113 | "no-delete-var": 2, 114 | "no-dupe-args": 2, 115 | "no-dupe-class-members": 2, 116 | "no-dupe-keys": 2, 117 | "no-duplicate-case": 2, 118 | "no-empty-character-class": 2, 119 | "no-eval": 2, 120 | "no-ex-assign": 2, 121 | "no-extend-native": 2, 122 | "no-extra-bind": 2, 123 | "no-extra-boolean-cast": 2, 124 | "no-extra-parens": [ 125 | 2, 126 | "functions" 127 | ], 128 | "no-fallthrough": 2, 129 | "no-floating-decimal": 2, 130 | "no-func-assign": 2, 131 | "no-implied-eval": 2, 132 | "no-inner-declarations": [ 133 | 2, 134 | "functions" 135 | ], 136 | "no-invalid-regexp": 2, 137 | "no-irregular-whitespace": 2, 138 | "no-iterator": 2, 139 | "no-label-var": 2, 140 | "no-labels": 2, 141 | "no-lone-blocks": 2, 142 | "no-mixed-spaces-and-tabs": 2, 143 | "no-multi-spaces": 2, 144 | "no-multi-str": 2, 145 | "no-multiple-empty-lines": [ 146 | 2, 147 | { 148 | "max": 1 149 | } 150 | ], 151 | "no-native-reassign": 0, 152 | "no-negated-in-lhs": 2, 153 | "no-new": 2, 154 | "no-new-func": 2, 155 | "no-new-object": 2, 156 | "no-new-require": 2, 157 | "no-new-wrappers": 2, 158 | "no-obj-calls": 2, 159 | "no-octal": 2, 160 | "no-octal-escape": 2, 161 | "no-proto": 0, 162 | "no-redeclare": 2, 163 | "no-regex-spaces": 2, 164 | "no-return-assign": 2, 165 | "no-self-compare": 2, 166 | "no-sequences": 2, 167 | "no-shadow-restricted-names": 2, 168 | "no-spaced-func": 2, 169 | "no-sparse-arrays": 2, 170 | "no-this-before-super": 2, 171 | "no-throw-literal": 2, 172 | "no-trailing-spaces": 0, 173 | "no-undef": 2, 174 | "no-undef-init": 2, 175 | "no-unexpected-multiline": 2, 176 | "no-unneeded-ternary": [ 177 | 2, 178 | { 179 | "defaultAssignment": false 180 | } 181 | ], 182 | "no-unreachable": 2, 183 | "no-unused-vars": [ 184 | 2, 185 | { 186 | "vars": "all", 187 | "args": "none" 188 | } 189 | ], 190 | "no-useless-call": 0, 191 | "no-with": 2, 192 | "one-var": [ 193 | 0, 194 | { 195 | "initialized": "never" 196 | } 197 | ], 198 | "operator-linebreak": [ 199 | 0, 200 | "after", 201 | { 202 | "overrides": { 203 | "?": "before", 204 | ":": "before" 205 | } 206 | } 207 | ], 208 | "padded-blocks": [ 209 | 0, 210 | "never" 211 | ], 212 | "quotes": [ 213 | 2, 214 | "single", 215 | "avoid-escape" 216 | ], 217 | "radix": 2, 218 | "semi": [ 219 | 2, 220 | "always" 221 | ], 222 | "semi-spacing": [ 223 | 2, 224 | { 225 | "before": false, 226 | "after": true 227 | } 228 | ], 229 | "space-before-blocks": [ 230 | 2, 231 | "always" 232 | ], 233 | "space-before-function-paren": [ 234 | 2, 235 | "never" 236 | ], 237 | "space-in-parens": [ 238 | 2, 239 | "never" 240 | ], 241 | "space-infix-ops": 2, 242 | "space-unary-ops": [ 243 | 2, 244 | { 245 | "words": true, 246 | "nonwords": false 247 | } 248 | ], 249 | "spaced-comment": [ 250 | 0, 251 | "always", 252 | { 253 | "markers": [ 254 | "global", 255 | "globals", 256 | "eslint", 257 | "eslint-disable", 258 | "*package", 259 | "!", 260 | "," 261 | ] 262 | } 263 | ], 264 | "use-isnan": 2, 265 | "valid-typeof": 2, 266 | "wrap-iife": [ 267 | 2, 268 | "any" 269 | ], 270 | "yoda": [ 271 | 2, 272 | "never" 273 | ] 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "6" 5 | - "5" 6 | - "4" 7 | - "0.12" 8 | - "0.10" 9 | matrix: 10 | fast_finish: true 11 | allow_failures: 12 | - node_js: "4" 13 | - node_js: "0.10" 14 | - node_js: "0.12" 15 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## CLI 2 | 3 | When installed globally, this module adds a `snake` command that will start the demo: 4 | 5 | ```sh 6 | $ snake 7 | ``` 8 | 9 |

10 | 11 |

12 | 13 | ## API 14 | 15 | This module also exports a function that may be called manually to start the demo: 16 | 17 | ```js 18 | var snake = require('snake-cli'); 19 | snake(); 20 | ``` 21 | 22 | There are currently no options or configuration. This is just an demo to play around with some [readline-utils][] features. 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Brian Woodward 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # snake-cli [![NPM version](https://img.shields.io/npm/v/snake-cli.svg?style=flat)](https://www.npmjs.com/package/snake-cli) [![NPM downloads](https://img.shields.io/npm/dm/snake-cli.svg?style=flat)](https://npmjs.org/package/snake-cli) [![Linux Build Status](https://img.shields.io/travis/doowb/snake-cli.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/snake-cli) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/snake-cli.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/snake-cli) 2 | 3 | > Experiment / demo using readline-utils to create a simple snake game in the terminal. 4 | 5 | ## Install 6 | 7 | Install globally with [npm](https://www.npmjs.com/) 8 | 9 | ```sh 10 | $ npm install --global snake-cli 11 | ``` 12 | 13 | ## CLI 14 | 15 | When installed globally, this module adds a `snake` command that will start the demo: 16 | 17 | ```sh 18 | $ snake 19 | ``` 20 | 21 |

22 | 23 |

24 | 25 | ## API 26 | 27 | This module also exports a function that may be called manually to start the demo: 28 | 29 | ```js 30 | var snake = require('snake-cli'); 31 | snake(); 32 | ``` 33 | 34 | There are currently no options or configuration. This is just an demo to play around with some [readline-utils](https://github.com/enquirer/readline-utils) features. 35 | 36 | ## About 37 | 38 | ### Related projects 39 | 40 | * [enquirer](https://www.npmjs.com/package/enquirer): Intuitive, plugin-based prompt system for node.js. Much faster and lighter alternative to Inquirer, with all… [more](https://github.com/enquirer/enquirer) | [homepage](https://github.com/enquirer/enquirer "Intuitive, plugin-based prompt system for node.js. Much faster and lighter alternative to Inquirer, with all the same prompt types and more, but without the bloat.") 41 | * [readline-ui](https://www.npmjs.com/package/readline-ui): Create readline interface to use in prompts. | [homepage](https://github.com/enquirer/readline-ui "Create readline interface to use in prompts.") 42 | * [readline-utils](https://www.npmjs.com/package/readline-utils): Readline utils, for moving the cursor, clearing lines, creating a readline interface, and more. | [homepage](https://github.com/enquirer/readline-utils "Readline utils, for moving the cursor, clearing lines, creating a readline interface, and more.") 43 | * [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.") 44 | * [window-size](https://www.npmjs.com/package/window-size): Reliable way to to get the height and width of the terminal/console in a node.js… [more](https://github.com/jonschlinkert/window-size) | [homepage](https://github.com/jonschlinkert/window-size "Reliable way to to get the height and width of the terminal/console in a node.js environment.") 45 | 46 | ### Contributing 47 | 48 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 49 | 50 | Please read the [contributing guide](contributing.md) for avice on opening issues, pull requests, and coding standards. 51 | 52 | ### Building docs 53 | 54 | _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ 55 | 56 | To generate the readme and API documentation with [verb](https://github.com/verbose/verb): 57 | 58 | ```sh 59 | $ npm install -g verb verb-generate-readme && verb 60 | ``` 61 | 62 | ### Running tests 63 | 64 | Install dev dependencies: 65 | 66 | ```sh 67 | $ npm install -d && npm test 68 | ``` 69 | 70 | ### Author 71 | 72 | **Brian Woodward** 73 | 74 | * [github/doowb](https://github.com/doowb) 75 | * [twitter/doowb](http://twitter.com/doowb) 76 | 77 | ### License 78 | 79 | Copyright © 2016, [Brian Woodward](https://github.com/doowb). 80 | Released under the [MIT license](LICENSE). 81 | 82 | *** 83 | 84 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on October 21, 2016._ -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Test against this version of Node.js 2 | environment: 3 | matrix: 4 | # node.js 5 | - nodejs_version: "6.0" 6 | - nodejs_version: "5.0" 7 | - nodejs_version: "4.0" 8 | 9 | # Install scripts. (runs after repo cloning) 10 | install: 11 | # Get the latest stable version of Node.js or io.js 12 | - ps: Install-Product node $env:nodejs_version 13 | # install modules 14 | - npm install 15 | 16 | # Post-install test scripts. 17 | test_script: 18 | # Output useful info for debugging. 19 | - node --version 20 | - npm --version 21 | # run tests 22 | - npm test 23 | 24 | # Don't actually build. 25 | build: off 26 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | require('./')(); 4 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to snake-cli 2 | 3 | First and foremost, thank you! We appreciate that you want to contribute to snake-cli, your time is valuable, and your contributions mean a lot to us. 4 | 5 | **What does "contributing" mean?** 6 | 7 | Creating an issue is the simplest form of contributing to a project. But there are many ways to contribute, including the following: 8 | 9 | - Updating or correcting documentation 10 | - Feature requests 11 | - Bug reports 12 | 13 | If you'd like to learn more about contributing in general, the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) has a lot of useful information. 14 | 15 | **Showing support for snake-cli** 16 | 17 | Please keep in mind that open source software is built by people like you, who spend their free time creating things the rest the community can use. 18 | 19 | Don't have time to contribute? No worries, here are some other ways to show your support for snake-cli: 20 | 21 | - star the [project](https://github.com/doowb/snake-cli) 22 | - tweet your support for snake-cli 23 | 24 | ## Issues 25 | 26 | ### Before creating an issue 27 | 28 | Please try to determine if the issue is caused by an underlying library, and if so, create the issue there. Sometimes this is difficult to know. We only ask that you attempt to give a reasonable attempt to find out. Oftentimes the readme will have advice about where to go to create issues. 29 | 30 | Try to follow these guidelines 31 | 32 | - **Investigate the issue**: 33 | - **Check the readme** - oftentimes you will find notes about creating issues, and where to go depending on the type of issue. 34 | - Create the issue in the appropriate repository. 35 | 36 | ### Creating an issue 37 | 38 | Please be as descriptive as possible when creating an issue. Give us the information we need to successfully answer your question or address your issue by answering the following in your issue: 39 | 40 | - **version**: please note the version of snake-cli are you using 41 | - **extensions, plugins, helpers, etc** (if applicable): please list any extensions you're using 42 | - **error messages**: please paste any error messages into the issue, or a [gist](https://gist.github.com/) 43 | 44 | ## Above and beyond 45 | 46 | Here are some tips for creating idiomatic issues. Taking just a little bit extra time will make your issue easier to read, easier to resolve, more likely to be found by others who have the same or similar issue in the future. 47 | 48 | - read the [Guide to Idiomatic Contributing](https://github.com/jonschlinkert/idiomatic-contributing) 49 | - take some time to learn basic markdown. This [markdown cheatsheet](https://gist.github.com/jonschlinkert/5854601) is super helpful, as is the GitHub guide to [basic markdown](https://help.github.com/articles/markdown-basics/). 50 | - Learn about [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/). And if you want to really go above and beyond, read [mastering markdown](https://guides.github.com/features/mastering-markdown/). 51 | - use backticks to wrap code. This ensures that code will retain its format, making it much more readable to others 52 | - use syntax highlighting by adding the correct language name after the first "code fence" 53 | 54 | 55 | [node-glob]: https://github.com/isaacs/node-glob 56 | [micromatch]: https://github.com/jonschlinkert/micromatch 57 | [so]: http://stackoverflow.com/questions/tagged/snake-cli -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/doowb/snake-cli/f0763c3942b1f1f2ff8f5e5d1f1641ea900ebce1/demo.gif -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var utils = require('readline-utils'); 3 | var repeat = require('repeat-string'); 4 | 5 | module.exports = function() { 6 | var windowSize = require('window-size'); 7 | var size = windowSize.height - 10; 8 | var score = 0; 9 | var head = '^'; 10 | var body = []; 11 | 12 | // borders are from cli-table chars: https://github.com/Automattic/cli-table/blob/master/lib/index.js#L21-L37 13 | var borders = { 14 | 'top': '─', 15 | 'top-mid': '┬', 16 | 'top-left': '┌', 17 | 'top-right': '┐', 18 | 'bottom': '─', 19 | 'bottom-mid': '┴', 20 | 'bottom-left': '└', 21 | 'bottom-right': '┘', 22 | 'left': '│', 23 | 'left-mid': '├', 24 | 'mid': '─', 25 | 'mid-mid': '┼', 26 | 'right': '│', 27 | 'right-mid': '┤', 28 | 'middle': '│' 29 | }; 30 | 31 | var center = [ 32 | Math.floor(windowSize.width / 2), 33 | Math.floor(windowSize.height / 2) 34 | ]; 35 | 36 | var len = 1; 37 | var direction = [0, -1]; 38 | var position, board; 39 | 40 | function render() { 41 | rl.output.unmute(); 42 | utils.clearScreen(rl); 43 | 44 | var output = ''; 45 | var top = center[1] - Math.floor(size / 2); 46 | var left = center[0] - size; 47 | output += '\n' + repeat(' ', left) + score + '\n'; 48 | output += repeat('\n', top - 2); 49 | output += repeat(' ', left); 50 | output += borders['top-left'] + repeat(borders.top, (size * 2)) + borders['top-right'] + '\n'; 51 | 52 | for (var row = 0; row < board.length; row++) { 53 | output += repeat(' ', left) + borders.left; 54 | for (var col = 0; col < board[row].length; col++) { 55 | output += ' ' + board[row][col]; 56 | } 57 | output += borders.right + '\n'; 58 | } 59 | output += repeat(' ', left); 60 | output += borders['bottom-left'] + repeat(borders.bottom, (size * 2)) + borders['bottom-right'] + '\n'; 61 | 62 | rl.output.write(output); 63 | rl.output.mute(); 64 | } 65 | 66 | function move(event) { 67 | if (!event) return; 68 | switch (event.key.name) { 69 | case 'right': 70 | direction = [1, 0]; 71 | head = '>'; 72 | break; 73 | case 'left': 74 | direction = [-1, 0]; 75 | head = '<'; 76 | break; 77 | case 'up': 78 | direction = [0, -1]; 79 | head = '^'; 80 | break; 81 | case 'down': 82 | direction = [0, 1]; 83 | head = 'v'; 84 | break; 85 | } 86 | } 87 | 88 | function reset() { 89 | score = 0; 90 | len = 1; 91 | body = []; 92 | position = [ 93 | Math.floor(size / 2), 94 | Math.floor(size / 2) 95 | ]; 96 | 97 | board = new Array(size); 98 | for (var i = 0; i < board.length; i++) { 99 | board[i] = new Array(size); 100 | for (var j = 0; j < board[i].length; j++) { 101 | board[i][j] = ' '; 102 | } 103 | } 104 | board[position[1]][position[0]] = head; 105 | } 106 | 107 | function tick() { 108 | board[position[1]][position[0]] = ' '; 109 | position[0] += direction[0]; 110 | position[1] += direction[1]; 111 | 112 | if (position[0] >= size || position[1] >= size || position[0] < 0 || position[1] < 0) { 113 | reset(); 114 | return; 115 | } 116 | 117 | if (board[position[1]][position[0]] === '@') { 118 | score++; 119 | len++; 120 | if (body.length) { 121 | body.push(position.slice()); 122 | } 123 | } else if (board[position[1]][position[0]] !== ' ') { 124 | reset(); 125 | return; 126 | } 127 | 128 | board[position[1]][position[0]] = head; 129 | body.reduce(function(acc, curr, i, arr) { 130 | var tail = 'X'; 131 | var prev = (i === 0 ? position : arr[i - 1]); 132 | var next = (i === arr.length - 1 ? null : arr[i + 1]); 133 | if (prev[0] === curr[0]) { 134 | if (next) { 135 | if (next[0] === curr[0]) tail = borders.middle; 136 | else if (next[0] > curr[0]) { 137 | if (prev[1] < curr[1]) tail = borders['bottom-left']; 138 | else tail = borders['top-left']; 139 | } else { 140 | if (prev[1] < curr[1]) tail = borders['bottom-right']; 141 | else tail = borders['top-right']; 142 | } 143 | } else { 144 | tail = borders.middle; 145 | } 146 | } else if (prev[1] === curr[1]) { 147 | if (next) { 148 | if (next[1] === curr[1]) tail = borders.mid; 149 | else if (next[1] > curr[1]) { 150 | if (prev[0] > curr[0]) tail = borders['top-left']; 151 | else tail = borders['top-right']; 152 | } else { 153 | if (prev[0] > curr[0]) tail = borders['bottom-left']; 154 | else tail = borders['bottom-right']; 155 | } 156 | } else { 157 | tail = borders.mid; 158 | } 159 | } 160 | board[curr[1]][curr[0]] = tail; 161 | }, []); 162 | 163 | body.unshift(position.slice()); 164 | while (body.length > len) { 165 | var part = body.pop(); 166 | board[part[1]][part[0]] = ' '; 167 | } 168 | render(); 169 | } 170 | 171 | function apple() { 172 | var x = Math.floor(Math.random() * size); 173 | var y = Math.floor(Math.random() * size); 174 | board[y][x] = '@'; 175 | } 176 | 177 | var rl = utils.createInterface(); 178 | var width = utils.cliWidth(rl); 179 | rl.setPrompt(''); 180 | utils.hideCursor(rl); 181 | 182 | rl.input.on('keypress', function(str, key) { 183 | var event = utils.normalize(str, key); 184 | move(event); 185 | }); 186 | 187 | rl.on('line', function(line) { 188 | console.log('line', line); 189 | }); 190 | 191 | rl.output.mute(); 192 | reset(); 193 | render(); 194 | 195 | setInterval(tick, 200); 196 | setInterval(apple, 2000); 197 | 198 | }; 199 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "snake-cli", 3 | "description": "Experiment / demo using readline-utils to create a simple snake game in the terminal.", 4 | "version": "0.1.1", 5 | "homepage": "https://github.com/doowb/snake-cli", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "repository": "doowb/snake-cli", 8 | "bugs": { 9 | "url": "https://github.com/doowb/snake-cli/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "cli.js", 14 | "index.js" 15 | ], 16 | "preferGlobal": true, 17 | "bin": { 18 | "snake": "cli.js" 19 | }, 20 | "engines": { 21 | "node": ">=0.10.0" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "dependencies": { 27 | "readline-utils": "^0.1.2", 28 | "repeat-string": "^1.5.4", 29 | "window-size": "^0.2.0" 30 | }, 31 | "devDependencies": { 32 | "gulp-format-md": "^0.1.11", 33 | "mocha": "^3.1.2" 34 | }, 35 | "keywords": [ 36 | "cli", 37 | "snake" 38 | ], 39 | "verb": { 40 | "toc": false, 41 | "layout": "global", 42 | "tasks": [ 43 | "readme" 44 | ], 45 | "plugins": [ 46 | "gulp-format-md" 47 | ], 48 | "lint": { 49 | "reflinks": true 50 | }, 51 | "related": { 52 | "list": [ 53 | "enquirer", 54 | "readline-ui", 55 | "readline-utils", 56 | "repeat-string", 57 | "window-size" 58 | ] 59 | }, 60 | "reflinks": [ 61 | "verb", 62 | "verb-generate-readme" 63 | ] 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var snake = require('./'); 6 | 7 | describe('snake-cli', function() { 8 | it('should export a function', function() { 9 | assert.equal(typeof snake, 'function'); 10 | }); 11 | }); 12 | --------------------------------------------------------------------------------