petereon / beaupy

A Python library of interactive CLI elements you have been looking for
https://petereon.github.io/beaupy/
MIT License
181 stars 13 forks source link

feat: completion callable #61

Closed petereon closed 9 months ago

petereon commented 1 year ago

Changes:

Resolves #49

sonarcloud[bot] commented 1 year ago

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

25.9% 25.9% Coverage
0.0% 0.0% Duplication

petereon commented 1 year ago

@juxeii, I have implemented a very simple POC for customizable completion. prompt now has a completion argument, which takes a Callable[[str], List[str]] that takes current content of prompt as a str argument and should return a List[str] that will serve as completion.

It reacts to tab key and behaves much like shell completion as far as I have tested.

I've designed a very naive file-system completion:

from os import listdir
from pathlib import Path

def file_completion(str_path: str = ""):
    if not str_path:
        return []
    try:
        path = Path(str_path)
        rest = ''
        if not path.exists():
            str_path, rest = str_path.rsplit('/', 1)
            path = Path(str_path or '/')

        filtered_list_dir = [i for i in listdir(path) if i.startswith(rest)]

        if not path.is_absolute():
            return ['./'+str(Path(path)/i) for i in filtered_list_dir]
        else:
            return [str(Path(path)/i) for i in filtered_list_dir]
    except Exception as e:
        return []

This can be passed to prompt as such:

from beaupy import prompt

prompt("Enter a path:", completion=file_completion)

Is this the sort of functionality you envisoned in #49?

petereon commented 1 year ago

I should mention it is currently only on this branch, you should be able to install using:

poetry add git+https://github.com/petereon/beaupy.git@prompt_completion
juxeii commented 1 year ago

Hi,

that looks great. Thx for your effort.

codecov-commenter commented 9 months ago

Codecov Report

Merging #61 (278863c) into master (4a62069) will decrease coverage by 1.06%. The diff coverage is 83.33%.

@@            Coverage Diff             @@
##           master      #61      +/-   ##
==========================================
- Coverage   99.43%   98.37%   -1.06%     
==========================================
  Files           3        3              
  Lines         351      370      +19     
  Branches       92       99       +7     
==========================================
+ Hits          349      364      +15     
- Misses          0        1       +1     
- Partials        2        5       +3     
Flag Coverage Δ
unittests 98.37% <83.33%> (-1.06%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
beaupy/_beaupy.py 97.72% <78.94%> (-1.47%) :arrow_down:
beaupy/_internals.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

sonarcloud[bot] commented 9 months ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

88.9% 88.9% Coverage
0.0% 0.0% Duplication