Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<shift>+<up/down> does not work in Mac Terminal #206

Open
kaustavghosh06 opened this issue Aug 25, 2019 · 11 comments
Open

<shift>+<up/down> does not work in Mac Terminal #206

kaustavghosh06 opened this issue Aug 25, 2019 · 11 comments
Labels

Comments

@kaustavghosh06
Copy link

I'm trying to use the Sort type of question for a CLI flow, like the following:

  const sortPrompt = new Sort({
    name: 'sortNames",
    message: 'Sort the user list in order of preference',
    choices: userList
  });

Here's my CLI display

? Sort the user list in order of preference …  (Use <shift>+<up/down> to sort)
  abc
  xyz

But I'm not able to sort the above list with the instructions - <shift> + up/down arrow.
I'm on a Mac system and not sure what I'm doing wrong out here.

@doowb
Copy link
Member

doowb commented Aug 25, 2019

Hi @kaustavghosh06, I copied the sort/prompt.js example and replaced it with code to match yours, and it's working for me (I'm also on Mac):

'use strict';

const { Sort } = require('enquirer');

const userList = ['abc', 'xyz'];
const prompt = new Sort({
  name: 'sortNames',
  message: 'Sort the user list in order of preference',
  choices: userList
});

prompt.run()
  .then(function(answer = []) {
    console.log(answer);
  })
  .catch(console.error);

To rule out any bugs that may have been fixed in the GitHub repository but not published to NPM yet, will you run your code using the latest on GitHub (either clone the repo and run in the same way the examples run, or install it using npm install enquirer/enquirer).

Also, if you have any additional information about your terminal, that might be helpful.

@kaustavghosh06
Copy link
Author

kaustavghosh06 commented Oct 2, 2019

@doowb This works on iterm on a Mac but won't work in the default Mac terminal. The root-cause is out here - https://github.com/enquirer/enquirer/blob/master/lib/keypress.js#L104
On iterm key.sequence is '\u001b[1;2B' for shift+down but on mac terminal it's '\u001b[B'. Related issue - https://superuser.com/questions/841391/os-x-terminal-eating-the-shift-key
Any way to solve this? I'm blocked on this currently :(

@doowb
Copy link
Member

doowb commented Oct 2, 2019

@kaustavghosh06 thank you for the additional details (I see that it also does not work for me in the Mac Terminal).

However, the sequence that you put above is the same. Did you mean to put a different sequence?

@doowb doowb changed the title Usage of Sort type of question <shift>+<up/down> does not work in Mac Terminal Oct 2, 2019
@doowb doowb added the bug label Oct 2, 2019
@kaustavghosh06
Copy link
Author

@doowb Sorry, updated the sequence.

@kaustavghosh06
Copy link
Author

kaustavghosh06 commented Oct 2, 2019

As a workaround, I forked the package and changed the right/left keypresses to invoke the up and down functionality and the Mac terminal is able to detect shift +left/right arrows.

Change in lib/keypress.js:

   /* xterm ESC [ letter */
    '[A': 'up',
    '[B': 'down',
    '[C': 'down',
    '[D': 'up',
    '[E': 'clear',
    '[F': 'end',
    '[H': 'home',

Hesitant to submit a PR with the above change, since I'm not certain if it'll break any other functionality in the lib which actually uses right/left arrows to perform some activities. This solution works for me as I'm just interested in the sort functionaility.

@undefobj
Copy link

undefobj commented Oct 2, 2019

@doowb I also work with @kaustavghosh06 and we're happy to submit a PR on this right now, but we don't want to break anything. We actually need this functionality very soon for a release and would prefer not to temporarily fork the project and just contribute a fix, but we understand if you need to think about this more.

@doowb
Copy link
Member

doowb commented Oct 2, 2019

Thanks for looking into this more. A PR would be wonderful, but it would need to ensure other things didn't break (as @kaustavghosh06 mentioned).

I don't think that the suggested change of the meaning of the escape sequence is the correct way to go, however, it lead me to investigate a little more and I think I have an option as a work around for you.

Since you'd be willing to let the user know they could use the <shift>+<left/right> keys, you can override the default functionality of those actions and just call the <shift>+<up/down> actions. I've updated the example from above with the options you can pass into your prompt:

'use strict';

const { Sort } = require('enquirer');

const userList = ['abc', 'xyz'];
const prompt = new Sort({
  name: 'sortNames',
  message: 'Sort the user list in order of preference',
  hint: '(Use <shift>+<left/right> to sort)',
  choices: userList,
  shiftLeft(...args) {
    return this.shiftUp(...args);
  },
  shiftRight(...args) {
    return this.shiftDown(...args);
  }
});

prompt.run()
  .then(function(answer = []) {
    console.log(answer);
  })
  .catch(console.error);

This lets you use <shift>+<left/right> instead of <shift>+<up/down>. If we can figure out how to detect what sequence the Mac Terminal is sending for <shift>+<up/down> (and it's different than just regular <up/down>), then we could fix this properly.

/edit: I updated the example to include the hint with instructions on using <shift>+<left/right>.

@undefobj
Copy link

undefobj commented Oct 2, 2019

@doowb Thanks a bunch! We just tested this and it's working great. Really appreciate the help. We'll roll with this solution for now but let us know if there is something we should replace it with in the future.

@doowb
Copy link
Member

doowb commented Oct 3, 2019

NP... I'll leave this open as a reminder to see if we can do anything.

@kaustavghosh06
Copy link
Author

Thanks a lot @doowb for the workaround.

@docsobral
Copy link

I've verified that the issue persists. I'm on Windows 10 with enquirer v2.3.6, and it works fine in the embeded GitBash shell inside VS Code, for example, but doesn't work in the standalone GitBash shell. Tested with the most recent version, v2.4.1, and I'm getting the same results.

@doowb's proposed solution of overrinding the default functionality doesn't work either :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants