{"id":13879672,"url":"https://github.com/piotrmurach/strings-truncation","last_synced_at":"2025-06-12T13:09:24.532Z","repository":{"id":59156667,"uuid":"226529638","full_name":"piotrmurach/strings-truncation","owner":"piotrmurach","description":"Truncate strings with fullwidth characters and ANSI codes.","archived":false,"fork":false,"pushed_at":"2024-03-12T23:17:32.000Z","size":61,"stargazers_count":48,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-10T17:56:56.587Z","etag":null,"topics":["ansi","elide","elipsis","elision","omission","ruby","rubygem","shorten","strings","strings-manipulation","text","truncate","truncate-strings","truncation"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotrmurach.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":"piotrmurach"}},"created_at":"2019-12-07T14:47:49.000Z","updated_at":"2025-05-13T04:16:10.000Z","dependencies_parsed_at":"2024-01-13T20:57:38.470Z","dependency_job_id":"564e99d3-addd-4fed-ba60-da8a36ded473","html_url":"https://github.com/piotrmurach/strings-truncation","commit_stats":{"total_commits":57,"total_committers":1,"mean_commits":57.0,"dds":0.0,"last_synced_commit":"beda439fa85fe8715c31a73d8527f25ee80efa1c"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/piotrmurach/strings-truncation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Fstrings-truncation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Fstrings-truncation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Fstrings-truncation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Fstrings-truncation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrmurach","download_url":"https://codeload.github.com/piotrmurach/strings-truncation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Fstrings-truncation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259470951,"owners_count":22862999,"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":["ansi","elide","elipsis","elision","omission","ruby","rubygem","shorten","strings","strings-manipulation","text","truncate","truncate-strings","truncation"],"created_at":"2024-08-06T08:02:28.452Z","updated_at":"2025-06-12T13:09:24.508Z","avatar_url":"https://github.com/piotrmurach.png","language":"Ruby","funding_links":["https://github.com/sponsors/piotrmurach"],"categories":["Ruby"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"225\" src=\"https://github.com/piotrmurach/strings/blob/master/assets/strings_logo.png\" alt=\"strings logo\" /\u003e\n\u003c/div\u003e\n\n# Strings::Truncation\n\n[![Gem Version](https://badge.fury.io/rb/strings-truncation.svg)][gem]\n[![Actions CI](https://github.com/piotrmurach/strings-truncation/actions/workflows/ci.yml/badge.svg)][gh_actions_ci]\n[![Build status](https://ci.appveyor.com/api/projects/status/s8y94c4tvi8mgrh2?svg=true)][appveyor]\n[![Maintainability](https://api.codeclimate.com/v1/badges/f7ecb5bf87696e522ccb/maintainability)][codeclimate]\n[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/strings-truncation/badge.svg?branch=master)][coverage]\n\n[gem]: http://badge.fury.io/rb/strings-truncation\n[gh_actions_ci]: https://github.com/piotrmurach/strings-truncation/actions/workflows/ci.yml\n[appveyor]: https://ci.appveyor.com/project/piotrmurach/strings-truncation\n[codeclimate]: https://codeclimate.com/github/piotrmurach/strings-truncation/maintainability\n[coverage]: https://coveralls.io/github/piotrmurach/strings-truncation?branch=master\n\n\u003e Truncate strings with fullwidth characters and ANSI codes.\n\n## Features\n\n* No monkey-patching String class\n* Omit text from the start, middle, end or both ends\n* Account for fullwidth characters in encodings such as UTF-8, EUC-JP\n* Shorten text without whitespaces between words (Chinese, Japanese, Korean etc)\n* Preserve ANSI escape codes\n\n## Contents\n\n* [1. Usage](#1-usage)\n* [2. API](#2-api)\n  * [2.1 configure](#21-configure)\n  * [2.2 truncate](#22-truncate)\n* [3. Extending String class](#3-extending-string-class)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"strings-truncation\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install strings-truncation\n\n## 1. Usage\n\nUse `truncate` to shorten string to 30 characters by default:\n\n```ruby\nstrings = Strings::Truncation.new\nstrings.truncate(\"I try all things, I achieve what I can.\")\n# =\u003e \"I try all things, I achieve w…\"\n```\n\nAs a convenience, you can call `truncate` method directly on a class:\n\n```ruby\nStrings::Truncation.truncate(\"I try all things, I achieve what I can.\")\n# =\u003e \"I try all things, I achieve w…\"\n```\n\nTo change the default truncation length, pass an integer as a second argument:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", 15)\n# =\u003e \"I try all thin…\"\n```\n\nOr if you want to be more explicit and flexible use `:length` keyword:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", length: 15)\n# =\u003e \"I try all thin…\"\n```\n\nYou can specify custom omission string in place of default `…`:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", omission: \"...\")\n# =\u003e \"I try all things, I achieve...\"\n```\n\nIf you wish to truncate preserving words use a string or regexp as a separator:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", separator: /\\s/)\n# =\u003e \"I try all things, I achieve…\"\n```\n\nYou can omit text from the `start`, `middle`, `end` or both `ends`:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can\", position: :middle)\n# =\u003e \"I try all thing…ve what I can.\"\n```\n\nYou can truncate text with fullwidth characters (Chinese, Japanese, Korean etc):\n\n```ruby\nstrings.truncate(\"おはようございます\", 8)\n# =\u003e \"おはよ…\"\n```\n\nAs well as truncate text that contains ANSI escape codes:\n\n```ruby\nstrings.truncate(\"\\e[34mI try all things, I achieve what I can\\e[0m\", 18)\n# =\u003e \"\\e[34mI try all things,\\e[0m…\"\n```\n\n## 2. API\n\n### 2.1 configure\n\nTo change default configuration settings at initialization use keyword arguments.\n\nFor example, to omit text from the start and separate on a whitespace character do:\n\n```ruby\nstrings = Strings::Truncation.new(position: :start, separator: /\\s/)\n```\n\nAfter initialization, you can use `configure` to change settings inside a block:\n\n```ruby\nstrings.configure do |config|\n  config.length 25\n  config.omission \"[...]\"\n  config.position :start\n  config.separator /\\s/\nend\n```\n\nAlternatively, you can also use `configure` with keyword arguments:\n\n```ruby\nstrings.configure(position: :start, separator: /\\s/)\n```\n\n### 2.2 truncate\n\nBy default a string is truncated from the end to maximum length of `30` display columns.\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\")\n# =\u003e \"I try all things, I achieve w…\"\n```\n\nTo change the default truncation length, pass an integer as a second argument:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", 15)\n# =\u003e \"I try all thin…\"\n```\n\nOr use `:length` keyword to be more explicit:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", length: 15)\n# =\u003e \"I try all thin…\"\n```\n\nThe default `…` omission character can be replaced using `:omission`:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", omission: \"...\")\n# =\u003e \"I try all things, I achieve...\"\n```\n\nYou can omit text from the `start`, `middle`, `end` or both `ends` by specifying `:position`:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can\", position: :start)\n# =\u003e \"…things, I achieve what I can.\"\n\nstrings.truncate(\"I try all things, I achieve what I can\", position: :middle)\n# =\u003e \"I try all thing…ve what I can.\"\n\nstrings.truncate(\"I try all things, I achieve what I can\", position: :ends)\n# =\u003e \"… all things, I achieve what …\"\n```\n\nTo truncate based on custom character(s) use `:separator` that accepts a string or regular expression:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", separator: /\\s/)\n=\u003e \"I try all things, I achieve…\"\n```\n\nYou can combine all settings to achieve desired result:\n\n```ruby\nstrings.truncate(\"I try all things, I achieve what I can.\", length: 20,\n                 omission: \"...\", position: :ends, separator: /\\s/)\n# =\u003e \"...I achieve what...\"\n```\n\n## 3. Extending String class\n\nThough it is highly discouraged to pollute core Ruby classes, you can add the required methods to String class by using refinements.\n\nTo include all the **Strings::Truncation** methods, you can load extensions like so:\n\n```ruby\nrequire \"strings/truncation/extensions\"\n\nusing Strings::Truncation::Extensions\n```\n\nAnd then call `truncate` directly on any string:\n\n```ruby\n\"I try all things, I achieve what I can.\".truncate(20, separator: \" \")\n# =\u003e \"I try all things, I…\"\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/strings-truncation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Strings::Truncation project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/strings-truncation/blob/master/CODE_OF_CONDUCT.md).\n\n## Copyright\n\nCopyright (c) 2019 Piotr Murach. See LICENSE for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Fstrings-truncation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrmurach%2Fstrings-truncation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Fstrings-truncation/lists"}