Skip to content

Instantly share code, notes, and snippets.

@robinknowles
Created November 17, 2023 14:26
Show Gist options
  • Save robinknowles/ab5e5b0c841cc7ce7c805eb39cac90bc to your computer and use it in GitHub Desktop.
Save robinknowles/ab5e5b0c841cc7ce7c805eb39cac90bc to your computer and use it in GitHub Desktop.
Using fzf to fuzzy-find OpenFOAM features

Fuzzy finding 'FOAM features

Accompanies OnCFD Newsletter #173

A couple of code snippets that use fzf to mine your install & extract the answers to everyday OpenFOAM questions, like...

  • How do I use a particular feature?
  • What does so-and-so dictionary look like?
  • Does this need a semi-colon? 🤦‍♂️

It looks like this...

oncfd-173-fzf-OpenFOAM

Here are the code snippets...

fzf + grep + less version

grep \
--line-buffered --line-number --color=never \
--recursive --binary-files=without-match '' $FOAM_TUTORIALS |
fzf \
--delimiter : \
--preview 'less {1}' \
--preview-window 'up,75%,border,+{2}/3'

fzf + ripgrep + bat version

rg \
--color=always --line-number --no-heading \
--smart-case '' $FOAM_TUTORIALS |
fzf --ansi \
--color 'hl:-1:underline,hl+:-1:underline:reverse' \
--delimiter : \
--preview 'bat -lcpp --color=always {1} --highlight-line {2}' \
--preview-window 'up,75%,border,+{2}+3/3,~3'

That should save you some typing, but make sure you check out the original article for more info 👋

@cloner0110
Copy link

It's not working, at least for me, on macOS.

@robinknowles
Copy link
Author

I'll check 'em out again on my Mac - any error messages?

@cloner0110
Copy link

Thanks for response, I just alias this :

rg \
--color=always --line-number --no-heading \
--smart-case '' $FOAM_TUTORIALS |
fzf --ansi \
--color 'hl:-1:underline,hl+:-1:underline:reverse' \
--delimiter : \
--preview 'bat -lcpp --color=always {1} --highlight-line {2}' \
--preview-window 'up,75%,border,+{2}+3/3,~3'

to something, for example command, then when i execute command, I see lots of permission denied messages, you can see some in the following image.
Screenshot 2024-01-17 at 00 08 29

@robinknowles
Copy link
Author

rg seems to be looking in some odd places - perhaps you don't have the $FOAM_TUTORIALS environment variable set

@abzrg
Copy link

abzrg commented Jun 12, 2024

Thanks for sharing!

I tweaked it a bit:

function gtfo() {
  test -z "$WM_PROJECT_DIR" \
  && { echo "OpenFOAM environment is not loaded." && return 1 ;}

  ( cd $FOAM_TUTORIALS || exit # Assert

    # [ 1st version: grep ]
    # ggrep                                                                  \
    #   --no-messages                                                        \
    #   --line-buffered --line-number --color=never                          \
    #   --binary-files=without-match ''                                      \
    #   --recursive                                                          \
    #   --exclude={'*.m4','*.inp','*.sh','*.py','*plot*','*All*','*Test*'}   \
    #   --exclude={'*otape17*','*list-worlds*','*.schema','*validation'}     \
    #   --exclude={'*README*'}                                               \
    #   --exclude-dir={'*geometry*','*polyMesh*','*dataset*','*triSurface*'} \

    # [ 2nd version: find, xargs, grep ]
    # find . -type f                                                   \
    #   -not \( -path '*.m4*' -o -path "*.csv" \)                      \
    #   -not \( -path '*geometry*' -prune -o -path "dataset" -prune \) \
    #   -type f -print0                                                \

    find . -type f                                \
      -not \(                                     \
          -path "*polyMesh*" -prune               \
          -o -path '*triSurface*' -prune          \
          -o -path '*geometry*' -prune            \
          -o -path "*dataset*" -prune             \
          -o -path '*validation*' -prune          \
          -o -path "*All*"                        \
          -o -path '*.sh'                         \
          -o -path "*.py"                         \
          -o -path '*README*'                     \
          -o -path '*.m4*'                        \
          -o -path "*plot*"                       \
          -o -path "*list-worlds*"                \
          -o -path "*.schema"                     \
          -o -path "*.csv"                        \
          -o -path '*otape17*'                    \
          -o -path "*Test*"                       \
      \) -type f -print0                          \
    | xargs -0 --max-procs 0  --no-run-if-empty   \
        ggrep --line-buffered --line-number       \
          --binary-files=without-match --color "" \
    | sk --ansi                                     \
      --query "$1"                                  \
      --delimiter ':' --nth=3                       \
      --preview '                                   \
          test -n {} && zless {1}                   \
          | nl -ba                                  \
          | perl -pe "s/"{2}"/>/g if "{2}" .. "{2}' \
      --preview-window 'up:70%:+{2}-/3'
  )
}

