{"id":17472532,"url":"https://github.com/shinnn/tty-truncate","last_synced_at":"2025-04-09T23:00:19.825Z","repository":{"id":57382061,"uuid":"95769478","full_name":"shinnn/tty-truncate","owner":"shinnn","description":"Truncate a string to the current text terminal width","archived":false,"fork":false,"pushed_at":"2019-07-11T08:29:40.000Z","size":111,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-18T19:56:44.400Z","etag":null,"topics":["javascript","log","nodejs","string-manipulation","terminal","truncate","tty","width"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shinnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-29T11:08:15.000Z","updated_at":"2024-07-01T19:25:20.000Z","dependencies_parsed_at":"2022-09-13T11:31:23.434Z","dependency_job_id":null,"html_url":"https://github.com/shinnn/tty-truncate","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ftty-truncate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ftty-truncate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ftty-truncate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Ftty-truncate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shinnn","download_url":"https://codeload.github.com/shinnn/tty-truncate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125581,"owners_count":21051768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["javascript","log","nodejs","string-manipulation","terminal","truncate","tty","width"],"created_at":"2024-10-18T17:26:04.693Z","updated_at":"2025-04-09T23:00:19.708Z","avatar_url":"https://github.com/shinnn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tty-truncate\n\n[![npm version](https://img.shields.io/npm/v/tty-truncate.svg)](https://www.npmjs.com/package/tty-truncate)\n[![GitHub Actions](https://action-badges.now.sh/shinnn/tty-truncate)](https://wdp9fww0r9.execute-api.us-west-2.amazonaws.com/production/results/shinnn/tty-truncate)\n[![Coverage Status](https://img.shields.io/coveralls/shinnn/tty-truncate.svg)](https://coveralls.io/github/shinnn/tty-truncate?branch=master)\n\nTruncate a `string` to the current text terminal width, considering its [visual width](https://eev.ee/blog/2015/09/12/dark-corners-of-unicode/#combining-characters-and-character-width)\n\n```javascript\nconst ttyTruncate = require('tty-truncate');\n\nconst string = '4724e053261747b278049de678b1ed';\n\nprocess.stdout.columns; //=\u003e 30\nttyTruncate(string); //=\u003e '4724e053261747b278049de678b1ed'\n\nprocess.stdout.columns; //=\u003e 20\nttyTruncate(string); //=\u003e '4724e053261747b2780…'\n```\n\nThough the first impression of this module would be “`string.slice(0, process.stdout.columns)` suffices.”, it doesn't always work well because lots of non-ASCII characters occupy 2 columns in a terminal.\n\n```javascript\nprocess.stdout.columns; //=\u003e 80\n\nconst original = '字'.repeat(100);\n//=\u003e '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字'\nconst sliced = original.slice(0, process.stdout.columns);\n//=\u003e '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字'\n\noriginal.length; //=\u003e 100\nsliced.length; //=\u003e 80\n\n// The output will overflow the current row,\n// because the width of 字 in a text terminal is not 1 column but 2 columns.\nconsole.log(sliced);\n```\n\ntty-truncate handles this case.\n\n```javascript\nttyTruncate(original);\n//=\u003e '字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字字…'\n```\n\n## Installation\n\n[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).\n\n```\nnpm install tty-truncate\n```\n\n## API\n\n```javascript\nconst ttyTruncate = require('tty-truncate');\n```\n\n### ttyTruncate(*input*)\n\n*input*: `string` with no `\\n`  \nReturn: `string`\n\nIt replaces overflowing text with a single `…`.\n\n```javascript\nprocess.stdout.columns; //=\u003e 20\n\nttyTruncate('Halfwidth characters');\n//=\u003e 'Halfwidth characters'\n\nttyTruncate('Ｆｕｌｌｗｉｄｔｈ　ｃｈａｒａｃｔｅｒｓ');\n//=\u003e 'Ｆｕｌｌｗｉｄｔｈ…'\n```\n\nThis works only when [`process.stdout.isTTY`](https://nodejs.org/api/tty.html#tty_writestream_istty) is `true`. In a non-TTY environment it throws an `Error` instead.\n\n## License\n\n[ISC License](./LICENSE) © 2018 - 2019 Watanabe Shinnosuke\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Ftty-truncate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshinnn%2Ftty-truncate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Ftty-truncate/lists"}