Permalink
Browse files

Remove `Shell.confirm`, now only in req; New `styles.unstyle` function

  • Loading branch information...
1 parent f31f02a commit 268a5d4e1fe8e9d4dee5949fea185911fa9dea9f @wdavidw committed Dec 3, 2011
Showing with 61 additions and 24 deletions.
  1. +1 −0 README.md
  2. +2 −0 changes.md
  3. +26 −3 lib/Request.coffee
  4. +1 −21 lib/Shell.coffee
  5. +4 −0 lib/Styles.coffee
  6. +27 −0 test/ConfirmTest.coffee
View
@@ -122,6 +122,7 @@ The request object contains the following properties:
- `command` , Command entered by the user
- `params` , Parameters object extracted from the command, defined by the `shell.router` middleware
- `qestion` , Ask questions with optionally suggested and default answers
+- `confirm` , Ask a question expecting a boolean answer
## `Response` parameter
View
@@ -6,6 +6,8 @@ Version 0.2.4
-------------
`Shell.question` has been removed, use `req.question` instead
+`Shell.confirm` has been removed, use `req.confirm` instead
+Add `styles.unstyle`
Version 0.2.3
-------------
View
@@ -5,9 +5,9 @@ module.exports = class Request
constructor: (shell, command) ->
@shell = shell
@command = command
- confirm: (msg, defaultTrue, callback) ->
- @shell.confirm.apply @shell, arguments
- # Ask one or more questions
+ ###
+ Ask one or more questions
+ ###
question: (questions, callback) ->
isObject = (v) -> typeof v is 'object' and v? and not Array.isArray v
multiple = true
@@ -32,3 +32,26 @@ module.exports = class Request
.on 'end', ->
answers = answers[questions[0].name] unless multiple
return callback answers
+ ###
+ Ask a question expecting a boolean answer
+ ###
+ confirm: (msg, defaultTrue, callback) ->
+ args = arguments
+ unless callback
+ callback = defaultTrue
+ defaultTrue = true
+ @shell.settings.key_true ?= 'y'
+ @shell.settings.key_false ?= 'n'
+ key_true = @shell.settings.key_true.toLowerCase()
+ key_false = @shell.settings.key_false.toLowerCase()
+ keyTrue = if defaultTrue then key_true.toUpperCase() else key_true
+ keyFalse = if defaultTrue then key_false else key_false.toUpperCase()
+ msg += ' '
+ msg += "[#{keyTrue}#{keyFalse}] "
+ question = @shell.styles.raw( msg, {color: 'green'})
+ @shell.interface().question question, (answer) =>
+ accepted = ['', key_true, key_false]
+ answer = answer.toLowerCase()
+ valid = accepted.indexOf(answer) isnt -1
+ return @confirm.apply(@, args) unless valid
+ callback answer is key_true or (defaultTrue and answer is '')
View
@@ -14,7 +14,7 @@ Interface = require('readline').Interface
Interface.prototype.setPrompt = ( (parent) ->
(prompt, length) ->
args = Array.prototype.slice.call arguments
- args[1] = args[0].replace(/\x1b.*?m/g, '').length if not args[1]
+ args[1] = styles.unstyle(args[0]).length if not args[1]
parent.apply @, args
)( Interface.prototype.setPrompt )
@@ -145,26 +145,6 @@ module.exports = class Shell extends EventEmitter
@settings.stdout.on 'close', ->
process.exit()
- # Ask a question with a boolean answer
- confirm: (msg, defaultTrue, callback) ->
- args = arguments
- unless callback
- callback = defaultTrue
- defaultTrue = true
- @settings.key_true ?= 'y'
- @settings.key_false ?= 'n'
- key_true = @settings.key_true.toLowerCase()
- key_false = @settings.key_false.toLowerCase()
- keyTrue = if defaultTrue then key_true.toUpperCase() else key_true
- keyFalse = if defaultTrue then key_false else key_false.toUpperCase()
- msg += "[#{keyTrue}#{keyFalse}] "
- @interface().question @styles.raw( msg, {color: 'green'}), (answer) =>
- accepted = ['', key_true, key_false]
- answer = answer.toLowerCase()
- valid = accepted.indexOf(answer) isnt -1
- return @confirm.apply(@, args) unless valid
- callback answer is key_true or (defaultTrue and answer is '')
-
# Command quit
quit: (params) ->
@emit 'quit'
View
@@ -111,3 +111,7 @@ Styles.prototype.reset = (text) ->
@print null,
color: null
weight: 'regular'
+
+# Remove style
+Styles.unstyle = (text) -> text.replace(/\x1b.*?m/g, '')
+
@@ -0,0 +1,27 @@
+
+shell = require '..'
+assert = require 'assert'
+styles = require '../lib/styles'
+
+module.exports =
+ 'Confirm # yes and no': (next) ->
+ stdin = new shell.NullStream
+ stdout = new shell.NullStream
+ stdout.on 'data', (data) ->
+ return unless data.trim()
+ assert.eql styles.unstyle(data), 'Do u confirm? [Yn] '
+ @answer = !@answer
+ stdin.emit 'data', if @answer then 'y' else 'N'
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test string'
+ stdin: stdin
+ stdout: stdout
+ app.configure ->
+ app.use shell.router shell: app
+ app.cmd 'test string', (req, res) ->
+ req.confirm 'Do u confirm?', (value) ->
+ assert.eql value, true
+ req.confirm 'Do u confirm?', (value) ->
+ assert.eql value, false
+ next()

0 comments on commit 268a5d4

Please sign in to comment.