All Projects → piotrmurach → strings-truncation

piotrmurach / strings-truncation

Licence: MIT license
Truncate strings with fullwidth characters and ANSI codes.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to strings-truncation

Strings
A set of useful functions for transforming strings.
Stars: ✭ 111 (+146.67%)
Mutual labels:  text, strings, ansi
strings-ansi
Handle ANSI escape codes in strings
Stars: ✭ 17 (-62.22%)
Mutual labels:  rubygem, strings, ansi
ngx-line-truncation
An Angular line truncating solution. Truncate text block by given line number and add ellipsis to the end.
Stars: ✭ 13 (-71.11%)
Mutual labels:  truncate, shorten, truncation
react-middle-truncate
A React component for intelligently truncating text in the middle of the string.
Stars: ✭ 27 (-40%)
Mutual labels:  text, truncate, truncation
strings-case
Convert strings between different cases.
Stars: ✭ 65 (+44.44%)
Mutual labels:  rubygem, strings
learning R
List of resources for learning R
Stars: ✭ 32 (-28.89%)
Mutual labels:  strings, strings-manipulation
pytextcodifier
📦 Turn your text files into codified images or your codified images into text files.
Stars: ✭ 14 (-68.89%)
Mutual labels:  text, strings
Str metrics
Ruby gem (native extension in Rust) providing implementations of various string metrics
Stars: ✭ 68 (+51.11%)
Mutual labels:  rubygem, strings
regXwild
⏱ Superfast ^Advanced wildcards++? | Unique algorithms that was implemented on native unmanaged C++ but easily accessible in .NET via Conari (with caching of 0x29 opcodes +optimizations) etc.
Stars: ✭ 20 (-55.56%)
Mutual labels:  text, strings
ansiart2utf8
Processes legacy BBS-style ANSI art (ACiDDraw, PabloDraw, etc.) to UTF-8. Escape codes and line endings are processed for terminal friendliness.
Stars: ✭ 32 (-28.89%)
Mutual labels:  text, ansi
lt2circuitikz
Python (3.5) tool to convert .asc files into circuiTikz graphics
Stars: ✭ 57 (+26.67%)
Mutual labels:  text
svensktext
Svenska språkresurser: kvinno- och mansnamn, orter, län, kommuner, länder, nationaliteter, yrken, sentimentlexikon, moral, stoppord, myndigheter m.m.
Stars: ✭ 54 (+20%)
Mutual labels:  text
text-to-freemind
A simple text to Freemind conversion program
Stars: ✭ 38 (-15.56%)
Mutual labels:  text
glimmer-dsl-swt
Glimmer DSL for SWT (JRuby Desktop Development GUI Framework)
Stars: ✭ 53 (+17.78%)
Mutual labels:  rubygem
ansilove-php
A set of tools to convert ANSi and artscene related file formats into PNG images
Stars: ✭ 56 (+24.44%)
Mutual labels:  ansi
lda2vec
Mixing Dirichlet Topic Models and Word Embeddings to Make lda2vec from this paper https://arxiv.org/abs/1605.02019
Stars: ✭ 27 (-40%)
Mutual labels:  text
consul-templaterb
consul-template-like with erb (ruby) template expressiveness
Stars: ✭ 65 (+44.44%)
Mutual labels:  rubygem
muse-as-service
REST API for sentence tokenization and embedding using Multilingual Universal Sentence Encoder.
Stars: ✭ 45 (+0%)
Mutual labels:  text
gdb-dashboard
Modular visual interface for GDB in Python
Stars: ✭ 8,699 (+19231.11%)
Mutual labels:  ansi
n65
An assembler for the 6502 microprocessor written in Ruby
Stars: ✭ 12 (-73.33%)
Mutual labels:  rubygem
strings logo

Strings::Truncation

Gem Version Actions CI Build status Maintainability Coverage Status Inline docs

Truncate strings with fullwidth characters and ANSI codes.

Features

  • No monkey-patching String class
  • Omit text from the start, middle, end or both ends
  • Account for fullwidth characters in encodings such as UTF-8, EUC-JP
  • Shorten text without whitespaces between words (Chinese, Japanese, Korean etc)
  • Preserve ANSI escape codes

