Add FileSystem completion

This commit is contained in:
glacambre 2018-10-11 16:55:01 +02:00 committed by Oliver Blanthorn
parent a555c6a330
commit 698fc6ad36
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
5 changed files with 98 additions and 4 deletions

View file

@ -14,7 +14,7 @@ import time
import unicodedata
DEBUG = False
VERSION = "0.1.9"
VERSION = "0.1.10"
class NoConnectionError(Exception):
@ -488,6 +488,16 @@ def handleMessage(message):
elif cmd == "win_firefox_restart":
reply = win_firefox_restart(message)
elif cmd == "list_dir":
path = os.path.expanduser(message.get("path"))
reply["sep"] = os.sep
reply["isDir"] = os.path.isdir(path)
if not reply["isDir"]:
path = os.path.dirname(path)
if not path:
path = "./"
reply["files"] = os.listdir(path)
else:
reply = {"cmd": "error", "error": "Unhandled message"}
eprint("Unhandled message: {}".format(message))

View file

@ -2,6 +2,7 @@
* Background functions for the native messenger interface
*/
import * as Messaging from "@src/lib/messaging"
import * as semverCompare from "semver-compare"
import * as config from "@src/lib/config"
import { browserBg } from "@src/lib/webext"
@ -16,6 +17,7 @@ type MessageCommand =
| "read"
| "write"
| "temp"
| "list_dir"
| "mkdir"
| "move"
| "eval"
@ -54,11 +56,10 @@ async function sendNativeMsg(
}
}
export async function getrcpath(): Promise <string> {
export async function getrcpath(): Promise<string> {
const res = await sendNativeMsg("getconfigpath", {})
if (res.code != 0)
throw new Error("getrcpath error: " + res.code)
if (res.code != 0) throw new Error("getrcpath error: " + res.code)
return res.content
}
@ -260,6 +261,10 @@ export async function move(from: string, to: string) {
return sendNativeMsg("move", { from, to })
}
export async function listDir(dir: string) {
return sendNativeMsg("list_dir", { path: dir })
}
export async function winFirefoxRestart(
profiledir: string,
browsercmd: string,
@ -582,3 +587,6 @@ export async function writePref(name: string, value: any) {
write(file, text.replace(substr, `pref("${name}", ${value})`))
}
}
import * as SELF from "@src/background/native_background"
Messaging.addListener("native_background", Messaging.attributeCaller(SELF))

View file

@ -8,6 +8,7 @@ import { BufferAllCompletionSource } from "@src/completions/BufferAll"
import { BufferCompletionSource } from "@src/completions/Buffer"
import { BmarkCompletionSource } from "@src/completions/Bmark"
import { ExcmdCompletionSource } from "@src/completions/Excmd"
import { FileSystemCompletionSource } from "@src/completions/FileSystem"
import { HelpCompletionSource } from "@src/completions/Help"
import { HistoryCompletionSource } from "@src/completions/History"
import { SettingsCompletionSource } from "@src/completions/Settings"
@ -66,6 +67,7 @@ function enableCompletions() {
new BufferAllCompletionSource(completionsDiv),
new BufferCompletionSource(completionsDiv),
new ExcmdCompletionSource(completionsDiv),
new FileSystemCompletionSource(completionsDiv),
new HelpCompletionSource(completionsDiv),
new HistoryCompletionSource(completionsDiv),
new SettingsCompletionSource(completionsDiv),

View file

@ -0,0 +1,73 @@
import * as Completions from "@src/completions"
import * as Messaging from "@src/lib/messaging"
import * as config from "@src/lib/config"
class FileSystemCompletionOption extends Completions.CompletionOptionHTML
implements Completions.CompletionOptionFuse {
public fuseKeys = []
constructor(public value: string) {
super()
this.fuseKeys = [value]
this.html = html`<tr class="FileSystemCompletionOption option">
<td class="value">${value}</td>
</tr>`
}
}
export class FileSystemCompletionSource extends Completions.CompletionSourceFuse {
public options: FileSystemCompletionOption[]
constructor(private _parent) {
super(["saveas"], "FileSystemCompletionSource", "FileSystem")
this._parent.appendChild(this.node)
}
public async onInput(exstr) {
this.filter(exstr)
}
public async filter(exstr: string) {
if (!exstr || exstr.indexOf(" ") == -1) {
this.state = "hidden"
return
}
let [cmd, path] = this.splitOnPrefix(exstr)
if (!path) path = "."
if (!["/", "$", "~", "."].find(s => path.startsWith(s))) {
// If the path doesn't start with a special character, it is relative to the native messenger, thus use "." as starting point
// Does this work on windows?
path = "./" + path
}
// Update lastExstr because we modified the path and scoreOptions uses that in order to assign scores
this.lastExstr = cmd + path
let req
try {
req = await Messaging.message("native_background", "listDir", [
path,
])
} catch (e) {
// Failing silently because we can't nativegate (the user is typing stuff in the commandline)
this.state = "hidden"
return
}
if (req.isDir) {
if (!path.endsWith(req.sep)) path += req.sep
} else {
path = path.substring(0, path.lastIndexOf("/") + 1)
}
this.options = req.files.map(
p => new FileSystemCompletionOption(path + p),
)
this.state = "normal"
this.updateChain()
}
}

View file

@ -11,6 +11,7 @@ export type NonTabMessageType =
| "commandline_background"
| "controller_background"
| "browser_proxy_background"
| "native_background"
| "download_background"
| "performance_background"
export type MessageType = TabMessageType | NonTabMessageType