{"id":51133662,"url":"https://github.com/trananhtung/slice-ansi","last_synced_at":"2026-06-25T15:01:45.178Z","repository":{"id":366615780,"uuid":"1276461698","full_name":"trananhtung/slice-ansi","owner":"trananhtung","description":"Slice a string by terminal display column, preserving ANSI styling and wide (CJK) characters. A Rust take on Node's slice-ansi.","archived":false,"fork":false,"pushed_at":"2026-06-22T14:47:30.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-06-22T16:26:43.531Z","etag":null,"topics":["ansi","no-std","rust","slice","substring","terminal","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-22T02:12:14.000Z","updated_at":"2026-06-22T14:48:01.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/trananhtung/slice-ansi","commit_stats":null,"previous_names":["trananhtung/slice-ansi"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/trananhtung/slice-ansi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fslice-ansi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fslice-ansi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fslice-ansi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fslice-ansi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trananhtung","download_url":"https://codeload.github.com/trananhtung/slice-ansi/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trananhtung%2Fslice-ansi/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","no-std","rust","slice","substring","terminal","unicode","width"],"created_at":"2026-06-25T15:01:44.258Z","updated_at":"2026-06-25T15:01:45.168Z","avatar_url":"https://github.com/trananhtung.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# slice-ansi\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/slice-ansi.svg)](https://crates.io/crates/slice-ansi)\n[![Documentation](https://docs.rs/slice-ansi/badge.svg)](https://docs.rs/slice-ansi)\n[![CI](https://github.com/trananhtung/slice-ansi/actions/workflows/ci.yml/badge.svg)](https://github.com/trananhtung/slice-ansi/actions/workflows/ci.yml)\n[![License](https://img.shields.io/crates/l/slice-ansi.svg)](#license)\n\n**Slice a string by terminal display column** — correctly handling **wide (CJK)\ncharacters** (two columns) and **ANSI escape sequences** (zero columns). Styling\nactive at the cut is re-applied to the slice and reset at the end, so the result\nrenders exactly like that part of the original. A Rust take on Node's\n[`slice-ansi`](https://www.npmjs.com/package/slice-ansi).\n\n```rust\nuse slice_ansi::{slice, slice_from, width};\n\nassert_eq!(slice(\"hello\", 1, 3), \"el\");\nassert_eq!(slice(\"古池や蛙\", 0, 4), \"古池\");                       // wide chars\nassert_eq!(slice(\"\\u{1b}[31mhello\\u{1b}[0m\", 1, 3), \"\\u{1b}[31mel\\u{1b}[0m\"); // style preserved\nassert_eq!(width(\"\\u{1b}[31mhi\\u{1b}[0m\"), 2);                     // ANSI = zero width\n```\n\n## Why slice-ansi?\n\n`ansi-width` measures a styled string's width, but there's no crate to *extract* a\ncolumn range from one. Naively slicing bytes splits escape sequences and wide\ncharacters and drops the active color. `slice-ansi` is the focused piece — pull a\nwindow out of a styled, wide-character line for tables, columns, scrolling viewports,\nand TUIs. It pairs with [`cli-truncate`](https://crates.io/crates/cli-truncate)\n(which keeps a width-bounded prefix).\n\n```toml\n[dependencies]\nslice-ansi = \"0.1\"\n```\n\n## API\n\n| Item | Purpose |\n| --- | --- |\n| `slice(text, start, end)` | Substring covering display columns `[start, end)` |\n| `slice_from(text, start)` | Slice from `start` to the end of the string |\n| `width(text)` | Display width in columns (ANSI-ignored, wide chars = 2) |\n\n## Behavior\n\n- Columns are half-open `[start, end)`; `end` is clamped to the string width and\n  `start \u003e= end` yields `\"\"`. Output width never exceeds `end - start`.\n- A wide character that straddles either boundary is excluded, so the result is\n  never a broken half-glyph.\n- SGR styling active at `start` is re-emitted at the front of the slice, and a reset\n  (`\\x1b[0m`) is appended when styling is still open at the end — color can't leak,\n  and a reset already inside the slice isn't duplicated. An OSC 8 hyperlink open at\n  the cut is likewise re-opened and closed, so links stay balanced.\n- Escape sequences (CSI, OSC, DCS/etc., two-byte/nF escapes, 8-bit C1) are\n  recognized as zero width and never split. Combining marks travel with their base\n  character.\n- Limitations: attribute-off SGR codes (`39`/`49`, `22`–`29`) aren't collapsed, so a\n  slice may carry a redundant (but correctly rendering) style; and a cut *inside* a\n  multi-codepoint grapheme cluster (a ZWJ emoji) may split it.\n\n## `no_std`\n\n`#![no_std]` (needs only `alloc`); the single dependency is `unicode-width`.\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/slice-ansi/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%2Fslice-ansi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrananhtung%2Fslice-ansi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrananhtung%2Fslice-ansi/lists"}