Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Don't use printf for escaping for performance reasons.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed May 12, 2017
1 parent 9e8596c commit 93d5c3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/plugins/autocompletion_utils/Common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {statsIn, resolveDirectory, directoryName, joinPath, isImage} from "../../utils/Common";
import {statsIn, resolveDirectory, directoryName, joinPath, isImage, escapeFilePath} from "../../utils/Common";
import {
FileInfo, AutocompletionContext, AutocompletionProvider,
} from "../../Interfaces";
Expand All @@ -9,7 +9,6 @@ import {fontAwesome} from "../../views/css/FontAwesome";
import {colors} from "../../views/css/colors";
import {CSSObject} from "../../views/css/definitions";
import {StatusCode} from "../../utils/Git";
import {executeCommand} from "../../PTY";
import {ASTNode, leafNodeAt, serializeReplacing} from "../../shell/Parser";

type Style = { value: string; css: CSSObject};
Expand Down Expand Up @@ -227,19 +226,19 @@ const filesSuggestions = (filter: (info: FileInfo) => boolean) => async(tokenVal
.filter(info => info.name.startsWith(".") ? basePath.startsWith(".") : true)
.filter(info => info.stat.isDirectory() || filter(info))
.map(async info => {
// Shell out to printf to ecape the filename, it is the only thing that truly knows how to do that
const escapedName: string = await executeCommand("printf", ['"%q"', `"${info.name}"`], "/");
const escapedName: string = escapeFilePath(info.name);

if (info.stat.isDirectory()) {
return new Suggestion({
value: joinPath(tokenDirectory, escapedName + Path.sep),
displayValue: escapedName + Path.sep,
displayValue: info.name + Path.sep,
style: styles.directory,
promptSerializer: noEscapeSpacesPromptSerializer,
});
} else {
return new Suggestion({
value: joinPath(tokenDirectory, escapedName),
displayValue: escapedName,
displayValue: info.name,
style: styles.file(info, joinPath(directoryPath, escapedName)),
promptSerializer: noEscapeSpacesPromptSerializer,
});
Expand Down
4 changes: 4 additions & 0 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ export function mapObject<T, R>(object: Dictionary<T>, mapper: (key: string, val
return result;
}

export function escapeFilePath(unescaped: string): string {
return unescaped.replace(/([\s'"\[\]<>#$%^&*()])/g, "\\$1");
}

const baseConfigDirectory = Path.join(homeDirectory, ".black-screen");
export const presentWorkingDirectoryFilePath = Path.join(baseConfigDirectory, "presentWorkingDirectory");
export const historyFilePath = Path.join(baseConfigDirectory, "history");
Expand Down

0 comments on commit 93d5c3d

Please sign in to comment.