summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/flameshot-once/profile.nix
blob: 4427e5b233d0faab78f9f15b0adebe3d1912e08e (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
with import <stockholm/lib>;
{ config, pkgs }:
let

  # Refs https://github.com/lupoDharkael/flameshot/blob/master/src/widgets/capture/capturebutton.h
  ButtonType = {
    PENCIL             = 0;
    DRAWER             = 1;
    ARROW              = 2;
    SELECTION          = 3;
    RECTANGLE          = 4;
    CIRCLE             = 5;
    MARKER             = 6;
    SELECTIONINDICATOR = 7;
    MOVESELECTION      = 8;
    UNDO               = 9;
    COPY               = 10;
    SAVE               = 11;
    EXIT               = 12;
    IMAGEUPLOADER      = 13;
    OPEN_APP           = 14;
    BLUR               = 15;
    REDO               = 16;
    PIN                = 17;
    TEXT               = 18;
  };

  cfg = eval.config;

  eval = evalModules {
    modules = singleton {
      _file = toString ./profile.nix;
      imports = singleton config;
      options = {
        buttons = mkOption {
          apply = map (name: ButtonType.${name});
          default = [
            "PENCIL"
            "DRAWER"
            "ARROW"
            "SELECTION"
            "RECTANGLE"
            "CIRCLE"
            "MARKER"
            "SELECTIONINDICATOR"
            "MOVESELECTION"
            "UNDO"
            "SAVE"
            "EXIT"
            "BLUR"
          ]
          ++ optional cfg.imgur.enable "IMAGEUPLOADER"
          ;
          type = types.listOf (types.enum (attrNames ButtonType));
        };
        disabledTrayIcon = mkOption {
          default = true;
          type = types.bool;
        };
        drawThickness = mkOption {
          default = 8;
          type = types.positive;
        };
        filenamePattern = mkOption {
          default = "%FT%T%z_flameshot";
          type =
            # This is types.filename extended by [%:][%:+]*
            types.addCheck types.str (test "[%:0-9A-Za-z._][%:+0-9A-Za-z._-]*");
        };
        imgur = mkOption {
          default = {};
          type = types.submodule {
            options = {
              enable = mkEnableOption "imgur";
              createUrl = mkOption {
                example = "http://p.r/image";
                type = types.str;
              };
              deleteUrl = mkOption {
                example = "http://p.r/image/delete/%1";
                type = types.str;
              };
              xdg-open = mkOption {
                default = {};
                type = types.submodule {
                  options = {
                    enable = mkEnableOption "imgur.xdg-open" // {
                      default = true;
                    };
                    browser = mkOption {
                      default = "${pkgs.coreutils}/bin/false";
                      type = types.str;
                    };
                    createPrefix = mkOption {
                      default = cfg.imgur.createUrl;
                      type = types.str;
                    };
                    deletePrefix = mkOption {
                      default = removeSuffix "/%1" cfg.imgur.deleteUrl;
                      type = types.str;
                    };
                  };
                };
              };
            };
          };
        };
        savePath = mkOption {
          default = "/tmp";
          type = types.absolute-pathname;
        };
        showDesktopNotification = mkOption {
          default = false;
          type = types.bool;
        };
        showHelp = mkOption {
          default = false;
          type = types.bool;
        };
        timeout = mkOption {
          default = 100;
          description = ''
            Maximum time in milliseconds allowed for the flameshot daemon to
            react.
          '';
          type = types.positive;
        };
      };
    };
  };

  hexchars = stringToCharacters "0123456789abcdef";

  # Encode integer to C-escaped string of bytes, little endian / LSB 0
  le = rec {
    x1 = i: let
      i0 = mod i 16;
      i1 = i / 16;
    in
      "\\x${elemAt hexchars i1}${elemAt hexchars i0}";

    x2 = i: let
      i0 = mod i 256;
      i1 = i / 256;
    in
      "${x1 i0}${x1 i1}";

    x4 = i: let
      i0 = mod i 65536;
      i1 = i / 65536;
    in
      "${x2 i0}${x2 i1}";
  };

  toQList = t: xs:
    assert t == "int";
    "QList<${t}>${le.x4 0}${le.x4 (length xs)}${concatMapStrings le.x4 xs}";

  XDG_CONFIG_HOME = pkgs.write "flameshot-config" {
    "/Dharkael/flameshot.ini".text = ''
      [General]
      buttons=@Variant(\0\0\0\x7f\0\0\0\v${toQList "int" cfg.buttons})
      disabledTrayIcon=${toJSON cfg.disabledTrayIcon}
      drawThickness=${toJSON cfg.drawThickness}
      filenamePattern=${toJSON cfg.filenamePattern}
      savePath=${toJSON cfg.savePath}
      showDesktopNotification=${toJSON cfg.showDesktopNotification}
      showHelp=${toJSON cfg.showHelp}
    '';
  };

in

  pkgs.writeDash "flameshot.profile" ''
    export FLAMESHOT_CAPTURE_PATH=${cfg.savePath}
    export FLAMESHOT_ONCE_TIMEOUT=${toString cfg.timeout}
    export XDG_CONFIG_HOME=${XDG_CONFIG_HOME}
    ${optionalString cfg.imgur.enable /* sh */ ''
      export IMGUR_CREATE_URL=${shell.escape cfg.imgur.createUrl}
      export IMGUR_DELETE_URL=${shell.escape cfg.imgur.deleteUrl}
      ${optionalString cfg.imgur.xdg-open.enable /* sh */ ''
        PATH=$PATH:${makeBinPath [
          (pkgs.writeDashBin "xdg-open" ''
            set -efu
            uri=$1
            prefix=$(${pkgs.coreutils}/bin/dirname "$uri")
            case $prefix in
              (${shell.escape cfg.imgur.xdg-open.createPrefix})
                echo "opening image in browser: $uri" >&2
                exec ${config.imgur.xdg-open.browser} "$uri"
                ;;
              (${shell.escape cfg.imgur.xdg-open.deletePrefix})
                echo "deleting image: $uri" >&2
                exec ${pkgs.curl}/bin/curl -fsS -X DELETE "$uri"
                ;;
              (*)
                echo "don't know how to open URI: $uri" >&2
                exit 1
            esac
          '')
        ]}
      ''}
    ''}
  ''