question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to use prompt-toolkit to implement these modern CLI tools ??

See original GitHub issue

Prompt-toolkit supports prompt ui and fullscreen ui. But there is another partial fullscreen ui, I don’t know how to implement it with prompt-toolkit.

FZF:

When I input:

cd `find . -type d | fzf --height 30% --reverse`

fzf will use stderr to render it’s ui and write the result to stdout:

fzf1

As you see, with --height 30% parameter, fzf will not occupy the fullscreen and will resue only a region of current screen to display it’s ui widget. When I press enter and accept a path. fzf will clear the region it used, and restore the cursor position for me:

fzf2

Then cd command get the output from fzf and take it as an argument.

Deer

Deer is a ranger like zsh plugin, it can be used as a file picker:

When I input cd <alt+k>, deer will display it’s widgets under current line:

deer1

After picking a path, deer will close (clear it’s ui) and restore cursor position and paste it’s result:

deer2

Question

Both fzf and deer are widely used command line tools today, they are not traditional prompt program and still not traditional fullscreen application.

I really want to know is it possible to use prompt_toolkit to implement sth like fzf or deer ??

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
skywind3000commented, Feb 16, 2019

I want to implement a new fzf, I have a better algorithm, can I use prompt_toolkit for TUI ?? Any example ??

2reactions
kylepollinacommented, Jul 2, 2021

I use IPython and have figured out a simple way to implement this (EDIT: it seems that you want this to be built into prompt toolkit instead of using an external program? I’m not sure this will help with that, but maybe can offer some building blocks). I bet this can be used with purely prompt toolkit since IPython is built using prompt toolkit. Here is the code:

# .config/ipython/profile_default/startup/fzf.py

from subprocess import check_output

from IPython import get_ipython
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.filters import HasFocus, ViInsertMode

ip = get_ipython()


def inline_fzf(event):
    try:
        file = check_output("fzf -m --reverse --height 40% --preview 'bat --style=numbers --color=always --line-range :500 {}'", shell=True, text=True).strip()
        event.cli.current_buffer.text += file
        event.cli.current_buffer.cursor_position = len(event.cli.current_buffer.text)
    except Exception:
        pass
    event.cli.reset()


if hasattr(ip, 'pt_app'):
    registry = ip.pt_app.key_bindings

    # Inline FZF with ctrl+space
    registry.add_binding(
        u'c-@',
        filter=(HasFocus(DEFAULT_BUFFER) & ViInsertMode())
    )(inline_fzf)

And some screenshots: Screen Shot 2021-07-02 at 11 02 22 AM

and after hitting enter it pops back into the current line:

Screen Shot 2021-07-02 at 11 02 24 AM

The ip.pt_app object is a <class 'prompt_toolkit.shortcuts.prompt.PromptSession'> object, so I’m assuming if you have access to that you can implement this in any prompt toolkit app

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reference — prompt_toolkit 3.0.36 documentation
Formatted text. Many places in prompt_toolkit can take either plain text, or formatted text. For instance the prompt() function takes either plain text...
Read more >
prompttoolkit Documentation
prompt_toolkit is a library for building powerful interactive command line and terminal applications in Python. It can be a very advanced pure ...
Read more >
Python Prompt Toolkit 3.0 — prompt_toolkit 3.0.36 ...
prompt_toolkit is a library for building powerful interactive command line and terminal applications in Python. It can be a very advanced pure Python ......
Read more >
Reference — prompt_toolkit 3.0.16 documentation
For a prompt, this is often EOFError or KeyboardInterrupt . style – Apply this ... a tool to work around spelling mistakes, like...
Read more >
Building full screen applications - Python Prompt Toolkit 3.0
prompt_toolkit can be used to create complex full screen terminal applications. Typically, an application consists of a layout (to describe the graphical part) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found