Skip to content

Commit 9bcfd9a

Browse files
committed
feat: add the possibility to select directories
The `type` option is added to define the type selection, either `file`, `directory`, or both `file+directory`. By default the value will be `file` following the expected behavior so far. In addition, from now on the `backspace` key will be used to navigate within directories.
1 parent 3a5b866 commit 9bcfd9a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isBackspaceKey,
66
isDownKey,
77
isEnterKey,
8+
isSpaceKey,
89
isUpKey,
910
makeTheme,
1011
useKeypress,
@@ -55,6 +56,7 @@ const fileSelectorTheme: FileSelectorTheme = {
5556

5657
export default createPrompt<string, FileSelectorConfig>((config, done) => {
5758
const {
59+
type = 'file',
5860
pageSize = 10,
5961
loop = false,
6062
showExcluded = false,
@@ -101,13 +103,19 @@ export default createPrompt<string, FileSelectorConfig>((config, done) => {
101103

102104
useKeypress((key, rl) => {
103105
if (isEnterKey(key)) {
104-
if (activeItem.isDirectory()) {
105-
setCurrentDir(activeItem.path)
106-
setActive(bounds.first)
107-
} else if (!activeItem.isDisabled) {
108-
setStatus('done')
109-
done(activeItem.path)
106+
if (
107+
activeItem.isDisabled ||
108+
(type === 'file' && activeItem.isDirectory()) ||
109+
(type === 'directory' && !activeItem.isDirectory())
110+
) {
111+
return
110112
}
113+
114+
setStatus('done')
115+
done(activeItem.path)
116+
} else if (isSpaceKey(key) && activeItem.isDirectory()) {
117+
setCurrentDir(activeItem.path)
118+
setActive(bounds.first)
111119
} else if (isUpKey(key) || isDownKey(key)) {
112120
rl.clearLine(0)
113121

@@ -173,8 +181,8 @@ export default createPrompt<string, FileSelectorConfig>((config, done) => {
173181
const header = theme.style.currentDir(ensureTrailingSlash(currentDir))
174182
const helpTip = useMemo(() => {
175183
const helpTipLines = [
176-
`${theme.style.key(figures.arrowUp + figures.arrowDown)} navigate, ${theme.style.key('<enter>')} select or open directory`,
177-
`${theme.style.key('<backspace>')} go back${allowCancel ? `, ${theme.style.key('<esc>')} cancel` : ''}`
184+
`${theme.style.key(figures.arrowUp + figures.arrowDown)} navigate, ${theme.style.key('<enter>')} select${allowCancel ? `, ${theme.style.key('<esc>')} cancel` : ''}`,
185+
`${theme.style.key('<space>')} open directory, ${theme.style.key('<backspace>')} go back`
178186
]
179187

180188
const helpTipMaxLength = getMaxLength(helpTipLines)

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ export type FileSelectorConfig = {
105105
* @default process.cwd()
106106
*/
107107
basePath?: string
108+
/**
109+
* The type of elements that are valid selection options.
110+
* @default 'file'
111+
*/
112+
type?: 'file' | 'directory' | 'file+directory'
108113
/**
109114
* The maximum number of items to display in the list.
110115
* @default 10

0 commit comments

Comments
 (0)