| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e30f32f commit 6b1fc63
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -357,6 +357,7 @@ added: v0.1.98 | |||
| 357 | 357 | the history set this value to `0`. Defaults to `30`. This option makes sense | |
| 358 | 358 | only if `terminal` is set to `true` by the user or by an internal `output` | |
| 359 | 359 | check, otherwise the history caching mechanism is not initialized at all. | |
| 360 | + * `prompt` - the prompt string to use. Default: `'> '` | ||
| 360 | 361 | ||
| 361 | 362 | The `readline.createInterface()` method creates a new `readline.Interface` | |
| 362 | 363 | instance. | |
@@ -467,9 +468,12 @@ implement a small command-line interface: | |||
| 467 | 468 | ||
| 468 | 469 | ```js | |
| 469 | 470 | const readline = require('readline'); | |
| 470 | - const rl = readline.createInterface(process.stdin, process.stdout); | ||
| 471 | + const rl = readline.createInterface({ | ||
| 472 | + input: process.stdin, | ||
| 473 | + output: process.stdout, | ||
| 474 | + prompt: 'OHAI> ' | ||
| 475 | + }); | ||
| 471 | 476 | ||
| 472 | - rl.setPrompt('OHAI> '); | ||
| 473 | 477 | rl.prompt(); | |
| 474 | 478 | ||
| 475 | 479 | rl.on('line', (line) => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,13 +45,17 @@ function Interface(input, output, completer, terminal) { | |||
| 45 | 45 | ||
| 46 | 46 | EventEmitter.call(this); | |
| 47 | 47 | var historySize; | |
| 48 | + let prompt = '> '; | ||
| 48 | 49 | ||
| 49 | 50 | if (arguments.length === 1) { | |
| 50 | 51 | // an options object was given | |
| 51 | 52 | output = input.output; | |
| 52 | 53 | completer = input.completer; | |
| 53 | 54 | terminal = input.terminal; | |
| 54 | 55 | historySize = input.historySize; | |
| 56 | + if (input.prompt !== undefined) { | ||
| 57 | + prompt = input.prompt; | ||
| 58 | + } | ||
| 55 | 59 | input = input.input; | |
| 56 | 60 | } | |
| 57 | 61 | ||
@@ -88,7 +92,7 @@ function Interface(input, output, completer, terminal) { | |||
| 88 | 92 | }; | |
| 89 | 93 | } | |
| 90 | 94 | ||
| 91 | - this.setPrompt('> '); | ||
| 95 | + this.setPrompt(prompt); | ||
| 92 | 96 | ||
| 93 | 97 | this.terminal = !!terminal; | |
| 94 | 98 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -385,11 +385,10 @@ function REPLServer(prompt, | |||
| 385 | 385 | output: self.outputStream, | |
| 386 | 386 | completer: complete, | |
| 387 | 387 | terminal: options.terminal, | |
| 388 | - historySize: options.historySize | ||
| 388 | + historySize: options.historySize, | ||
| 389 | + prompt | ||
| 389 | 390 | }); | |
| 390 | 391 | ||
| 391 | - self.setPrompt(prompt !== undefined ? prompt : '> '); | ||
| 392 | - | ||
| 393 | 392 | this.commands = Object.create(null); | |
| 394 | 393 | defineDefaultCommands(this); | |
| 395 | 394 | ||
@@ -408,8 +407,6 @@ function REPLServer(prompt, | |||
| 408 | 407 | }; | |
| 409 | 408 | } | |
| 410 | 409 | ||
| 411 | - self.setPrompt(self._prompt); | ||
| 412 | - | ||
| 413 | 410 | self.on('close', function() { | |
| 414 | 411 | self.emit('exit'); | |
| 415 | 412 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,11 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | - require('../common'); | ||
| 3 | - var assert = require('assert'); | ||
| 4 | - var readline = require('readline'); | ||
| 5 | - var EventEmitter = require('events').EventEmitter; | ||
| 6 | - var inherits = require('util').inherits; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const readline = require('readline'); | ||
| 5 | + const EventEmitter = require('events').EventEmitter; | ||
| 6 | + const inherits = require('util').inherits; | ||
| 7 | + const Writable = require('stream').Writable; | ||
| 8 | + const Readable = require('stream').Readable; | ||
| 7 | 9 | ||
| 8 | 10 | function FakeInput() { | |
| 9 | 11 | EventEmitter.call(this); | |
@@ -400,4 +402,29 @@ function isWarned(emitter) { | |||
| 400 | 402 | }); | |
| 401 | 403 | }); | |
| 402 | 404 | ||
| 405 | + { | ||
| 406 | + const expected = terminal | ||
| 407 | + ? ['\u001b[1G', '\u001b[0J', '$ ', '\u001b[3G'] | ||
| 408 | + : ['$ ']; | ||
| 409 | + | ||
| 410 | + let counter = 0; | ||
| 411 | + const output = new Writable({ | ||
| 412 | + write: common.mustCall((chunk, enc, cb) => { | ||
| 413 | + assert.strictEqual(chunk.toString(), expected[counter++]); | ||
| 414 | + cb(); | ||
| 415 | + rl.close(); | ||
| 416 | + }, expected.length) | ||
| 417 | + }); | ||
| 418 | + | ||
| 419 | + const rl = readline.createInterface({ | ||
| 420 | + input: new Readable({ read: () => {} }), | ||
| 421 | + output: output, | ||
| 422 | + prompt: '$ ', | ||
| 423 | + terminal: terminal | ||
| 424 | + }); | ||
| 425 | + | ||
| 426 | + rl.prompt(); | ||
| 427 | + | ||
| 428 | + assert.strictEqual(rl._prompt, '$ '); | ||
| 429 | + } | ||
| 403 | 430 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments