Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 123 additions & 17 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,53 @@ function getAllFiles(dir: string, rootDir = dir): string[] {
}

function getHistory(): string[] {
const historyPath = path.join(process.env.HOME || "", ".zsh_history");
const shell = process.env.SHELL || ''
const isPowerShell = !!process.env.PSModulePath

let historyPath = ''

if (isPowerShell) {
historyPath = path.join(
process.env.APPDATA || '',
'Microsoft/Windows/PowerShell/PSReadLine/ConsoleHost_history.txt'
)
} else if (shell.includes('zsh')) {
historyPath = path.join(process.env.HOME || '', '.zsh_history')
} else if (shell.includes('bash')) {
historyPath = path.join(process.env.HOME || '', '.bash_history')
} else if (shell.includes('fish')) {
historyPath = path.join(
process.env.HOME || '',
'.local/share/fish/fish_history'
)
} else {
return []
}

if (!fs.existsSync(historyPath)) return []

if (!fs.existsSync(historyPath)) return [];
const content = fs.readFileSync(historyPath, 'utf-8')

const content = fs.readFileSync(historyPath, "utf-8");
if (isPowerShell) {
return content
.split('\n')
.filter(Boolean)
.filter(cmd => !cmd.trim().startsWith('zap '))
}

if (shell.includes('fish')) {
return content
.split('\n')
.filter(line => line.startsWith('- cmd:'))
.map(line => line.replace('- cmd:', '').trim())
.filter(cmd => !cmd.startsWith('zap '))
}

return content
.split("\n")
.map((line) => line.split(";").pop() || "")
.split('\n')
.map(line => line.split(';').pop() || '')
.filter(Boolean)
.filter((cmd) => !cmd.trim().startsWith("zap "));
.filter(cmd => !cmd.trim().startsWith('zap '))
}

function reportDirectorySelection(dirPath: string) {
Expand Down Expand Up @@ -159,15 +195,11 @@ program
});

program
.command("init")
.argument("<shell>")
.action((shell: string) => {
if (shell !== "zsh") {
console.error(`Unsupported shell: ${shell}`);
process.exit(1);
}

console.log(`zap() {
.command("init")
.argument("<shell>")
.action((shell: string) => {
if (shell === "zsh") {
console.log(`zap() {
local cd_file
local exit_code
local target
Expand All @@ -187,8 +219,82 @@ program

rm -f "$cd_file"
return $exit_code
}`);
});
}
alias zap-search=zap`);

} else if (shell === "bash") {
console.log(`zap() {
local cd_file
local exit_code
local target

cd_file=$(mktemp)
ZAP_CD_FILE="$cd_file" command zap "$@"
exit_code=$?

if [[ $exit_code -eq 0 && -s "$cd_file" ]]; then
target=$(<"$cd_file")
if [[ "$target" == CMD:* ]]; then
READLINE_LINE="\${target#CMD:}"
READLINE_POINT=\${#READLINE_LINE}
elif [[ -d "$target" ]]; then
cd "$target"
fi
fi

rm -f "$cd_file"
return $exit_code
}
alias zap-search=zap`);

} else if (shell === "fish") {
console.log(`function zap
set cd_file (mktemp)
set -x ZAP_CD_FILE $cd_file
command zap $argv
set exit_code $status

if test $exit_code -eq 0 -a -s "$cd_file"
set target (cat "$cd_file")
if string match -q "CMD:*" "$target"
commandline (string replace "CMD:" "" "$target")
else if test -d "$target"
cd "$target"
end
end

rm -f "$cd_file"
return $exit_code
end
alias zap-search=zap`);

} else if (shell === "powershell") {
console.log(`function zap {
$cd_file = [System.IO.Path]::GetTempFileName()
$env:ZAP_CD_FILE = $cd_file
$zap_bin = (Get-Command zap -CommandType Application).Source
& $zap_bin @args
$exit_code = $LASTEXITCODE

if ($exit_code -eq 0 -and (Get-Item $cd_file).Length -gt 0) {
$target = Get-Content $cd_file
if ($target.StartsWith("CMD:")) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($target.Substring(4))
} elseif (Test-Path $target -PathType Container) {
Set-Location $target
}
}

Remove-Item $cd_file -Force
}
Set-Alias zap-search zap`);

} else {
console.error(`Unsupported shell: ${shell}`);
console.error(`Supported: zsh, bash, fish, powershell`);
process.exit(1);
}
});

program
.command('web')
Expand Down
Loading