I was trying to use GNU parallel instead of xargs to concurrently grep for the query, but wasn't successful.

@robinknowles
Copy link
Author

Nice work @abzrg - much better than mine 😉

@cloner0110
Copy link

rg seems to be looking in some odd places - perhaps you don't have the $FOAM_TUTORIALS environment variable set

Sorry for the late reply on this; I solved the problem by migrating to lazyVim, I believe its possible to integrate yours into lazyVim too :D

@robinknowles
Copy link
Author

Glad it was the start of something useful @cloner0110 🙌

@abzrg
Copy link

abzrg commented Jun 14, 2024

All credit to you 🙏

Well, thanks to this idea, I thought about something similar: quickly looking at the OpenFOAM source code as well. Not like greping in all the source code, but let's say I want to look at the createFields.H in the icoFoam solver, Or I want to know how sphereDrag force in lagrangian/intermediate library is implemented. Basically I want to quickly get access to certain files.

I wrote a script for that, which is available in this gist.
Although it works well, It's a bit hacky at the moment :)

@robinknowles
Copy link
Author

Doesn't look hacky to me - nice work @abzrg

@otaolafranc
Copy link

Hey @robinknowles thanks for this great tool!
I have a question if I may,
I did some aliases (also discovered that if you do aliases instead of funciton as you posted it goes way faster! not sure why), in any case...
sometimes my fuzzy searchs are too much fuzzy any ideas how I would make it less fuzzy?
one example:
image
here I am looking for the swarmcorrection and it arrives to make the word from the path of the file lol.
the alias I am using is:
alias ftut="rg --color=always --line-number --no-heading --smart-case '' $FOAM_TUTORIALS | fzf --ansi --color 'hl:-1:underline,hl+:-1:underline:reverse' --delimiter : --preview 'batcat -lcpp --color=always {1} --highlight-line {2}' --preview-window 'up,75%,border,+{2}+3/3,~3' | sed 's/:.*//'"

@robinknowles
Copy link
Author

Hi @otaolafranc - I'm glad you're getting some use out of it.

Unfortunately, when I try your command on my machine I get the expected behaviour i.e. it pulls up a bunch of phaseProperties files in twoPhaseEulerFoam etc 🤔

That's with rg = 14.1.0 , fzf = 0.44.1 & bat = 0.24.0 in an Ubuntu Docker container.

Not sure if it's a version thing or an operating system thing.

fzf has options to tweak how it matches, but they aren't something I've played with.

Hope you can get it working how you need 🤞

@otaolafranc
Copy link

I have the 'good' matches but also when i go up even more on the list i beging getting some matches such as the one showed there...
for the corresponding versions I am rg=14.1.0 fzf=0.57.0 and bat=0.24.0. strange... will have a look at the link, but as it is a pipe of so many things I am a little bit lost lol.
thanks in any case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment