Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag specific preview and cycling #79

Closed
adelin-b opened this issue May 2, 2020 · 17 comments · Fixed by #126
Closed

Tag specific preview and cycling #79

adelin-b opened this issue May 2, 2020 · 17 comments · Fixed by #126
Labels
enhancement New feature or request

Comments

@adelin-b
Copy link

adelin-b commented May 2, 2020

Is your feature request related to a problem? Please describe.
Im trying to define different preview specific to the type/tag of the completion, for example it can be either a command / folder / textfile / image / aliases
vim
Describe the solution you'd like
Actually Im using

zstyle ':fzf-tab:complete:*' extra-opts --preview=$extract'

  # Display Information about the command
  which ${~ctxt[hpre]}$in | grep -v "not found" 2> /dev/null;
  whatis ${~ctxt[hpre]}$in 2> /dev/null;

  # Search through man and give the description segment
 man "${~ctxt[hpre]}$in" 2> /dev/null |col -bx|awk -v S="$2" "DESCRIPTION ~ S {cap="true"; print} DESCRIPTION !~ S && /^[A-Z ]+$/ {cap="false"} DESCRIPTION !~ S && !/^[A-Z ]+$/ {if(cap == "true")print}" 2> /dev/null;

  # Display images
  tiv -h ${FZF_PREVIEW_LINES} -w ${FZF_PREVIEW_COLUMNS} ${~ctxt[hpre]}$in  2> /dev/null;

  # Display exif data
  # exiftool ${~ctxt[hpre]}$in  2> /dev/null;
  
  # Display directory
  lsd --tree --depth 2 --color=always ${~ctxt[hpre]}$in 2> /dev/null
  # exa -1 --color=always ${~ctxt[hpre]}$in 2> /dev/null;
  # du -h ${~ctxt[hpre]}$in 2> /dev/null | tail -n1

  # Display file
  bat --theme="OneHalfDark" --style=numbers,changes --color always ${~ctxt[hpre]}$in 2> /dev/null | head -n50 | grep -v "bat warning";
  '

I would like to be able to handle all kind of completion types like options / commits / users / services / diff / urls.
So the extra ops should have a way to know which kind of tag is previewed.

Hence thoses questions that are in the way:

  1. How to make preview that are aware of the current tag to display?
  2. How to make functions available inside zstyle? (I tryed to define functions just above be I couldnt use theme)
  3. How to make aliases available inside zstyle? (which g work inside a shell but not when im trying to show it during the completion preview)
  4. How would you implement a way to switch the preview mode (for example the type imageFile would have 2 preview mode: exif and terminal preview)

Here is what the code above do, im using tiv for the image display and bat for the code
fzf-tab

git diff

@adelin-b adelin-b added the enhancement New feature or request label May 2, 2020
@Aloxaf
Copy link
Owner

Aloxaf commented May 4, 2020

  1. You can use different context:
# for command
zstyle ':fzf-tab:complete:-command-:*' extra-opts --preview=$extract'
  which ${~ctxt[hpre]}$in | grep -v "not found" 2> /dev/null;
  whatis ${~ctxt[hpre]}$in 2> /dev/null;
  man "${~ctxt[hpre]}$in" 2> /dev/null |col -bx|awk -v S="$2" "DESCRIPTION ~ S {cap="true"; print} DESCRIPTION !~ S && /^[A-Z ]+$/ {cap="false"} DESCRIPTION !~ S && !/^[A-Z ]+$/ {if(cap == "true")print}" 2> /dev/null;
'
# for options
zstyle ':fzf-tab:complete:*:options' extra-opts --preview=$extract'echo $in'
  1. (&3) It has nothing to do with zstyle. --preview will spawn a new zsh process with zsh -c PREVIEW_CODE, which won't load the zshrc and know nothing about your current zsh session.
    Here are two solutions:
  • Source your zshrc manually. This is a simple solution but you might need to modify your zshrc to ensure that unnecessary code won't be sourced.
  • use zstyle -e. It will eval its content as zsh code and get the final value from the reply variable.
  1. I don't understand... Can you give me a example?

