Skip to content

Commit

Permalink
escape quotes: fixes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
gvolpe committed Oct 13, 2021
1 parent 3161c4d commit 3d67a84
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions data/keybindings.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2]
binding='<Super>space'
command='gnome-terminal -e "vim --cmd startinsert"'
name='Launch scratchpad'
15 changes: 15 additions & 0 deletions output/keybindings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated via dconf2nix: https://github.com/gvolpe/dconf2nix
{ lib, ... }:

with lib.hm.gvariant;

{
dconf.settings = {
"/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2" = {
binding = "<Super>space";
command = "gnome-terminal -e \"vim --cmd startinsert\"";
name = "Launch scratchpad";
};

};
}
14 changes: 12 additions & 2 deletions src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@ renderEntry (Entry h c) root =
close = mkSpaces 4 <> "};\n\n"
in Nix $ header <> T.pack body <> close

escapeQuotes :: T.Text -> T.Text
escapeQuotes = T.replace "\"" "\\\""

strip :: T.Text -> T.Text
strip t | T.isPrefixOf "'" t = strip $ f (T.drop 1 t)
| T.isSuffixOf "'" t = f (T.dropEnd 1 t)
| otherwise = f t
where f = T.strip

renderValue :: Value -> Nix
renderValue raw = Nix $ renderValue' raw <> ";"
where
renderValue' (S v) = "\"" <> T.strip v <> "\""
renderValue' (S v) = "\"" <> escapeQuotes (strip v) <> "\""
renderValue' (B v) = T.toLower . T.pack $ show v
renderValue' (I v) = T.pack $ show v
renderValue' (D v) = T.pack $ show v
Expand All @@ -59,7 +68,8 @@ renderValue raw = Nix $ renderValue' raw <> ";"
renderValue' (T x y) =
let wrapNegNumber x | x < 0 = "(" <> T.pack (show x) <> ")"
| otherwise = T.pack $ show x
mkTuple x' y' = "mkTuple [ " <> wrapNegNumber x' <> " " <> wrapNegNumber y' <> " ]"
mkTuple x' y' =
"mkTuple [ " <> wrapNegNumber x' <> " " <> wrapNegNumber y' <> " ]"
in case (x, y) of
(I x', I y') -> mkTuple x' y'
(D x', D y') -> mkTuple x' y'
Expand Down
10 changes: 10 additions & 0 deletions test/DConf2NixTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,15 @@ dconf2nixClocks =
prop_dconf2nix_clocks :: Property
prop_dconf2nix_clocks = withTests (10 :: TestLimit) dconf2nixClocks

dconf2nixKeybindings :: Property
dconf2nixKeybindings =
let input = "data/keybindings.settings"
output = "output/keybindings.nix"
root = Root T.empty
in baseProperty input output root

prop_dconf2nix_keybindings :: Property
prop_dconf2nix_keybindings = withTests (10 :: TestLimit) dconf2nixKeybindings

dconf2nixTests :: Group
dconf2nixTests = $$(discover)

0 comments on commit 3d67a84

Please sign in to comment.