From b2ff96aa9e26297cbd7e7ee38d66eba3f735d20c Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 6 May 2025 23:18:14 +0100 Subject: [PATCH] Add bookmark this script Based on Luke Smith's script created during his "Bookmarking for Unix Chads (For Browsers, Terminals, IDEs and everything else)" video. https://videos.lukesmith.xyz/w/o5fiME4oJMdVzwSu7mA9mc --- home/opdavies/PW05CH3L.nix | 1 + home/opdavies/t480.nix | 1 + modules/home-manager/cli/bookmarkthis.nix | 68 +++++++++++++++++++++++ modules/home-manager/cli/default.nix | 1 + overlays/mods/dwm/default.nix | 8 ++- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 modules/home-manager/cli/bookmarkthis.nix diff --git a/home/opdavies/PW05CH3L.nix b/home/opdavies/PW05CH3L.nix index 243dfb50..637b3cdb 100644 --- a/home/opdavies/PW05CH3L.nix +++ b/home/opdavies/PW05CH3L.nix @@ -4,6 +4,7 @@ imports = [ ../common ]; cli = { + bookmarkthis.enable = true; direnv.enable = true; fzf.enable = true; diff --git a/home/opdavies/t480.nix b/home/opdavies/t480.nix index 337eb367..76f149e9 100644 --- a/home/opdavies/t480.nix +++ b/home/opdavies/t480.nix @@ -5,6 +5,7 @@ cli = { bluetuith.enable = true; + bookmarkthis.enable = true; direnv.enable = true; fzf.enable = true; diff --git a/modules/home-manager/cli/bookmarkthis.nix b/modules/home-manager/cli/bookmarkthis.nix new file mode 100644 index 00000000..f312198e --- /dev/null +++ b/modules/home-manager/cli/bookmarkthis.nix @@ -0,0 +1,68 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + inherit (lib) mkEnableOption mkOption; + + cfg = config.cli.${name}; + name = "bookmarkthis"; +in +{ + options.cli.${name} = { + enable = mkEnableOption "Enable ${name}"; + + snippetsFile = mkOption { + default = "${config.xdg.dataHome}/snippets.txt"; + type = lib.types.str; + }; + }; + + config = lib.mkIf cfg.enable { + home = { + packages = with pkgs; [ + (pkgs.writeShellApplication { + name = "bookmarkthis"; + + text = '' + message() { + if command -v ${lib.getExe libnotify} > /dev/null; then + ${lib.getExe libnotify} "$1" "$2" + else + echo "$2" + fi + } + + main() { + bookmark="$(xclip -o)" + file="$SNIPPETS_FILE" + + if grep -q "^$bookmark$" "$file"; then + message "Oops." "Already bookmarked." >&2 + + exit 2 + fi + + echo "$bookmark" >> "$file" + + message "Bookmark added!" "$bookmark is now saved to the file." + } + + main + ''; + }) + ]; + + sessionVariables = { + SNIPPETS_FILE = cfg.snippetsFile; + }; + }; + + programs.zsh.zsh-abbr.abbreviations = lib.optionalAttrs (config.programs.zsh.enable) { + "sn" = "${config.home.sessionVariables.EDITOR} $SNIPPETS_FILE"; + }; + }; +} diff --git a/modules/home-manager/cli/default.nix b/modules/home-manager/cli/default.nix index 084c35b0..2c3a2ede 100644 --- a/modules/home-manager/cli/default.nix +++ b/modules/home-manager/cli/default.nix @@ -1,6 +1,7 @@ { imports = [ ./bluetuith.nix + ./bookmarkthis.nix ./dev-commit.nix ./direnv.nix ./fzf.nix diff --git a/overlays/mods/dwm/default.nix b/overlays/mods/dwm/default.nix index 5da95c2e..484bf89b 100644 --- a/overlays/mods/dwm/default.nix +++ b/overlays/mods/dwm/default.nix @@ -5,6 +5,9 @@ let in prev.dwm.override { conf = '' + #define BROWSER "${lib.getExe prev.librewolf}" + #define TERMINAL "${lib.getExe prev.st}" + static const unsigned int borderpx = 1; static const unsigned int snap = 32; static const int showbar = 1; @@ -51,7 +54,7 @@ prev.dwm.override { static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */ static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL }; - static const char *termcmd[] = { "st", NULL }; + static const char *termcmd[] = { TERMINAL, NULL }; static const Key keys[] = { { MODKEY, XK_p, spawn, {.v = dmenucmd } }, @@ -86,13 +89,14 @@ prev.dwm.override { TAGKEYS(XK_7, 6) TAGKEYS(XK_8, 7) TAGKEYS(XK_9, 8) - { MODKEY|ShiftMask, XK_b, spawn, SHCMD("${lib.getExe prev.librewolf}") }, + { MODKEY|ShiftMask, XK_b, spawn, {.v = (const char*[]){ "/etc/profiles/per-user/opdavies/bin/bookmarkthis", NULL }}}, { MODKEY|ShiftMask, XK_f, spawn, SHCMD("${prev.xfce.thunar}/bin/thunar") }, { MODKEY|ShiftMask, XK_i, spawn, SHCMD("${prev.xdotool}/bin/xdotool type $(cat ~/.local/share/snippets.txt | grep -v '^#' | grep -v '^$' | sort | dmenu -i -l 50 | cut -d' ' -f1)") }, { MODKEY|ShiftMask, XK_p, spawn, {.v = (const char*[]){ "${lib.getExe prev.passmenu-otp}", "-i", NULL} } }, { MODKEY|ShiftMask, XK_q, quit, {0} }, { MODKEY|ShiftMask, XK_r, quit, {1} }, { MODKEY|ShiftMask, XK_s, spawn, SHCMD("${prev.flameshot}/bin/flameshot gui") }, + { MODKEY, XK_w, spawn, SHCMD(BROWSER) }, { MODKEY, XK_F3, spawn, {.v = (const char*[]){ "${lib.getExe prev.displayselect}", NULL} } }, { MODKEY, XK_F9, spawn, {.v = (const char*[]){ "/home/opdavies/.local/bin/mounter", NULL } } }, { MODKEY, XK_F10, spawn, {.v = (const char*[]){ "/home/opdavies/.local/bin/unmounter", NULL } } },