@justinlovinger
Copy link

@Aloxaf Can context target files, for any command? I would like --preview when completing files, but not options, processes, etc.

In general, are the available fzf-tab zstyle contexts documented anywhere? The readme implies you can use c-x h to find contexts, but that doesn't work for fzf-tab in my experience. I can't use c-x h during fzf, and the :completion: contexts don't seem related to :fzf-tab: contexts.

@Aloxaf
Copy link
Owner

Aloxaf commented May 17, 2020

@justinlovinger

Can context target files, for any command?

For most command, you can use fzf-tab:complete:*:argument-rest, except some special commands like file. You should use :fzf-tab:complete:file:* for file command.

In general, are the available fzf-tab zstyle contexts documented anywhere?

Unfortunately, there aren't. There are command-specific, though most command will repect to a regular format. You may find some useful information here.

the :completion: contexts don't seem related to :fzf-tab: contexts

Just replace :completion: to :fzf-tab:.

@adelin-b
Copy link
Author

So this is what im doing at the moment but I'm still unsatisfied

fzf_tab_preview_commit='
# Show commits and branch commits 
git show $( echo "$in" | cut -f 1 -d " " ) | diff-so-fancy --colors
'

fzf_tab_preview_options=''

fzf_tab_preview_command='
# Show information on command
which ${~ctxt[hpre]}$in | grep -v "not found" 2> /dev/null;
whatis ${~ctxt[hpre]}$in 2> /dev/null;

# Show my own snippets related to the command
grep -w "$in" ~/.config/clisnippets 2> /dev/null;

# tldr show cheatsheet of command
tldr $in 2> /dev/null;

# Search through man and give the description segment
man "${~ctxt[hpre]}$in" 2> /dev/null |col -bx|awk -v S="$2" "DESCRIPTION ~ S {cap="true"; print} DESCRIPTION !~ S && /^[A-Z ]+$/ {cap="false"} DESCRIPTION !~ S && !/^[A-Z ]+$/ {if(cap == "true")print}" 2> /dev/null;
'

fzf_tab_preview_file='
# Display images
tiv -h ${FZF_PREVIEW_LINES} -w ${FZF_PREVIEW_COLUMNS} $in  2> /dev/null;

# Display directory
# du -h ${~ctxt[hpre]}$in 2> /dev/null | tail -n1
lsd --tree --icon --depth 2  --color=always $in 2> /dev/null
# exa -1 --color=always $in 2> /dev/null;

# Display file
bat --theme="OneHalfDark" --style=numbers,changes --color always $in 2> /dev/null | head -n50 | grep -v "bat warning"; 2> /dev/null;

# Display exif data
exiftool $in  2> /dev/null;
'

fzf_tab_preview_systemctl_service='
systemctl status $in
systemctl help $in
'

  zstyle ':fzf-tab:complete:*' extra-opts --preview=$extract$fzf_tab_preview_file
  zstyle ':fzf-tab:complete:*:options' extra-opts '' # --preview=$extract$fzf_tab_preview_options
  zstyle ':fzf-tab:complete:-command-:*' extra-opts --preview=$extract$fzf_tab_preview_command
  zstyle ':fzf-tab:complete:git*:*' extra-opts --preview=$extract$fzf_tab_preview_commit$fzf_tab_preview_file
  zstyle ':fzf-tab:complete:systemctl*:*' extra-opts --preview=$extract$fzf_tab_preview_systemctl_service

However this is not really the way I want to achieve it, I don't really get how to interpret the result of C-x h

tags in context :completion::complete:git::
    argument-rest  (_arguments _git _git)
tags in context :completion::complete:git-checkout::
    argument-rest options  (_arguments _git-checkout _git _git)
