[SERVER-3771] custom prompt usability issues Created: 06/Sep/11  Updated: 11/Jul/16  Resolved: 16/Nov/11

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 2.1.0

Type: Improvement Priority: Major - P3
Reporter: Aaron Staple Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-3267 custom prompt Closed
Participants:

 Description   

1) If there is an error evaluating a prompt function, prompt becomes undefined. Might want to set to a default prompt instead.

>
> prompt = '> '
>
> prompt = function()

{ return c; }

function ()

{ return c; } Tue Sep 6 13:45:42 ReferenceError: c is not defined (shell):1 undefined Tue Sep 6 13:45:47 ReferenceError: c is not defined (shell):1 undefined Tue Sep 6 13:45:48 ReferenceError: c is not defined (shell):1 undefined 2) setting prompt to a regex has strange results > prompt = /a/ /a/ Tue Sep 6 15:22:44 SyntaxError: no input for /a/ :1 undefined 3) new lines in prompt mess up shell handling of current line and use of history. Other unusual characters (eg \b) and long lines seem to cause problems too. If the prompt line is wider than my terminal window, I never see anything other than the prompt and every time I type a character I get a new line with the prompt on it. Aaron-Staples-MacBook-Pro:mongo aaron$ ./mongo MongoDB shell version: 2.0.0-rc2-pre- connecting to: test > prompt = "you are special!\n> " you are special! > you are special! > db.c.count() 1 you are special! you are special!> you are special!> db.c.count() <<<----- pressing backspace repeatedly you are special!> db.c.count( you are special!> db.c.count you are special!> db.c.coun you are special!> db.c.cou you are special!> db.c.co you are special!> db.c.c > db.c. find() { "_id" : ObjectId("4e66a1acc98ce70998274933") }

you are special!
you are special!>
you are special!> db.c.find() <<<----- pressing up arrow
you are special!> db.c.count()
you are special!> prompt = "you are special!\n> "
you are special!> db.c.count()
you are special!> prompt = "you are special!\n> "
you are special!> db.c.count()
you are special!> db.c.find()
you are special!>
you are special!> db.c.find()
you are special!> db.c.count()
you are special!> prompt = "you are special!\n> "
you are special!> db.c.count()
you are special!> db.c.find()
you are special!>
you are special!> db.c.find()
>

connecting to: test
> ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

4) Don't know if we want to allow them, but shell helpers aren't available in the prompt function:

> prompt = function()

{ use foo; return db.c.count(); }

Tue Sep 6 15:47:12 SyntaxError: missing ; before statement (shell):1

> prompt = function()

{ exit; }

function () {
exit;
}
Tue Sep 6 15:48:25 ReferenceError: exit is not defined (shell):1
undefined



 Comments   
Comment by Tad Marshall [ 16/Nov/11 ]

The problems described in this bug report are now fixed.

Comment by auto [ 15/Nov/11 ]

Author:

{u'login': u'tadmarshall', u'name': u'Tad Marshall', u'email': u'tad@tadmarshall.com'}

Message: SERVER-3771 – part 2 – make mongo shell behave like bash

Remove the all-on-one-line command line editing in the shell and replace it
with something more in the style of other command lines such as bash. Allow
prompts to exceed the width of the terminal screen and allow newlines in
them. Strip all other control characters from the prompt text before
displaying it so that embedded backspaces, tabs and other controls don't
display badly or require extra code to emulate. This prevents users from
using escape sequences in their prompts to perform highlighting, for now
at least. Allow command line text to exceed the available width following
the prompt by wrapping onto other lines the way they bash does it. Make the
newline character display as a newline instead of as a linefeed or DOS OEM
character. Increase the amount of common code between Windows and
non-Windows versions and make them behave the same way. Don't refresh the
Windows command line when the shift key is pressed.
Branch: master
https://github.com/mongodb/mongo/commit/1194acb7fb8d4336f8bb9097f0c847b4b2b3338e

