Skip to content

Instantly share code, notes, and snippets.

@fdncred
Last active September 24, 2022 07:37
Show Gist options
  • Save fdncred/c55a7a83f41bb6b3aa48381dd44ed4f8 to your computer and use it in GitHub Desktop.
Save fdncred/c55a7a83f41bb6b3aa48381dd44ed4f8 to your computer and use it in GitHub Desktop.
fzf_integration

FZF

Most of this is taken from fzf.fish. A really nice fzf integration with fish-shell.

There's quite a bit of usage of a fish command called commandline. It would be super helpful for porting things like this script if we had such a utility to modify the readline command line buffer.

General Setup

Set the default fzf options

let-env FZF_DEFAULT_OPTS = '--cycle --layout=reverse --border --height=90% --preview-window=wrap --marker="*"'

Search git log

Set the Log Format String

let log_fmt_str = '%C(bold blue)%h%C(reset) - %C(cyan)%ad%C(reset) %C(yellow)%d%C(reset) %C(normal)%s%C(reset)  %C(dim normal)[%an]%C(reset)'

Set the selected log lines and invote fzf

let selected_log_lines = (
git log [--format=format:($log_fmt_str) --date=short --color=always] |
fzf [--ansi --multi --tiebreak=index '--preview=git show --color=always --stat --patch {1}']
)

Here's an example of what $selected_log_lines looks like after an fzf selection

echo $selected_log_lines
1086fbe9 - Sun Jul 31 12:36:14 2022 -0700  Revert `query` command to `query db` (#6200)  [Reilly Wood]

Get full git hash

let abbreviated_commit_hash = ($selected_log_lines | split row ' ' | first)
let full_commit_hash = (git rev-parse $abbreviated_commit_hash | str trim)

Search directory

Set fd options

let fd_ops = [--color=always --strip-cwd-prefix]

Set fzf arguments

let fzf_arguments = [--multi --ansi]

Get the selected file

let file_paths_selected = (
    fd $fd_ops | fzf $fzf_arguments
)

Example of selection:

$file_paths_selected
crates/nu-command/src/filesystem/ls.rs

Search sqlite history

let command_with_ts = (history | each {|h| $"($h | get start_timestamp | into datetime | date format '%m-%d %H:%M:%S') ($h | get command)(char -i 0)"} | to text | fzf [--read0 --tiebreak=index "--preview=^echo -- {4..}" "--preview-window=bottom:3:wrap"])

maybe use $command_with_ts | str trim | str substring '15,' instead of echo -- {4..}

@amtoine
Copy link

amtoine commented Sep 23, 2022

that is really cool stuff 😮 🤩

everything is working fine

apart from just the last one gives me

DidYouMean("command", Span { start: 44215, end: 44230 })

in every line and

fork/exec /bin/bash: argument list too long

in the preview window of fzf 👍

@fdncred
Copy link
Author

fdncred commented Sep 23, 2022

@amtoine I think I just fixed the last one. Same fix, moving the quotes. Although it's not super useful because it's storing things in $command_with_ts.

@amtoine
Copy link

amtoine commented Sep 24, 2022

cooool thanks 🤩

@amtoine
Copy link

amtoine commented Sep 24, 2022

i've got a question 😋

you use

git log [--format=format:($log_fmt_str) --date=short --color=always]

instead of

git log $"--format=format:($log_fmt_str)" --date=short --color=always

, is that a way to force string expansion without the use of $" " in nushell and with argument arrays? 😮

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