tags in context :completion::complete:git-checkout:argument-rest:
    modified-files tree-ishs remote-branch-names-noprefix  (_git-checkout _git _git) 
    modified-files                                         (__git_files __git_modified_files _git-checkout _git _git) 
    recent-branches commits                                (__git_commits_prefer_recent _git-checkout _git _git) 
    recent-branches                                        (__git_recent_branches __git_commits_prefer_recent _git-checkout _git _git) 

I would like to be able to target each tag differently for example

  zstyle ':fzf-tab:complete:git*:commits' extra-opts --preview=$extract' echo Commits\n; '$fzf_tab_preview_commit$fzf_tab_preview_file
  zstyle ':fzf-tab:complete:git*:modified-files' extra-opts --preview=$extract' echo Modified files \n; '$fzf_tab_preview_commit$fzf_tab_preview_file

or even better something like

  zstyle ':fzf-tab:complete:*:*' extra-opts --preview=$extract' tag=$extractedTagName 
if [[ tag == "file" ]]; then
  
fi
if [[ tag == "commits" ]]; then
  
fi
if [[ tag == "recent branches" ]]; then
  
fi
  '

@adelin-b adelin-b reopened this Jun 29, 2020
@Aloxaf
Copy link
Owner

Aloxaf commented Jul 2, 2020

Ummm, there may need a way to extract the group name.

@adelin-b
Copy link
Author

adelin-b commented Jul 2, 2020

Indeed with extracted group name and tag and context string we could really leverage a lot of interesting preview easily

@magnetophon
Copy link

@adelin-b That gif looks fantastic, thanks for posting it, and the code that goes with it!
I pasted your code into my zshrc, but I'm getting an incorrect preview.
I don't expect you to troubleshoot this for me, therefore I haven't provided more details, but could you just tell me:

  • do I need to (not) do anything else?
  • does the placement/order of where I paste your code matter?

@adelin-b
Copy link
Author

adelin-b commented Oct 14, 2020

Here is an updated version of this code snippet as they were breaking changes since.
https://gist.github.com/adelin-b/53fecd0f9010819bae2e69792da29704

  • You need to install the dependencies of cli used in the code snippets
  • It only matter of where compinit is I think but im not sure, it should be before
  • back up you system in case in a completion something pop like ;rm ~/* -rf or worst a ; ssh .... that would allow your machine to be used from remote

@magnetophon
Copy link

@adelin-b Thanks a lot!
Unfortunately, that link gave me: "We couldn’t find any files to show."

@adelin-b
Copy link
Author

It should be working now, mind that there will be a revision of this gist to escape the completion string to prevent injection @magnetophon

@magnetophon
Copy link

@adelin-b WOW! Thank you very much!
That works perfectly!

@Aloxaf Any chance to make this a part of fzf-tab?

@adelin-b
Copy link
Author

It is actually really usefull but not much people ive shown this like it as it look clutered
In my experience it saved me countless keystroke

  • by not having to ls <directory> before copying / accessing it / removing it
  • by not having to preview images
  • showing git diff automatically on the file I want to add or branch to checkout / rebase onto

It replace packages like https://github.com/wfxr/forgit https://github.com/chriswalz/bit and all of thoses

@magnetophon
Copy link

It is actually really usefull but not much people ive shown this like it as it look clutered

I imagine that if @Aloxaf ads this to fzf-tab, it will be an option.

@Aloxaf
Copy link
Owner

Aloxaf commented Oct 16, 2020

@Aloxaf Any chance to make this a part of fzf-tab?

Sorry, the answer is no. Though this configuration is cool, it goes beyond the purpose of this repo.
Just like p10k is cool but it still hasn't been the default theme of zsh.

@magnetophon
Copy link

@Aloxaf Ok, fair enough.

@adelin-b
Copy link
Author

@Aloxaf it also goes beyond the scope of this issues which is getting the $tag name of the ongoing preview which is still needed for cleaner previews

@Aloxaf
Copy link
Owner

Aloxaf commented Oct 17, 2020

@adelin-b Please take a look at #126.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants