Skip to content

Commit 19d3f31

Browse files
committed
Add -e/--edit to neuron search. Resolves #43
1 parent 9082c42 commit 19d3f31

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- New features
66
- [Short links](https://neuron.srid.ca/2014501.html).
77
- Full text search: `neuron search --full-text`
8+
- #43: Add `neuron search -e` to open the matching zettel in $EDITOR
89
- Allow customizing output directory
910
- Added `neuron open` to open the locally generated Zettelkasten site.
1011
- CLI revamp

guide/2013501.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ neuron -d ~/notes search
1414

1515
This command will allow you to search your Zettels by title, and then print the matching zettel's filepath at the end.
1616

17-
You may pipe the command to your text editor in order to directly edit the matching Zettel:
17+
You may pipe the command to your text editor in order to directly edit the matching Zettel, or simply pass the `-e` option which opens the zettel in your $EDITOR:
1818

1919
```bash
20-
neuron search | xargs -rt vim
20+
neuron search -e
2121
```
2222

2323
[![asciicast](https://asciinema.org/a/313358.png)](https://asciinema.org/a/313358)

src-script/neuron-search/default.nix

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ pkgs.writeShellScriptBin "neuron-search"
99
NOTESDIR=''${1}
1010
FILTERBY=''${2}
1111
SEARCHFROMFIELD=''${3}
12+
OPENCMD=`${pkgs.envsubst}/bin/envsubst -no-unset -no-empty <<< ''${4}`
1213
cd ''${NOTESDIR}
1314
${pkgs.ripgrep}/bin/rg --no-heading --no-line-number --sort path "''${FILTERBY}" *.md \
1415
| ${pkgs.fzf}/bin/fzf --tac --no-sort -d ':' -n ''${SEARCHFROMFIELD}.. \
1516
--preview '${pkgs.bat}/bin/bat --style=plain --color=always {1}' \
1617
--bind 'ctrl-j:execute(xdg-open https://localhost:8080/(echo {1} | ${pkgs.gnused}/bin/sed "s/\.md/.html/g"))+abort' \
17-
| ${pkgs.gawk}/bin/awk -F: "{printf \"''${NOTESDIR}/%s\", \$1}"
18+
| ${pkgs.gawk}/bin/awk -F: "{printf \"''${NOTESDIR}/%s\", \$1}" \
19+
| xargs -r ''${OPENCMD}
1820
''

src/Neuron/Zettelkasten.hs

+12-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ import System.Which
3535
neuronSearchScript :: FilePath
3636
neuronSearchScript = $(staticWhich "neuron-search")
3737

38+
searchScriptArgs :: SearchCommand -> [String]
39+
searchScriptArgs SearchCommand {..} =
40+
let searchByArgs =
41+
case searchBy of
42+
SearchByTitle -> ["title: ", "3"]
43+
SearchByContent -> ["", "2"]
44+
editArg =
45+
bool "echo" "$EDITOR" searchEdit
46+
in searchByArgs <> [editArg]
47+
3848
run :: Action () -> IO ()
3949
run act = do
4050
defaultNotesDir <- (</> "zettelkasten") <$> getHomeDirectory
@@ -68,12 +78,8 @@ runWith act App {..} = do
6878
store <- Z.mkZettelStore =<< Rib.forEvery ["*.md"] pure
6979
let matches = Z.runQuery store queries
7080
putLTextLn $ Aeson.encodeToLazyText $ matches
71-
Search searchBy -> do
72-
let args =
73-
case searchBy of
74-
SearchByTitle -> ["title: ", "3"]
75-
SearchByContent -> ["", "2"]
76-
execScript neuronSearchScript $ notesDir : args
81+
Search searchCmd -> do
82+
execScript neuronSearchScript $ notesDir : searchScriptArgs searchCmd
7783
where
7884
execScript scriptPath args =
7985
-- We must use the low-level execvp (via the unix package's `executeFile`)

src/Neuron/Zettelkasten/CLI.hs

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Neuron.Zettelkasten.CLI
1111
Command (..),
1212
NewCommand (..),
1313
SearchBy (..),
14+
SearchCommand (..),
1415
commandParser,
1516
runRib,
1617
runRibOnceQuietly,
@@ -37,6 +38,12 @@ data App = App
3738
data NewCommand = NewCommand {title :: Text, edit :: Bool}
3839
deriving (Eq, Show)
3940

41+
data SearchCommand = SearchCommand
42+
{ searchBy :: SearchBy,
43+
searchEdit :: Bool
44+
}
45+
deriving (Eq, Show)
46+
4047
data SearchBy
4148
= SearchByTitle
4249
| SearchByContent
@@ -48,7 +55,7 @@ data Command
4855
| -- | Open the locally generated Zettelkasten
4956
Open
5057
| -- | Search a zettel by title
51-
Search SearchBy
58+
Search SearchCommand
5259
| -- | Run a query against the Zettelkasten
5360
Query [Z.Query]
5461
| -- | Delegate to Rib's command parser
@@ -116,7 +123,7 @@ commandParser defaultNotesDir = do
116123
command "rib" $ info ribCommand $ progDesc "Generate static site via rib"
117124
]
118125
newCommand = do
119-
edit <- switch (long "edit" <> short 'e' <> help "Open the newly-created file in $EDITOR")
126+
edit <- switch (long "edit" <> short 'e' <> help "Open the newly-created zettel in $EDITOR")
120127
title <- argument str (metavar "TITLE" <> help "Title of the new Zettel")
121128
return (New NewCommand {..})
122129
openCommand =
@@ -129,7 +136,8 @@ commandParser defaultNotesDir = do
129136
searchBy <-
130137
fmap (bool SearchByTitle SearchByContent) $
131138
switch (long "--full-text" <> short 'a' <> help "Full-text search")
132-
pure $ Search searchBy
139+
edit <- switch (long "edit" <> short 'e' <> help "Open the matching zettel in $EDITOR")
140+
pure $ Search $ SearchCommand searchBy edit
133141
ribCommand = fmap Rib $ do
134142
ribOutputDir <-
135143
optional $

0 commit comments

Comments
 (0)