Skip to content

Commit

Permalink
Merge pull request #1 from roose/dev
Browse files Browse the repository at this point in the history
Add context menu
  • Loading branch information
roose authored Oct 17, 2020
2 parents 2b58e0c + ca57c1c commit c7d8ac5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ Temporary Items
nimcache
*.exe
*.wox
*.zip
Binary file added Images/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/npm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ Download latest plugin(\*.wox/\*.flowlauncher file) from [releases](https://gith
## Screenshot

![example](https://habrastorage.org/files/6cf/ccd/643/6cfccd643a1243558dea491883837d93.png)

### Context menu(<kbd>Shift</kbd>+<kbd>Enter</kbd>):

![demo](data/demo.png)
Binary file added data/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 60 additions & 10 deletions npms.nim
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
import os, strutils, httpclient, json, browsers, cgi, future
import "../../code/nim-wox/src/wox"
import os, strutils, httpclient, json, browsers, cgi, cliptomania
import wox

proc parsePackage(n: JsonNode): tuple[title, desc, url: string] =
type
Package = object
name: string
title: string
desc: string
github: string
npm: string

const
ico = "Images\\npms.png"
url = "https://api.npms.io/v2/search?q="
icons = @["copy.png", "npm.png", "npms.png"]

proc getWoxDirs(): seq[string] =
var dirs: seq[string]
for kind, path in walkDir(joinPath([getEnv("LOCALAPPDATA"), "Wox"])):
if kind == pcDir and lastPathPart(path).startsWith("app"):
dirs.add(path)
return dirs

proc copyIcons(paths: seq[string]) =
for icon in icons:
for dir in getWoxDirs():
let imageDir = joinPath([dir, "Images"])
if dirExists(imageDir):
copyFile(joinPath([getAppDir(), "Images", icon]), joinPath([imageDir, icon]))

proc existsWoxIcon(icon: string): bool =
for dir in getWoxDirs():
if fileExists(joinPath([dir, "Images", icon])):
return true

proc parsePackage(n: JsonNode): Package =
let
p = n["package"]
name = p["name"].getStr
version = p["version"].getStr
username = p["publisher"]["username"].getStr
links = p["links"]
var
desc, url, flags = ""
desc, url, npm, flags = ""

if n.hasKey("flags"):
var flagsKeys: seq[string]
Expand All @@ -22,20 +54,20 @@ proc parsePackage(n: JsonNode): tuple[title, desc, url: string] =
else:
url = links["npm"].getStr

npm = links["npm"].getStr

if p.hasKey("description"):
desc =join(p["description"].getStr.split(), " ")

let
# desc =join(p["description"].getStr.split(), " ")
title = name & " v" & version & " by " & username & flags

return (title, desc, url)
return Package(name: name, title: title, desc: desc, github: url, npm: npm)

proc query(wp: Wox, params: varargs[string]) =
let
query = params[0].strip
ico = "Images\\npms.png"
url = "https://api.npms.io/v2/search?q="
params = encodeUrl(query)

if query == "":
Expand All @@ -50,20 +82,38 @@ proc query(wp: Wox, params: varargs[string]) =

let packages = wp.loadCache(params)
for package in packages["results"]:
let (title, desc, url) = parsePackage(package)
wp.add(title, desc, ico, "openUrl", url, false)
let package = parsePackage(package)
wp.add(package.title, package.desc, ico, package.name & " " & package.npm, "openUrl", package.github, false)

if wp.data.result.len == 0:
wp.add("No Results", "", ico, "", "", true)
wp.add("No Results", "", ico, "", "", "", true)

echo wp.results()

proc contextmenu(wp: Wox, params: varargs[string]) =
let
params = params[0].split(" ")
cmd = "npm install " & params[0].strip
link = params[1].strip
wp.add("Open npm page", "", "Images/npm.png", "", "openUrl", link, false)
wp.add("Copy install command", "", "Images/copy.png", "", "copyCmd", cmd, false)
echo wp.results()

proc openUrl(wp: Wox, params: varargs[string]) =
let url = params[0].strip
openDefaultBrowser(url)

proc copyCmd(wp: Wox, params: varargs[string]) =
let cmd = params[0].strip
clip.set_text(cmd)

when isMainModule:
if not existsWoxIcon("npms.png"):
copyIcons(icons)

var wp = newWox()
wp.register("query", query)
wp.register("contextmenu", contextmenu)
wp.register("openUrl", openUrl)
wp.register("copyCmd", copyCmd)
wp.run()
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Name": "npms search",
"Description": "Search for npm packages with npms.io",
"Author": "roose",
"Version": "0.1.2",
"Version": "1.0.0",
"Language": "executable",
"Website": "https://github.com/roose/wox-npms-search",
"IcoPath": "Images\\npms.png",
Expand Down

0 comments on commit c7d8ac5

Please sign in to comment.