about summary refs log tree commit diff
path: root/pkgs/profpatsch/xdg-open/xdg-open.dhall
blob: cbd9654ed3cd4e85aeb147585d4722630595324f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
let Text/concatSep = ./imports/Prelude/Text/concatSep

let Text/concatMap = ./imports/Prelude/Text/concatMap

let List/concatMap = ./imports/Prelude/List/concatMap

let List/map = ./imports/Prelude/List/map

let
    -- TODO use library like with shell commands
    Executable =
      Text

let config = ./config.dhall

let foo =
      { match = [ "image", "png" ]
      , cmd = λ(_ : Text) → { exe = "echo", args = [ "foo" ] }
      }

let renderMime = Text/concatSep "/"

let shellEscapeCommand =
        λ(shellEscape : Text → Text)
      → λ(file : Text)
      → λ(cmd : config.Command)
      →   Text/concatSep
            " "
            (   [ shellEscape cmd.exe ]
              # List/map
                  config.Arg
                  Text
                  (   λ(arg : config.Arg)
                    → merge
                        { String = λ(t : Text) → shellEscape t
                        , Variable = λ(t : Text) → t
                        }
                        arg
                  )
                  (cmd.args file)
            )
        : Text

let repeatText =
        λ(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
          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})"
              , "${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
              "xdg-open"
              ''
              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
              ''

in  xdg-open