forked from withfig/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOTSLASH] Prioritize executable files (withfig#290)
* Prioritizze executables in file lists for dotslash * Remove characters from ls -F * Remove characters from ls -F
- Loading branch information
Showing
1 changed file
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,10 +1,55 @@ | ||
// use to complete on files when they are the first string in a branch | ||
const dotslashGenerator: Fig.Generator = { | ||
script: function (context) { | ||
let path = context[context.length - 1]; | ||
|
||
if (path.includes("/")) { | ||
const components = path.split("/"); | ||
components.pop(); | ||
path = components.join("/"); | ||
} else { | ||
path = "."; | ||
} | ||
|
||
// escape spaces in path | ||
path = path.split(" ").join("\\"); | ||
|
||
// output full filepath for each item in `path` folder | ||
return `ls -1AF ${path} | xargs -I '{}' echo $PWD/{} `; | ||
}, | ||
|
||
postProcess: function (files) { | ||
return files.split("\n").map((path) => { | ||
const components = path.split("/"); | ||
let filename = components[components.length - 1]; | ||
const isExecutable = path.endsWith("*"); | ||
const isFolder = path.endsWith("/"); | ||
|
||
if (isFolder) { | ||
filename = components[components.length - 2] + "/"; | ||
} | ||
|
||
// Removes character at the end of the filepath | ||
if (["@", "*", "%", "|"].includes(path.slice(-1))) { | ||
filename = filename.slice(0, -1); | ||
} | ||
|
||
return { | ||
name: filename, | ||
icon: "fig:https://" + path, | ||
description: isFolder ? "folder" : "file", | ||
priority: isExecutable && 80, | ||
}; | ||
}); | ||
}, | ||
trigger: "/", | ||
filterTerm: "/", | ||
}; | ||
|
||
export const completionSpec: Fig.Spec = { | ||
name: "dotslash", | ||
args: { | ||
// This was previously just "filepaths", however, we added folders so | ||
// users of ohmyzsh could cd into a folder by typing the folder name without `cd` | ||
template: ["filepaths", "folders"], | ||
generators: [dotslashGenerator], | ||
}, | ||
}; |