From 3d67a84ff31be83bee86a1de6deb590648656de0 Mon Sep 17 00:00:00 2001 From: Gabriel Volpe Date: Wed, 13 Oct 2021 16:28:42 +0200 Subject: [PATCH] escape quotes: fixes #48 --- data/keybindings.settings | 4 ++++ output/keybindings.nix | 15 +++++++++++++++ src/Nix.hs | 14 ++++++++++++-- test/DConf2NixTest.hs | 10 ++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 data/keybindings.settings create mode 100644 output/keybindings.nix diff --git a/data/keybindings.settings b/data/keybindings.settings new file mode 100644 index 0000000..0f99c90 --- /dev/null +++ b/data/keybindings.settings @@ -0,0 +1,4 @@ +[/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2] +binding='space' +command='gnome-terminal -e "vim --cmd startinsert"' +name='Launch scratchpad' diff --git a/output/keybindings.nix b/output/keybindings.nix new file mode 100644 index 0000000..512053f --- /dev/null +++ b/output/keybindings.nix @@ -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 = "space"; + command = "gnome-terminal -e \"vim --cmd startinsert\""; + name = "Launch scratchpad"; + }; + + }; +} diff --git a/src/Nix.hs b/src/Nix.hs index e114915..eb23656 100644 --- a/src/Nix.hs +++ b/src/Nix.hs @@ -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 @@ -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' diff --git a/test/DConf2NixTest.hs b/test/DConf2NixTest.hs index 67c8101..5e276c7 100644 --- a/test/DConf2NixTest.hs +++ b/test/DConf2NixTest.hs @@ -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)