{"id":51133676,"url":"https://github.com/trananhtung/cli-truncate","last_synced_at":"2026-06-25T15:01:46.274Z","repository":{"id":366610692,"uuid":"1276434521","full_name":"trananhtung/cli-truncate","owner":"trananhtung","description":"Truncate strings to a terminal display width with an ellipsis — wide-character (CJK) and ANSI/OSC-escape aware. A Rust take on Node's cli-truncate.","archived":false,"fork":false,"pushed_at":"2026-06-22T14:45:20.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-06-22T16:16:40.018Z","etag":null,"topics":["ansi","cli","rust","terminal","text","truncate","unicode","width"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trananhtung.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-22T01:14:44.000Z","updated_at":"2026-06-22T14:47:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/trananhtung/cli-truncate","commit_stats":null,"previous_names":["trananhtung/cli-truncate"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/trananhtung/cli-truncate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fcli-truncate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fcli-truncate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fcli-truncate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fcli-truncate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trananhtung","download_url":"https://codeload.github.com/trananhtung/cli-truncate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fcli-truncate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34780126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-25T02:00:05.521Z","response_time":101,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansi","cli","rust","terminal","text","truncate","unicode","width"],"created_at":"2026-06-25T15:01:45.416Z","updated_at":"2026-06-25T15:01:46.258Z","avatar_url":"https://github.com/trananhtung.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cli-truncate\n\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)\n\n[![Crates.io](https://img.shields.io/crates/v/cli-truncate.svg)](https://crates.io/crates/cli-truncate)\n[![Documentation](https://docs.rs/cli-truncate/badge.svg)](https://docs.rs/cli-truncate)\n[![CI](https://github.com/trananhtung/cli-truncate/actions/workflows/ci.yml/badge.svg)](https://github.com/trananhtung/cli-truncate/actions/workflows/ci.yml)\n[![License](https://img.shields.io/crates/l/cli-truncate.svg)](#license)\n\n**Truncate strings to a terminal display width** with an ellipsis — correctly\nhandling **wide (CJK) characters** (two columns) and **ANSI escape sequences**\n(zero columns). A Rust take on Node's [`cli-truncate`](https://www.npmjs.com/package/cli-truncate).\n\n```rust\nuse cli_truncate::{truncate, width, Options, Position};\n\nassert_eq!(truncate(\"unicorn\", 4), \"uni…\");\nassert_eq!(truncate(\"古池や蛙\", 6), \"古池…\");                  // wide chars counted as 2\nassert_eq!(width(\"\\u{1b}[31mhi\\u{1b}[0m\"), 2);                // ANSI = zero width\n\n// Position + custom ellipsis\nassert_eq!(Options::new().position(Position::Start).truncate(\"unicorn\", 5), \"…corn\");\nassert_eq!(Options::new().position(Position::Middle).truncate(\"unicorn\", 5), \"un…rn\");\nassert_eq!(Options::new().ellipsis(\"...\").truncate(\"unicorn\", 6), \"uni...\");\n```\n\n## Why cli-truncate?\n\n`unicode-truncate` truncates by display width, but doesn't understand ANSI color\ncodes; the heavyweight `console` crate can, but pulls in a full terminal stack.\n`cli-truncate` is the small, focused piece: truncate styled, wide-character text\nto fit a column budget — for tables, status lines, log viewers, and TUIs.\n\n```toml\n[dependencies]\ncli-truncate = \"0.1\"\n```\n\n## API\n\n| Item | Purpose |\n| --- | --- |\n| `truncate(text, max_width)` | Truncate at the end with a `…` ellipsis |\n| `Options::new().position(..).ellipsis(..).truncate(text, max_width)` | Configurable truncation |\n| `width(text)` | Display width in columns (ANSI-ignored, wide chars = 2) |\n| `Position` | `End` (default), `Start`, `Middle` |\n\n## Behavior\n\n- Output never exceeds `max_width` columns; `max_width == 0` yields `\"\"`. An ellipsis\n  wider than `max_width` is itself clamped, so the bound always holds.\n- Escape sequences are recognized as zero width and never split across the cut:\n  CSI (`ESC [ … m`, including the 8-bit `0x9B` form), string sequences (OSC\n  hyperlinks, DCS/SOS/PM/APC with their `ST`/`BEL` terminators), and two-byte / nF\n  escapes (`ESC c`, `ESC ( B`).\n- A reset (`\\x1b[0m`) is appended only when the kept text leaves a style open, so\n  color can't \"leak\" past the cut and resets are never duplicated.\n- Other C0/C1 control characters (newline, tab, carriage return, NUL, …) have no\n  defined column width and are dropped from truncated output, so they can't break\n  single-line layout.\n- For `Start`/`Middle`, styling that began in the *dropped* region isn't carried\n  over, and a combining mark whose base was dropped is not orphaned onto the ellipsis.\n- Truncation respects character boundaries, not grapheme clusters: a multi-codepoint\n  cluster (a ZWJ emoji or a flag) may be cut between codepoints — the column budget\n  is always honored regardless.\n\n## Contributors ✨\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.\n\nThanks goes to these wonderful people:\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/trananhtung\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/30992229?v=4?s=100\" width=\"100px;\" alt=\"Tung Tran\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eTung Tran\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/trananhtung/cli-truncate/commits?author=trananhtung\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#maintenance-trananhtung\" title=\"Maintenance\"\u003e🚧\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n## License\n\nLicensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at\nyour option.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrananhtung%2Fcli-truncate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrananhtung%2Fcli-truncate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrananhtung%2Fcli-truncate/lists"}