-
-
Notifications
You must be signed in to change notification settings - Fork 828
feat(repl): add settings and prototype methods for keybindings #3331
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
Merged
kgryte
merged 17 commits into
stdlib-js:develop
from
Snehil-Shah:keybindings-configuration
Jan 17, 2025
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f34f431
feat(repl): add settings and prototype methods for keybindings
Snehil-Shah 1647cb7
feat: allow setting keybinding using optional arg
Snehil-Shah ea1e48a
feat: update help text and output msg
Snehil-Shah 1ad026c
fix: remove `^D` from default keybindings
Snehil-Shah 1333a92
refactor: maintain hash in a dedicated keybindings class
Snehil-Shah e8ea315
style: remove space between parens
Snehil-Shah 986e2f7
fix: log error message correctly
Snehil-Shah d3d5ec9
fix: remove conflicting keybinding
Snehil-Shah d113879
refactor: explicitly check for primitive
kgryte 7c7f8c3
refactor: use partial application
kgryte ca9545e
docs: update description
kgryte e9c1b65
refactor: use partial application
kgryte 5ad5d51
docs: update description
kgryte 97ce1af
docs: add annotation
kgryte 235ebe8
refactor: check for primitives
kgryte 70a0bf8
fix: truncate help text
Snehil-Shah c6287e2
refactor: use partial application
Snehil-Shah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MAIN // | ||
|
||
/** | ||
* List of REPL actions. | ||
* | ||
* @private | ||
* @name ACTIONS | ||
* @type {Object} | ||
*/ | ||
var ACTIONS = [ | ||
'moveRight', | ||
'moveLeft', | ||
'moveWordRight', | ||
'moveWordLeft', | ||
'moveBeginning', | ||
'moveEnd', | ||
'tab', | ||
'indentLineRight', | ||
'indentLineLeft', | ||
'deleteLeft', | ||
'deleteRight', | ||
'deleteWordLeft', | ||
'deleteWordRight', | ||
'deleteLineLeft', | ||
'deleteLineRight', | ||
'yankKilled', | ||
'yankPop', | ||
'undo', | ||
'redo', | ||
'transposeAboutCursor', | ||
'uppercaseNextWord', | ||
'capitalizeNextWord', | ||
'lowercaseNextWord', | ||
'clearScreen' | ||
]; | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = ACTIONS; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var format = require( '@stdlib/string/format' ); | ||
var contains = require( '@stdlib/array/base/assert/contains' ).factory; | ||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var ACTIONS = require( './../actions.js' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var isAction = contains( ACTIONS ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Returns a callback to be invoked upon calling the `keybindings` command. | ||
* | ||
* @private | ||
* @param {REPL} repl - REPL instance | ||
* @returns {Function} callback | ||
*/ | ||
function command( repl ) { | ||
return onCommand; | ||
|
||
/** | ||
* Returns all (or select) keybindings. | ||
* | ||
* @private | ||
* @param {string} [action] - action name | ||
* @returns {(Object|Array<Object>)} keybindings object or a list of keybindings | ||
*/ | ||
function onCommand() { | ||
var action; | ||
var nargs; | ||
|
||
nargs = arguments.length; | ||
if ( nargs === 0 ) { | ||
return repl.keybindings(); | ||
} | ||
if ( nargs === 1 ) { | ||
action = arguments[ 0 ]; | ||
if ( !isString( action ) ) { | ||
repl._ostream.write( format( 'Error: invalid argument. First argument must be a string. Value: `%s`.', action ) ); | ||
return; | ||
} | ||
if ( !isAction( action ) ) { | ||
repl._ostream.write( format( 'Error: invalid argument. First argument must be a valid action name. Value: `%s`.', action ) ); | ||
return; | ||
} | ||
return repl.keybindings()[ action ]; | ||
} | ||
} | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = command; |
94 changes: 94 additions & 0 deletions
94
lib/node_modules/@stdlib/repl/lib/commands/set_keybinding.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var format = require( '@stdlib/string/format' ); | ||
var contains = require( '@stdlib/array/base/assert/contains' ).factory; | ||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var ACTIONS = require( './../actions.js' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var isAction = contains( ACTIONS ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Returns a callback to be invoked upon calling the `setKeybinding` command. | ||
* | ||
* @private | ||
* @param {REPL} repl - REPL instance | ||
* @returns {Function} callback | ||
*/ | ||
function command( repl ) { | ||
return onCommand; | ||
|
||
/** | ||
* Allows the user to set a keybinding. | ||
* | ||
* @private | ||
* @param {string} action - action name | ||
* @param {Array<Object>} [keys] - list of keys | ||
* @returns {void} | ||
*/ | ||
function onCommand() { | ||
var action; | ||
var nargs; | ||
var keys; | ||
|
||
nargs = arguments.length; | ||
if ( nargs === 0 ) { | ||
repl._ostream.write( 'Error: invalid argument. First argument must be an action name.\n' ); | ||
return; | ||
} | ||
action = arguments[ 0 ]; | ||
if ( nargs === 1 ) { | ||
if ( !isString( action ) ) { | ||
repl._ostream.write( format( 'Error: invalid argument. First argument must be a string. Value: `%s`.\n', action ) ); | ||
return; | ||
} | ||
if ( !isAction( action ) ) { | ||
repl._ostream.write( format( 'Error: invalid argument. First argument must be a valid action name. Value: `%s`.\n', action ) ); | ||
return; | ||
} | ||
repl._ostream.write( 'Listening for keypresses...' ); | ||
repl._isCapturingKeybinding = true; | ||
repl._targetAction = action; | ||
return; | ||
} | ||
keys = arguments[ 1 ]; | ||
try { | ||
repl.setKeybinding( action, keys ); | ||
repl._ostream.write( format( '\nSuccessfully set keybindings for action `%s`.\n', action ) ); | ||
} catch ( err ) { | ||
repl._ostream.write( format( 'Error: %s\n', err.message ) ); | ||
} | ||
} | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = command; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.