Skip to content

Instantly share code, notes, and snippets.

@roose roose/winscp.nim
Created Jul 28, 2016

Embed
What would you like to do?
import os, osproc, parsecfg, streams, strutils, json, winlean
import wox, registry
var wp = newWox()
proc getSessions(): seq[string] =
result = @[]
let iniPath = joinPath(getConfigDir(), "winscp.ini")
if fileExists(iniPath):
var
cfg = readFile(iniPath)
p: CfgParser
open(p, newStringStream(cfg), "")
while true:
var e = next(p)
case e.kind
of cfgEof:
break
of cfgSectionStart:
if e.section.startsWith("Sessions"):
result.add(e.section.split("\\")[1])
else:
continue
close(p)
else:
var h: RegHandle
try:
h = open("HKEY_CURRENT_USER\\SOFTWARE\\Martin Prikryl\\WinSCP 2\\Sessions", samRead)
for item in h.enumSubkeys:
if not item.startsWith("Default"):
result.add(item)
except:
discard
proc getPath(s: string): string =
result = s
result.delete(0, 6)
return result.strip
proc addPath(name: string) =
var j = parseJson(
"""{"method": "Wox.ChangeQuery","parameters":["scp addpath enter_path_to_winscp", true]}"""
)
echo j
proc savePath(path: string) =
writeFile("blabla.txt", "newpath: " & path)
wp.settings["path"] = newJString(expandFilename(path))
wp.saveSettings()
var j = parseJson(
"""{"method": "Wox.ChangeQuery","parameters":["scp", true]}"""
)
echo j
proc openSession(session: string) =
let
path = wp.settings["path"]
discard startProcess(path.getStr, args = [session])
# discard shellExecuteW(
# 0'i32,
# nil,
# newWideCString(path.getStr),
# newWideCString(session),
# nil,
# 0)
proc query(query: string) =
if not wp.settings.hasKey("path"):
wp.settings["path"] = newJString(getEnv("programfiles(x86)") / "WinSCP" / "WinSCP2.exe")
wp.saveSettings()
if (not fileExists(wp.settings["path"].getStr)) and (not query.startsWith("addpath")):
wp.add(
"Please enter a WinSCP.exe path",
"Can't find WinSCP.exe",
"Images\\winscp.png",
"addPath",
"addpath",
true
)
echo wp.results()
elif query.startsWith("addpath") and query.split.len > 1:
var newPath = getPath(query)
if fileExists(newPath):
wp.add(
"Save path",
"Please enter to save path",
"Images\\winscp.png",
"savePath",
newPath,
true
)
echo wp.results()
else:
wp.add(
"Please enter a valid WinSCP.exe path",
"Can't find WinSCP.exe",
"Images\\winscp.png",
"",
"",
true
)
echo wp.results()
else:
var sessions = getSessions()
for session in sessions:
wp.add(
session,
"Open " & session & " in WinSCP",
"Images\\winscp.png",
"openSession",
session,
false
)
echo wp.results()
when isMainModule:
register("query", query)
register("addPath", addPath)
register("savePath", savePath)
register("openSession", openSession)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.