Skip to content

utils

Class info

🛈 DocStrings

Common completion providers.

format_size

format_size(size: int) -> str

Format file size in human readable format.

Source code in src/slashed/utils.py
def format_size(size: int) -> str:
    """Format file size in human readable format."""
    for unit in ["B", "KB", "MB", "GB"]:
        if size < 1024:  # noqa: PLR2004
            return f"{size:.1f} {unit}"
        size /= 1024  # type: ignore
    return f"{size:.1f} TB"

get_file_kind

get_file_kind(path: Path | UPath) -> CompletionKind

Get more specific file kind based on extension.

Source code in src/slashed/utils.py
def get_file_kind(path: Path | upath.UPath) -> CompletionKind:
    """Get more specific file kind based on extension."""
    ext = path.suffix.lower()
    kinds: dict[str, CompletionKind] = {
        ".py": "python",
        ".yml": "yaml",
        ".yaml": "yaml",
        ".json": "json",
        ".md": "markdown",
        ".txt": "text",
    }
    return kinds.get(ext, "file")

get_first_line

get_first_line(text: str | None) -> str | None

Get first line of text, stripped of whitespace.

Parameters:

Name Type Description Default
text str | None

Input text or None

required

Returns:

Type Description
str | None

First line without whitespace if text is provided, None otherwise

Source code in src/slashed/utils.py
def get_first_line(text: str | None) -> str | None:
    """Get first line of text, stripped of whitespace.

    Args:
        text: Input text or None

    Returns:
        First line without whitespace if text is provided, None otherwise
    """
    if not text:
        return None
    return text.split("\n")[0].strip()

get_metadata

get_metadata(path: Path | UPath) -> str

Get metadata for path entry.

Source code in src/slashed/utils.py
def get_metadata(path: Path | upath.UPath) -> str:
    """Get metadata for path entry."""
    try:
        if path.is_dir():
            return f"Directory ({len(list(path.iterdir()))} items)"
        size = format_size(path.stat().st_size)
        return f"{path.suffix[1:].upper()} file, {size}"
    except Exception:  # noqa: BLE001
        return ""