about summary refs log tree commit diff
path: root/pkgs/profpatsch/xdg-open/xdg-open.dhall
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2020-05-11 10:59:31 +0200
committerProfpatsch <mail@profpatsch.de>2020-05-11 10:59:31 +0200
commit0fcac5f717a501691993f6682f67d3e9be3ae75b (patch)
treecac11ef38e762b07d640ef241d6e9e16d04cc4bb /pkgs/profpatsch/xdg-open/xdg-open.dhall
parentcef9ff283a558c2757d8cd7d9af69591d640e4d1 (diff)
pkgs/profpatsch/xdg-open: make possible to parse protocols
This is now on par with the original script in
https://github.com/Profpatsch/dotfiles/blob/a25c6c419525bef7ef5985f664b058dc9eb919e9/scripts/scripts/xdg-open

Eventually it should probably migrate away from a generated bash
script, but for now it’s fine.
Diffstat (limited to 'pkgs/profpatsch/xdg-open/xdg-open.dhall')
-rw-r--r--pkgs/profpatsch/xdg-open/xdg-open.dhall117
1 files changed, 78 insertions, 39 deletions
diff --git a/pkgs/profpatsch/xdg-open/xdg-open.dhall b/pkgs/profpatsch/xdg-open/xdg-open.dhall
index 8d6648b2..a7daebe3 100644
--- a/pkgs/profpatsch/xdg-open/xdg-open.dhall
+++ b/pkgs/profpatsch/xdg-open/xdg-open.dhall
@@ -21,17 +21,17 @@ let foo =
 let renderMime = Text/concatSep "/"
 
 let shellEscapeCommand =
-        λ(shellEscape : Text → Text)
-      → λ(file : Text)
-      → λ(cmd : config.Command)
-      →   Text/concatSep
+      λ(shellEscape : Text → Text) →
+      λ(file : Text) →
+      λ(cmd : config.Command) →
+          Text/concatSep
             " "
             (   [ shellEscape cmd.exe ]
               # List/map
                   config.Arg
                   Text
-                  (   λ(arg : config.Arg)
-                    → merge
+                  ( λ(arg : config.Arg) →
+                      merge
                         { String = λ(t : Text) → shellEscape t
                         , Variable = λ(t : Text) → t
                         }
@@ -42,52 +42,91 @@ let shellEscapeCommand =
         : Text
 
 let repeatText =
-        λ(t : Text)
-      → λ(n : Natural)
-      → Natural/fold n Text (λ(t2 : Text) → t ++ t2) ""
+      λ(t : Text) →
+      λ(n : Natural) →
+        Natural/fold n Text (λ(t2 : Text) → t ++ t2) ""
 
 let Lines = { indent : Natural, lines : List Text }
 
 let prettyLines =
-        λ(lines : Lines)
-      → Text/concatMap
+      λ(lines : Lines) →
+        Text/concatMap
           Text
           (λ(line : Text) → repeatText " " lines.indent ++ line ++ "\n")
           lines.lines
 
 let xdg-open =
-      let case =
-              λ(shellEscape2 : Text → Text)
-            → λ(file2 : Text)
-            → λ(m : config.MimeMatch)
-            → [ "${renderMime m.match})"
+      let mimeMatcherCase =
+            λ(shellEscape2 : Text → Text) →
+            λ(file2 : Text) →
+            λ(m : config.MimeMatch) →
+              [ "${renderMime m.match})"
               , "${shellEscapeCommand shellEscape2 file2 m.cmd}"
               , ";;"
               ]
 
-      in    λ(bins : { get-mime-type : Executable })
-          → λ(write-dash : Text → Text → Executable)
-          → λ(shellEscape : Text → Text)
-          → λ(pkgs : { package : Text, binary : Text } → Executable)
-          → λ(special : config.Special)
-          → write-dash
+      let mimeGlobCase =
+            λ(g : config.UriMimeGlob) →
+                List/concatMap
+                  Text
+                  Text
+                  ( λ(match : Text) →
+                      [ "${match})", "mime=${renderMime g.mime}", ";;" ]
+                  )
+                  g.glob
+              : List Text
+
+      in  λ(bins : { get-mime-type : Executable }) →
+          λ(write-dash : Text → Text → Executable) →
+          λ(shellEscape : Text → Text) →
+          λ(pkgs : { package : Text, binary : Text } → Executable) →
+          λ(special : config.Special) →
+            write-dash
               "xdg-open"
-              ''
-              set -e
-              file="$1"
-              mime=$(${bins.get-mime-type} "$file")
-
-              case "$mime" in
-              ${prettyLines
-                  { indent = 2
-                  , lines =
-                      List/concatMap
-                        config.MimeMatch
-                        Text
-                        (case shellEscape "\"\$file\"")
-                        (config.mimeMatcher pkgs special)
-                  }}
-              esac
-              ''
+              (     ''
+                    # TODO: --dry-run to display what would be opened and why
+
+                    # partially taken from
+                    # https://github.com/march-linux/mimi/blob/master/xdg-open
+
+                    set -e
+                    file="$1"
+                    case "$file" in
+                    ${prettyLines
+                        { indent = 2
+                        , lines =
+                            List/concatMap
+                              config.UriMimeGlob
+                              Text
+                              mimeGlobCase
+                              config.uriMimeGlobs
+                        }}
+                      *)
+                        # it’s a file
+
+                        # strip possible protocol
+                        file=''
+                ++  "\$"
+                ++  ''
+                    {file#file://}
+                        mime=$(file -E --brief --mime-type "$file") \
+                        || (echo "$mime" 1>&2; exit 1)
+                        # ^ echo the error message of file
+                        ;;
+                    esac
+
+                    case "$mime" in
+                    ${prettyLines
+                        { indent = 2
+                        , lines =
+                            List/concatMap
+                              config.MimeMatch
+                              Text
+                              (mimeMatcherCase shellEscape "\"\$file\"")
+                              (config.mimeMatcher pkgs special)
+                        }}
+                    esac
+                    ''
+              )
 
 in  xdg-open