Metadata-Version: 2.4
Name: philiprehberger-string-truncate
Version: 0.2.0
Summary: Truncate strings intelligently without cutting words
Project-URL: Homepage, https://github.com/philiprehberger/py-string-truncate#readme
Project-URL: Repository, https://github.com/philiprehberger/py-string-truncate
Project-URL: Issues, https://github.com/philiprehberger/py-string-truncate/issues
Project-URL: Changelog, https://github.com/philiprehberger/py-string-truncate/blob/main/CHANGELOG.md
Author: Philip Rehberger
License-Expression: MIT
License-File: LICENSE
Keywords: ellipsis,shorten,string,text,truncate
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# philiprehberger-string-truncate

[![Tests](https://github.com/philiprehberger/py-string-truncate/actions/workflows/publish.yml/badge.svg)](https://github.com/philiprehberger/py-string-truncate/actions/workflows/publish.yml)
[![PyPI version](https://img.shields.io/pypi/v/philiprehberger-string-truncate.svg)](https://pypi.org/project/philiprehberger-string-truncate/)
[![Last updated](https://img.shields.io/github/last-commit/philiprehberger/py-string-truncate)](https://github.com/philiprehberger/py-string-truncate/commits/main)

Truncate strings intelligently without cutting words.

## Installation

```bash
pip install philiprehberger-string-truncate
```

## Usage

```python
from philiprehberger_string_truncate import truncate, truncate_middle, truncate_path

truncate("Hello beautiful world", max_length=15)
# "Hello…"

truncate("Hello beautiful world", max_length=15, suffix="...")
# "Hello..."

truncate_middle("Hello beautiful world of code", max_length=20)
# "Hello bea…l of code"

truncate_path("/very/long/path/to/some/file.txt", max_length=25)
# "/very/long/.../file.txt"
```

### Word-count truncation

```python
from philiprehberger_string_truncate import truncate_words

truncate_words("one two three four five", max_words=3)
# "one two three..."

truncate_words("one two", max_words=5)
# "one two"
```

### Wrapping

```python
from philiprehberger_string_truncate import wrap

wrap("the quick brown fox jumps over the lazy dog", width=20)
# "the quick brown fox\njumps over the lazy\ndog"

wrap("first paragraph\n\nsecond paragraph", width=80)
# "first paragraph\n\nsecond paragraph"
```

## API

| Function / Class | Description |
|------------------|-------------|
| `truncate(text, max_length, suffix="…", break_words=False)` | Word-boundary truncation |
| `truncate_middle(text, max_length, separator="…")` | Keep start and end |
| `truncate_path(path, max_length, separator="/", placeholder="...")` | Path-aware truncation |
| `truncate_words(text, max_words, suffix="...")` | Truncate to a word count, appending suffix when shortened |
| `wrap(text, width=80)` | Wrap to lines at word boundaries; preserves paragraph breaks |

## Development

```bash
pip install -e .
python -m pytest tests/ -v
```

## Support

If you find this project useful:

⭐ [Star the repo](https://github.com/philiprehberger/py-string-truncate)

🐛 [Report issues](https://github.com/philiprehberger/py-string-truncate/issues?q=is%3Aissue+is%3Aopen+label%3Abug)

💡 [Suggest features](https://github.com/philiprehberger/py-string-truncate/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)

🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)

💻 [GitHub Profile](https://github.com/philiprehberger)

🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)

## License

[MIT](LICENSE)