Contents

Installation

Add this line to your application's Gemfile:

gem "strings-truncation"

And then execute:

$ bundle

Or install it yourself as:

$ gem install strings-truncation

1. Usage

Use truncate to shorten string to 30 characters by default:

strings = Strings::Truncation.new
strings.truncate("I try all things, I achieve what I can.")
# => "I try all things, I achieve w…"

As a convenience, you can call truncate method directly on a class:

Strings::Truncation.truncate("I try all things, I achieve what I can.")
# => "I try all things, I achieve w…"

To change the default truncation length, pass an integer as a second argument:

strings.truncate("I try all things, I achieve what I can.", 15)
# => "I try all thin…"

Or if you want to be more explicit and flexible use :length keyword:

strings.truncate("I try all things, I achieve what I can.", length: 15)
# => "I try all thin…"

You can specify custom omission string in place of default :

strings.truncate("I try all things, I achieve what I can.", omission: "...")
# => "I try all things, I achieve..."

If you wish to truncate preserving words use a string or regexp as a separator:

strings.truncate("I try all things, I achieve what I can.", separator: /\s/)
# => "I try all things, I achieve…"

You can omit text from the start, middle, end or both ends:

strings.truncate("I try all things, I achieve what I can", position: :middle)
# => "I try all thing…ve what I can."

You can truncate text with fullwidth characters (Chinese, Japanese, Korean etc):

strings.truncate("おはようございます", 8)
# => "おはよ…"

As well as truncate text that contains ANSI escape codes:

strings.truncate("\e[34mI try all things, I achieve what I can\e[0m", 18)
# => "\e[34mI try all things,\e[0m…"

2. API

2.1 configure

To change default configuration settings at initialization use keyword arguments.

For example, to omit text from the start and separate on a whitespace character do:

strings = Strings::Truncation.new(position: :start, separator: /\s/)

After initialization, you can use configure to change settings inside a block:

strings.configure do |config|
  config.length 25
  config.omission "[...]"
  config.position :start
  config.separator /\s/
end

Alternatively, you can also use configure with keyword arguments:

strings.configure(position: :start, separator: /\s/)

2.2 truncate

By default a string is truncated from the end to maximum length of 30 display columns.

strings.truncate("I try all things, I achieve what I can.")
# => "I try all things, I achieve w…"

To change the default truncation length, pass an integer as a second argument:

strings.truncate("I try all things, I achieve what I can.", 15)
# => "I try all thin…"

Or use :length keyword to be more explicit:

strings.truncate("I try all things, I achieve what I can.", length: 15)
# => "I try all thin…"

The default omission character can be replaced using :omission:

strings.truncate("I try all things, I achieve what I can.", omission: "...")
# => "I try all things, I achieve..."

You can omit text from the start, middle, end or both ends by specifying :position:

strings.truncate("I try all things, I achieve what I can", position: :start)
# => "…things, I achieve what I can."

strings.truncate("I try all things, I achieve what I can", position: :middle)
# => "I try all thing…ve what I can."

strings.truncate("I try all things, I achieve what I can", position: :ends)
# => "… all things, I achieve what …"

To truncate based on custom character(s) use :separator that accepts a string or regular expression:

strings.truncate("I try all things, I achieve what I can.", separator: /\s/)
=> "I try all things, I achieve…"

You can combine all settings to achieve desired result:

strings.truncate("I try all things, I achieve what I can.", length: 20,
                 omission: "...", position: :ends, separator: /\s/)
# => "...I achieve what..."

3. Extending String class

Though it is highly discouraged to pollute core Ruby classes, you can add the required methods to String class by using refinements.

To include all the Strings::Truncation methods, you can load extensions like so:

require "strings/truncation/extensions"

using Strings::Truncation::Extensions

And then call truncate directly on any string:

"I try all things, I achieve what I can.".truncate(20, separator: " ")
# => "I try all things, I…"

Development

After 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.

To 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.

Contributing

Bug 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 code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Strings::Truncation project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright

Copyright (c) 2019 Piotr Murach. See LICENSE for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].