Comment by auto [ 01/Nov/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot', u'email': u'eliot@10gen.com'}

Message: Merge pull request #122 from tadmarshall/SERVER-3771-part-1-reissue-2

SERVER-3771 – part 1 (reissue 2) – don't let 'prompt' become undefined
Branch: master
https://github.com/mongodb/mongo/commit/751896bafd129f851d334035e7a79ca51be5eeea

Comment by auto [ 01/Nov/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot', u'email': u'eliot@10gen.com'}

Message: Merge pull request #122 from tadmarshall/SERVER-3771-part-1-reissue-2

SERVER-3771 – part 1 (reissue 2) – don't let 'prompt' become undefined
Branch: master
https://github.com/mongodb/mongo/commit/751896bafd129f851d334035e7a79ca51be5eeea

Comment by Tad Marshall [ 27/Oct/11 ]

I closed pull request 96 and reissued the code changes as separate pull requests, but forgot to update the bug report.

Don't let the prompt be displayed as "undefined": https://github.com/mongodb/mongo/pull/122
Make the shell behave more like bash: https://github.com/mongodb/mongo/pull/123

Sorry for the delayed update to the bug report.

Comment by Tad Marshall [ 06/Oct/11 ]

Part 3 was added to pull request 96. The commits are https://github.com/tadmarshall/mongo/commit/95ce1bdede0cf49aad8dd432f995aa0a4964c9e1 and https://github.com/tadmarshall/mongo/commit/9d7b05cbc8fa5f280b5fdeb3082ee7101e25872b .

Comment by Tad Marshall [ 06/Oct/11 ]

Pull request https://github.com/mongodb/mongo/pull/96 addresses parts 1 and 2 of this bug: the undefined value being displayed for the prompt.

Comment by Tad Marshall [ 30/Sep/11 ]

Testing 2.1.0-pre- (my build) on Windows 7 and Ubuntu 11.04's standard Gnome Terminal program prior to working on the reported bugs.

Item 1 behavior on Windows and Ubuntu is identical to the Mac behavior. From code inspection, I could also see that we are leaving a _prompt_ variable lying around after successfully executing a prompt function. This leftover _prompt_ will return as a surprise if prompt is later redefined to a failing function.

MongoDB shell version: 2.1.0-pre-
connecting to: test
> prompt=function()

{return asdf;}

// set prompt to a function that will fail
function () {
return asdf;
}
Thu Sep 29 21:27:34 ReferenceError: asdf is not defined (shell):1 // error executing prompt
undefined // _prompt_ variable is not set but displayed anyway
Thu Sep 29 21:27:47 ReferenceError: asdf is not defined (shell):1
undefined prompt=function()

{return "Function prompt> ";}

// set prompt to a function that returns a string
function () {
return "Function prompt> ";
}
Function prompt> // functional prompt works as expected
Function prompt> prompt="String prompt> " // set prompt to a string
String prompt>
String prompt> // string prompt works as expected
String prompt> prompt=function()

{return junk;}

// set prompt to a function that will fail
function () {
return junk;
}
Thu Sep 29 21:29:58 ReferenceError: junk is not defined (shell):1
Function prompt> // the leftover _prompt_ variable is still around
Thu Sep 29 21:30:00 ReferenceError: junk is not defined (shell):1
Function prompt>

Item 2 behaves the same Windows and Ubuntu as on the Mac. "typeof /a/" displays "function" so the reported error is different but otherwise the behavior is the same as item 1.

Item 3 (regarding newline in the prompt and prompts that are wider than the terminal width) is a bit worse in Windows. With a \n in the prompt text, things are OK until a redraw happens. Redraws occur on backspace and up-arrow, but also when the shift key is pressed. When we redraw, we display the newline as a Windows/DOS OEM character, so the prompt gets longer by one character and the newline doesn't happen. When the prompt text length exceeds the terminal width, the redraw happens on the first keystroke and the prompt is truncated with the last position displaying a space. It is impossible to see anything that you type because it is drawn and then overwritten with a space. In Ubuntu it is perhaps even worse. The newline is interpreted literally as a linefeed, so the cursor is left hanging in space below the text and just to its right. You need to use \r\n to make the prompt display as a "normal" two-line prompt. Redraws are not triggered by the shift key (as on Windows) but when triggered with backspace (for example) the redraw is botched and even with \r\n the cursor is left hanging out in space.

We may want to mention item 4 in the documentation (or not), but the code executing the prompt is just the eval() function and the behavior is no different. eval() doesn't support the shell's extensions and prompt=function(){} doesn't either.

Generated at Thu Feb 08 03:04:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.