-
Notifications
You must be signed in to change notification settings - Fork 133
feat(@clack/core,@clack/prompts): path prompt #148
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
Closed
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
16a3c34
feat(@clack/core): path prompt
orochaa f6b5042
feat(@clack/prompts): path prompt
orochaa a93b884
feat: free navigation
orochaa 6a64200
chore: add changeset
orochaa c55a847
feat: max items
orochaa a1a6a94
feat: folder arrow
orochaa fb8262a
chore: lint
orochaa 9395dbb
feat: only show dir option
orochaa c99fa86
chore: restore basic example
orochaa 6ffd8c3
chore: add file selection example
orochaa ef2d7e1
fix: leading space
orochaa 647cefd
refactor: remove unused event listener
orochaa 2aaf6bb
refactor: improve cursor mapping
orochaa 1a11247
refactor: cancel state
orochaa a28d2b1
Merge branch 'main' of https://github.com/natemoo-re/clack into path
orochaa 9bc1e8b
Merge branch 'path' of https://github.com/Mist3rBru/clack into path
orochaa 1bdf0bc
docs: add path example
orochaa 154f9c2
feat: path input
orochaa 9ed6904
Merge branch 'main' of https://github.com/natemoo-re/clack into path
orochaa 0beb787
Merge branch 'path' of https://github.com/Mist3rBru/clack into path
orochaa c9e3043
refactor: prevent error on file root
orochaa b2c639c
chore: update docs
orochaa b118f43
feat: path validate
orochaa 5487280
feat: shell like input
orochaa ec70704
feat: highlight current hint option
orochaa 3f1db88
feat: maxHintOptions param
orochaa dec422f
feat: reverse hint option selection
orochaa 8c854c1
feat: indentify directories on hintOptions
orochaa 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,5 @@ | ||
--- | ||
'@clack/core': minor | ||
--- | ||
|
||
Feat PathPrompt |
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,5 @@ | ||
--- | ||
'@clack/prompts': minor | ||
--- | ||
|
||
Feat path prompt |
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,28 @@ | ||
import * as p from '@clack/prompts'; | ||
|
||
(async () => { | ||
const selectResult = await p.path({ | ||
type: 'select', | ||
message: 'Pick a file with select component:', | ||
initialValue: process.cwd(), | ||
onlyShowDir: false, | ||
maxItems: 15, | ||
}); | ||
if (p.isCancel(selectResult)) { | ||
p.cancel('File selection canceled'); | ||
process.exit(0); | ||
} | ||
|
||
const inputResult = await p.path({ | ||
type: 'text', | ||
message: 'Pick other file with input component:', | ||
onlyShowDir: false, | ||
placeholder: process.cwd(), | ||
}); | ||
if (p.isCancel(inputResult)) { | ||
p.cancel('File input canceled'); | ||
process.exit(0); | ||
} | ||
|
||
console.log({ selectResult, inputResult }); | ||
})(); |
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
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,285 @@ | ||
import { readdirSync, statSync } from 'node:fs'; | ||
import { resolve } from 'node:path'; | ||
import { Key } from 'node:readline'; | ||
import color from 'picocolors'; | ||
import Prompt, { PromptOptions } from './prompt'; | ||
|
||
interface PathNode { | ||
name: string; | ||
children: PathNode[] | undefined; | ||
} | ||
|
||
type PathType = 'text' | 'select'; | ||
|
||
type PathOptions = PromptOptions<PathPrompt> & | ||
( | ||
| { | ||
type: 'text'; | ||
onlyShowDir?: boolean; | ||
placeholder?: string; | ||
} | ||
| { | ||
type: 'select'; | ||
onlyShowDir?: boolean; | ||
maxHintOptions?: number; | ||
} | ||
); | ||
|
||
export default class PathPrompt extends Prompt { | ||
public readonly type: PathType; | ||
public readonly placeholder: string; | ||
public readonly onlyShowDir: boolean; | ||
public root: PathNode; | ||
public hint: string; | ||
public hintOptions: string[]; | ||
public valueWithHint: string; | ||
public hintIndex: number; | ||
|
||
private _cursorMap: number[]; | ||
private _maxHintOptions: number; | ||
|
||
public get option() { | ||
let aux: PathNode = this.root; | ||
for (const index of this._cursorMap) { | ||
if (aux.children && aux.children[index]) { | ||
aux = aux.children[index]; | ||
} | ||
} | ||
return { | ||
index: this._cursorMap[this._cursorMap.length - 1] ?? 0, | ||
depth: this._cursorMap.length, | ||
node: aux, | ||
}; | ||
} | ||
|
||
public get options(): PathNode[] { | ||
let aux: PathNode = this.root; | ||
let options: PathNode[] = [this.root]; | ||
for (const index of this._cursorMap) { | ||
options = options.concat(aux.children ?? []); | ||
if (aux.children && aux.children[index]) { | ||
aux = aux.children[index]; | ||
} | ||
} | ||
return options; | ||
} | ||
|
||
public get cursor(): number { | ||
return this.type === 'select' ? this._cursorMap.reduce((a, b) => a + b + 1, 0) : this._cursor; | ||
} | ||
|
||
private get _layer(): PathNode[] { | ||
let aux: PathNode = this.root; | ||
let options: PathNode[] = []; | ||
for (const index of this._cursorMap) { | ||
if (aux.children?.[index]) { | ||
options = aux.children; | ||
aux = aux.children[index]; | ||
} else { | ||
break; | ||
} | ||
} | ||
return options; | ||
} | ||
|
||
private get _selectedValue(): string { | ||
const value: string[] = []; | ||
let option: PathNode = this.root; | ||
for (const index of this._cursorMap) { | ||
if (option.children?.[index]) { | ||
option = option.children[index]; | ||
value.push(option.name); | ||
} | ||
} | ||
return resolve(this.root.name, ...value); | ||
} | ||
|
||
private _changeSelectValue(): void { | ||
this.value = this._selectedValue; | ||
} | ||
|
||
private get _valueDir(): string { | ||
return this.value.replace(/^(.*)\/.*/, '$1').replace(/\s+$/, ''); | ||
} | ||
|
||
private get _valueEnd(): string { | ||
return this.value.replace(/.*\/(.*)$/, '$1'); | ||
} | ||
|
||
private get _hintOptions(): string[] { | ||
return statSync(this._valueDir, { throwIfNoEntry: false })?.isDirectory() | ||
? this._mapDir(this._valueDir) | ||
.filter((node) => node.name.startsWith(this._valueEnd)) | ||
.slice(0, this._maxHintOptions) | ||
.map((node) => node.name + (!!node.children ? '/' : '')) | ||
: []; | ||
} | ||
|
||
private _changeInputHint(): void { | ||
const hintOption = this._hintOptions[0] ?? ''; | ||
this.hintOptions = []; | ||
this.hint = hintOption.replace(this._valueEnd, ''); | ||
} | ||
|
||
private _changeInputValue(): void { | ||
let value: string; | ||
let hint: string; | ||
const cursor = color.inverse(color.hidden('_')); | ||
if (this.cursor >= this.value.length) { | ||
value = this.value; | ||
hint = this.hint | ||
? `${color.inverse(this.hint.charAt(0))}${color.dim(this.hint.slice(1))}` | ||
: cursor; | ||
} else { | ||
const s1 = this.value.slice(0, this.cursor); | ||
const s2 = this.value.slice(this.cursor); | ||
value = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`; | ||
hint = color.dim(this.hint); | ||
} | ||
this.valueWithHint = value + hint; | ||
} | ||
|
||
private _changeValue(): void { | ||
if (this.type === 'select') { | ||
this._changeSelectValue(); | ||
} else { | ||
this._changeInputHint(); | ||
this._changeInputValue(); | ||
} | ||
} | ||
|
||
private _autocomplete(): void { | ||
const complete = this.value ? this.hint : this.placeholder; | ||
this.value += complete; | ||
this._cursor = this.value.length; | ||
this.hint = ''; | ||
this.hintOptions = []; | ||
this.rl.write(complete); | ||
this._changeInputValue(); | ||
} | ||
|
||
private _suggestAutocomplete(step: number): void { | ||
const hintOptions = this._hintOptions; | ||
if (hintOptions.length <= 1) { | ||
this._autocomplete(); | ||
} else if (this.hintOptions.length) { | ||
this.hintIndex += step; | ||
if (this.hintIndex >= this.hintOptions.length) { | ||
this.hintIndex = 0; | ||
} else if (this.hintIndex < 0) { | ||
this.hintIndex = this.hintOptions.length - 1; | ||
} | ||
this.hint = this.hintOptions[this.hintIndex].replace(this._valueEnd, ''); | ||
this._changeInputValue(); | ||
} else { | ||
this.hintIndex = -1; | ||
this.hintOptions = hintOptions; | ||
} | ||
} | ||
|
||
private _mapDir(path: string): PathNode[] { | ||
return readdirSync(path, { withFileTypes: true }) | ||
.map((item) => ({ | ||
name: item.name, | ||
children: item.isDirectory() ? [] : undefined, | ||
})) | ||
.filter((node) => { | ||
return this.onlyShowDir ? !!node.children : true; | ||
}); | ||
} | ||
|
||
constructor(opts: PathOptions) { | ||
super(opts, opts.type === 'text'); | ||
|
||
// General | ||
this.type = opts.type; | ||
this.onlyShowDir = opts.onlyShowDir ?? false; | ||
this.value = opts.initialValue ?? ''; | ||
|
||
// Select | ||
this._cursorMap = [0]; | ||
const cwd = opts.initialValue ?? process.cwd(); | ||
this.root = { | ||
name: cwd, | ||
children: this._mapDir(cwd), | ||
}; | ||
|
||
// Text | ||
this.placeholder = opts.placeholder ?? ''; | ||
this.hint = ''; | ||
this.hintOptions = []; | ||
this.hintIndex = -1; | ||
this._maxHintOptions = | ||
'maxHintOptions' in opts && opts.maxHintOptions ? opts.maxHintOptions : Infinity; | ||
this.valueWithHint = ''; | ||
|
||
this._changeValue(); | ||
|
||
this.on('cursor', (key) => { | ||
if (this.type !== 'select') return; | ||
|
||
switch (key) { | ||
case 'up': | ||
if (this._cursorMap.length) { | ||
this._cursorMap = [ | ||
...this._cursorMap.slice(0, -1), | ||
this.option.index > 0 ? this.option.index - 1 : this._layer.length - 1, | ||
]; | ||
} | ||
break; | ||
case 'down': | ||
if (this._cursorMap.length) { | ||
this._cursorMap = [ | ||
...this._cursorMap.slice(0, -1), | ||
this.option.index < this._layer.length - 1 ? this.option.index + 1 : 0, | ||
]; | ||
} | ||
break; | ||
case 'right': | ||
if (this.option.node.children) { | ||
const children = this._mapDir(this._selectedValue); | ||
this.option.node.children = children; | ||
this._cursorMap = children.length ? [...this._cursorMap, 0] : this._cursorMap; | ||
} | ||
break; | ||
case 'left': | ||
const prevCursor = this._cursorMap; | ||
this._cursorMap = this._cursorMap.slice(0, -1); | ||
if (this.option.node.children?.length && this._cursorMap.length) { | ||
this.option.node.children = []; | ||
} else if (prevCursor.length === 0) { | ||
const cwd = resolve(this.root.name, '..'); | ||
this.root = { | ||
name: cwd, | ||
children: this._mapDir(cwd), | ||
}; | ||
} | ||
break; | ||
} | ||
return this._changeSelectValue(); | ||
}); | ||
|
||
this.on('cursor', (key) => { | ||
if (this.type !== 'text') return; | ||
|
||
if (key === 'right' && this.cursor >= this.value.length) { | ||
this._autocomplete(); | ||
} | ||
}); | ||
|
||
this.on('key', (char: string, key: Key) => { | ||
if (this.type !== 'text') return; | ||
|
||
if (key.name === 'tab') { | ||
this._suggestAutocomplete(key.shift ? -1 : 1); | ||
} else { | ||
this._changeInputHint(); | ||
this._changeInputValue(); | ||
} | ||
}); | ||
|
||
this.on('finalize', () => { | ||
this.value = this.value ? resolve(this.value) : ''; | ||
}); | ||
} | ||
} |
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
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.