]> njoseph.me Git - nimcoon.git/commitdiff
Allow searching from Emacs
authorJoseph Nuthalapati <njoseph@riseup.net>
Mon, 7 Sep 2020 07:49:52 +0000 (13:19 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Mon, 7 Sep 2020 08:49:03 +0000 (14:19 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
README.md
nimcoon.el
src/lib.nim
src/nimcoon.nim

index a8791803b4266ad633996ae632eec8d5e99fd0fd..6bac1ee7f0875ef043b8c8bcad0da57e52104937 100644 (file)
--- a/README.md
+++ b/README.md
@@ -20,7 +20,9 @@ I tried all kinds of alternative YouTube players for the desktop. Most are eithe
 
 YouTube's business incentive is to make you watch as many videos as possible. If you open the YouTube website and are logged into it, you will just get distracted by recommendations and forget why you opened it in the first place. You might have wanted to watch a conference talk but end up going down a rabbit hole of other "interesting" videos customized for you. 
 
-NimCoon has a spartan design. It doesn't even show images of the search results. I have had better success with managing my YouTube consumption after shifting to this tool. Pinafore's wellness settings are an inspiration for this.
+NimCoon has a spartan design. It doesn't even show images of the search results. It doesn't let you browse YouTube. You have to explicitly search for something.
+
+I have had better success with managing my YouTube consumption after shifting to this tool. Settings inspired by Pinafore's wellness settings.
 
 ### Why no issues or merge requests?
 
index 5fdb2c9ad8c5bdd5d173ad43dc3ee924994a95b7..5146a9198ddaa42bbafc01c3e91ef043afeda68b 100644 (file)
@@ -5,8 +5,8 @@
 ;;; (load! "nimcoon")
 
 ;;; Non-interactive functions to respond to URL clicks
-(defun nimcoon-play-url (url)
-  "Play given url in NimCoon"
+(defun nimcoon-play-url (url &rest args)
+  "Play given URL in NimCoon."
   (start-process "nimcoon" nil "nimcoon" url))
 
 ;; Play all YouTube URLs in NimCoon
        (("youtu\\.?be" . nimcoon-play-url)
         ("." . browse-url-default-browser))))
 
+(defun run-nimcoon(args query)
+  "Search by QUERY and play in NimCoon."
+  (call-process "nimcoon" nil 0 nil args query))
+
 ;;; Interactive functions
 (defun nimcoon-feeling-lucky-music(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-m" "-l" (format "'%s'" query)))
+  (run-nimcoon "-ml" query))
 
 (defun nimcoon-feeling-lucky-video(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-l" (format "'%s'" query)))
+  (run-nimcoon "-l" query))
 
 (defun nimcoon-download-video(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-d" "-l" (format "'%s'" query)))
+  (run-nimcoon "-dl" query))
 
 (defun nimcoon-download-music(query)
   (interactive "sSearch query: ")
-  (call-process "nimcoon" nil 0 nil "-d" "-l" "-m" (format "'%s'" query)))
+  (run-nimcoon "-dlm" query))
+
+;; Assumes only one process exists. Must capture the pid of the running NimCoon process and only kill it.
+(defun nimcoon-kill-background-processes()
+  "Kill NimCoon process running in the background. Useful for stopping background music."
+  (interactive)
+  (shell-command "kill `pgrep nimcoon` `pgrep mpv` `pgrep vlc`"))
+
+(defun nimcoon-search-video(query)
+  "Search for a video by QUERY."
+  (interactive "sSearch query: ")
+  (with-output-to-temp-buffer "*NimCoon search results*"
+    (call-process "nimcoon" nil "*NimCoon search results*" t "--non-interactive" query)
+    (with-current-buffer "*NimCoon search results*"
+      (org-mode))))
+
+(defun nimcoon-search-music(query)
+  "Search for a video by QUERY."
+  (interactive "sSearch query: ")
+  (with-output-to-temp-buffer "*NimCoon search results*"
+    (call-process "nimcoon" nil "*NimCoon search results*" t "--music" "--non-interactive" query)
+    (with-current-buffer "*NimCoon search results*"
+      (org-mode))))
 
 ;;; Keybindings
 (map! :leader
@@ -39,5 +65,9 @@
        (:prefix ("d" . "Download")
         :desc "Video" "v" #'nimcoon-download-video
         :desc "Music" "m" #'nimcoon-download-music)
+       (:prefix ("s" . "Search")
+        :desc "Video" "v" #'nimcoon-search-video
+        :desc "Music" "m" #'nimcoon-search-music)
+       :desc "Kill"  "k" #'nimcoon-kill-process
        :desc "Video" "v" #'nimcoon-feeling-lucky-video
        :desc "Music" "m" #'nimcoon-feeling-lucky-music))
index e242532bc53b558089747a477da9ae2ad1b41940..16082c5a6f8ef0936e3a28e650217932149f7fdb 100644 (file)
@@ -49,7 +49,6 @@ proc presentVideoOptions*(searchResults: SearchResults) =
   for index, (title, url) in searchResults:
     styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, "   ", url, "\n"
 
-
 func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
   let musicOnly = if options["musicOnly"]: "--no-video" else: ""
   let fullScreen = if options["fullScreen"]: "--fullscreen" else: ""
@@ -63,7 +62,7 @@ proc play*(player: string, options: Table[string, bool], url: string, title: str
   if "--no-video" in args:
     discard execShellCmd(&"{player} {args.join(\" \")}")
   else:
-    discard execProcess(player, args=args, options=processOptions)
+    discard startProcess(player, args=args, options=processOptions)
 
 
 func buildMusicDownloadArgs(url: string): seq[string] =
index f3b29c6d0a4892df888f3d67127f050cde403b21..e1cbb23bf562bf03cd851fd5b6a67f7d10a2cb94 100644 (file)
@@ -14,7 +14,7 @@ import
 proc parseArguments(): CommandLineOptions =
   var
     searchQuery = ""
-    options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
+    options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false, "non-interactive": false})
 
   for kind, key, value in getopt():
     case kind
@@ -26,12 +26,17 @@ proc parseArguments(): CommandLineOptions =
       of "l", "lucky": options["feelingLucky"] = true
       of "f", "full-screen": options["fullScreen"] = true
       of "d", "download": options["download"] = true
+      of "n", "non-interactive": options["non-interactive"] = true
     of cmdEnd: discard
 
+  if searchQuery == "":
+    stderr.writeLine "NimCoon doesn't permit browsing. You must provide a search query."
+    quit(1)
+
   (searchQuery, options)
 
 
-proc isValidOptions*(options: Options): bool =
+proc isValidOptions(options: Options): bool =
   # Check for invalid combinations of options
   var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
   result = true
@@ -56,6 +61,13 @@ proc main() =
     quit(0)
 
   let searchResults = getSearchResults(searchQuery)
+  if options["non-interactive"]:
+    for index, (title, url) in searchResults:
+      echo title
+      echo url
+      echo ""
+    quit(0)
+
   let numResults = min(limit, len(searchResults))
 
   present(searchResults, options, (0, numResults-1), player)