strings-ansi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 540eea845a26f994ba09c899527626924af8e093fa4a34732ad55cb1b58bb931
4
+ data.tar.gz: ad0d1580f3242d77f7a6629ae8d890658aa8d151d5ee3350296a531e623213c8
5
+ SHA512:
6
+ metadata.gz: c0cdd5660042a10533cefea4fa776007219ac46671c62128f9e922697087a088e56aed1d6d7bcc0fbff01e09dd239c97230368bb8d043b2dd1ce6843acbe5469
7
+ data.tar.gz: d4818de6992115b1a0a56680dc681d7783fb4824ebe078992d9a11c26910d1ee0ddc28c5d35c9d01b461e3f57635037c09610e539aaa01ce63c649361faf0a96
@@ -0,0 +1,7 @@
1
+ # Change log
2
+
3
+ ## [v0.1.0] - 2018-08-27
4
+
5
+ * Inital implementation and release
6
+
7
+ [v0.1.0]: https://github.com/piotrmurach/strings-ansi/compare/v0.1.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Piotr Murach
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,140 @@
1
+ <div align="center">
2
+ <img width="225" src="https://cdn.rawgit.com/piotrmurach/strings/master/assets/strings_logo.png" alt="strings logo" />
3
+ </div>
4
+
5
+ # Strings::ANSI
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/strings-ansi.svg)][gem]
8
+ [![Build Status](https://secure.travis-ci.org/piotrmurach/strings-ansi.svg?branch=master)][travis]
9
+ [![Build status](https://ci.appveyor.com/api/projects/status/amffv5g25m02yu9h?svg=true)][appveyor]
10
+ [![Maintainability](https://api.codeclimate.com/v1/badges/7c018a761a342ccb0746/maintainability)][codeclimate]
11
+ [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/strings-ansi/badge.svg?branch=master)][coverage]
12
+ [![Inline docs](http://inch-ci.org/github/piotrmurach/strings-ansi.svg?branch=master)][inchpages]
13
+
14
+ [gem]: http://badge.fury.io/rb/strings-ansi
15
+ [travis]: http://travis-ci.org/piotrmurach/strings-ansi
16
+ [appveyor]: https://ci.appveyor.com/project/piotrmurach/strings-ansi
17
+ [codeclimate]: https://codeclimate.com/github/piotrmurach/strings-ansi/maintainability
18
+ [coverage]: https://coveralls.io/github/piotrmurach/strings-ansi?branch=master
19
+ [inchpages]: http://inch-ci.org/github/piotrmurach/strings-ansi
20
+
21
+ > Handle ANSI escape codes in strings.
22
+
23
+ **Strings::ANSI** provides ANSI handling for [Strings](https://github.com/piotrmurach/strings) utilities.
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'strings-ansi'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install strings-ansi
40
+
41
+ ## Contents
42
+
43
+ * [1. Usage](#1-usage)
44
+ * [2. API](#2-api)
45
+ * [2.1 ansi?](#21-ansi)
46
+ * [2.2 only_ansi?](#22-only_ansi)
47
+ * [2.3 sanitize](#23-sanitize)
48
+ * [3. Extending String class](#3-extending-string-class)
49
+
50
+ ## Usage
51
+
52
+ The `Strings::ANSI` is a module with functions that can be called directly.
53
+
54
+ ```ruby
55
+ Strings::ANSI.ansi?('...')
56
+ ```
57
+
58
+ ## 2. API
59
+
60
+ ### 2.1 ansi?
61
+
62
+ To check if a string includes ANSI escape codes use `ansi?` like so:
63
+
64
+ ```ruby
65
+ Strings::ANSI.ansi?("\e[33;44mfoo\e[0m")
66
+ # => true
67
+ ```
68
+
69
+ ### 2.2 only_ansi?
70
+
71
+ To check if a string includes only ANSI escape codes use `only_ansi?`:
72
+
73
+ ```ruby
74
+ Strings::ANSI.only_ansi?("\e[33;44mfoo\e[0m")
75
+ # => false
76
+ ```
77
+
78
+ ### 2.3 sanitize
79
+
80
+ To remove ANSI codes from a string use `sanitize`:
81
+
82
+ ```ruby
83
+ Strings::ANSI.sanitize("\e[0;33;49mHello\e[0m")
84
+ # => Hello
85
+ ```
86
+
87
+ ## 3. Extending String class
88
+
89
+ Though it is highly discouraged to polute core Ruby classes, you can add the required methods to `String` class by using refinements.
90
+
91
+ For example, if you wish to only extend strings with `sanitize` method do:
92
+
93
+ ```ruby
94
+ module MyStringExt
95
+ refine String do
96
+ def sanitize
97
+ Strings::ANSI.sanitize(self)
98
+ end
99
+ end
100
+ end
101
+ ```
102
+
103
+ then `sanitize` method will be available for any strings where refinement is applied:
104
+
105
+ ```ruby
106
+ using MyStringExt
107
+
108
+ string.sanitize("\e[32mHello\e[0m")
109
+ # => Hello
110
+ ```
111
+
112
+ Alternatively, if you want to include all the **Strings::ANSI** methods:
113
+
114
+ ```ruby
115
+ require 'strings/ansi/extensions'
116
+
117
+ using Strings::ANSI::Extensions
118
+ ```
119
+
120
+ ## Development
121
+
122
+ 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.
123
+
124
+ 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](https://rubygems.org).
125
+
126
+ ## Contributing
127
+
128
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/strings-ansi. 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.
129
+
130
+ ## License
131
+
132
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
133
+
134
+ ## Code of Conduct
135
+
136
+ Everyone interacting in the Strings::Ansi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/strings-ansi/blob/master/CODE_OF_CONDUCT.md).
137
+
138
+ ## Copyright
139
+
140
+ Copyright (c) 2018 Piotr Murach. See LICENSE for further details.
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ FileList['tasks/**/*.rake'].each(&method(:import))
4
+
5
+ desc 'Run all specs'
6
+ task ci: %w[ spec ]
7
+
8
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "strings/ansi"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require_relative 'strings/ansi'
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ansi/version'
4
+
5
+ module Strings
6
+ # Helper functions for handling ANSI escape sequences
7
+ module ANSI
8
+ # The control sequence indicator
9
+ CSI = "\033"
10
+
11
+ # The code for reseting styling
12
+ RESET = "\e[0m"
13
+
14
+ # The regex to match ANSI codes
15
+ ANSI_MATCHER = '(\[)?\033(\[)?[;?\d]*[\dA-Za-z]([\];])?'
16
+
17
+ # Return a copy of string with ANSI characters removed
18
+ #
19
+ # @param [String] string
20
+ #
21
+ # @example
22
+ # Strings::ANSI.sanitize("\e[33mfoo\[e0m")
23
+ # # => "foo"
24
+ #
25
+ # @return [String]
26
+ #
27
+ # @api public
28
+ def sanitize(string)
29
+ string.gsub(/#{ANSI_MATCHER}/, '')
30
+ end
31
+ module_function :sanitize
32
+
33
+ # Check if string contains ANSI codes
34
+ #
35
+ # @param [String] string
36
+ # the string to check
37
+ #
38
+ # @example
39
+ # Strings::ANSI.ansi?("\e[33mfoo\[e0m")
40
+ # # => true
41
+ #
42
+ # @return [Boolean]
43
+ #
44
+ # @api public
45
+ def ansi?(string)
46
+ !!(string =~ /#{ANSI_MATCHER}/)
47
+ end
48
+ module_function :ansi?
49
+
50
+ # Check if string contains only ANSI codes
51
+ #
52
+ # @param [String] string
53
+ # the string to check
54
+ #
55
+ # @example
56
+ # Strings::ANSI.only_ansi?("\e[33mfoo\[e0m")
57
+ # # => false
58
+ #
59
+ # Strings::ANSI.only_ansi?("\e[33m")
60
+ # # => false
61
+ #
62
+ # @return [Boolean]
63
+ #
64
+ # @api public
65
+ def only_ansi?(string)
66
+ !!(string =~ /^(#{ANSI_MATCHER})+$/)
67
+ end
68
+ module_function :only_ansi?
69
+ end # ANSI
70
+ end # Strings
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../ansi'
4
+
5
+ module Strings
6
+ module ANSI
7
+ module Extensions
8
+ refine String do
9
+ def ansi?
10
+ ANSI.ansi?(self)
11
+ end
12
+
13
+ def only_ansi?
14
+ ANSI.only_ansi?(self)
15
+ end
16
+
17
+ def sanitize
18
+ ANSI.sanitize(self)
19
+ end
20
+ end
21
+ end # Extensions
22
+ end # ANSI
23
+ end # Strings
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strings
4
+ module ANSI
5
+ VERSION = "0.1.0"
6
+ end # ANSI
7
+ end # Strings
@@ -0,0 +1,29 @@
1
+ if ENV['COVERAGE'] || ENV['TRAVIS']
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+
10
+ SimpleCov.start do
11
+ command_name 'spec'
12
+ add_filter 'spec'
13
+ end
14
+ end
15
+
16
+ require "bundler/setup"
17
+ require "strings/ansi"
18
+
19
+ RSpec.configure do |config|
20
+ # Enable flags like --only-failures and --next-failure
21
+ config.example_status_persistence_file_path = ".rspec_status"
22
+
23
+ # Disable RSpec exposing methods globally on `Module` and `main`
24
+ config.disable_monkey_patching!
25
+
26
+ config.expect_with :rspec do |c|
27
+ c.syntax = :expect
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::ANSI, '#ansi?' do
4
+ it "does't report empty string" do
5
+ expect(Strings::ANSI.ansi?('')).to eq(false)
6
+ end
7
+
8
+ it "reports non-ansi string correctly" do
9
+ expect(Strings::ANSI.ansi?("foo")).to eq(false)
10
+ end
11
+
12
+ it "checks if code is ansi" do
13
+ expect(Strings::ANSI.ansi?("\e[33;44mfoo\e[0m")).to eq(true)
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'strings/ansi/extensions'
4
+
5
+ using Strings::ANSI::Extensions
6
+
7
+ RSpec.describe Strings::ANSI::Extensions do
8
+ it "checks for presence of ansi codes" do
9
+ expect("\e[33mfoo\e[0m".ansi?).to eq(true)
10
+ end
11
+
12
+ it "checks if string contains only ansi codes" do
13
+ expect("\e[33mfoo\e[0m".ansi?).to eq(true)
14
+ end
15
+
16
+ it "strips ansi codes" do
17
+ expect("\e[33mfoo\e[0m".sanitize).to eq('foo')
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::ANSI, '#only_ansi?' do
4
+ it "doesn't report empty string as having ansi codes" do
5
+ expect(Strings::ANSI.only_ansi?('')).to eq(false)
6
+ end
7
+
8
+ it "doesn't report string without ansi" do
9
+ expect(Strings::ANSI.only_ansi?("foo")).to eq(false)
10
+ end
11
+
12
+ it "doesn't report string containing ansi codes" do
13
+ expect(Strings::ANSI.only_ansi?("\e[33;44mfoo\e[0m")).to eq(false)
14
+ end
15
+
16
+ it "reports string with only ansi codes correctly" do
17
+ expect(Strings::ANSI.only_ansi?("\e[33;44m\e[0m")).to eq(true)
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Strings::ANSI, '#sanitize' do
4
+ {
5
+ "\e[20h" => '',
6
+ "\e[?1h" => '',
7
+ "\e[20l" => '',
8
+ "\e[?9l" => '',
9
+ "\eO" => '',
10
+ "\e[m" => '',
11
+ "\e[0m" => '',
12
+ "\eA" => '',
13
+ "\e[0;33;49;3;9;4m\e[0m" => ''
14
+ }.each do |code, expected|
15
+ it "remove #{code.inspect} from string" do
16
+ expect(Strings::ANSI.sanitize(code)).to eq(expected)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "strings/ansi/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strings-ansi"
8
+ spec.version = Strings::ANSI::VERSION
9
+ spec.authors = ["Piotr Murach"]
10
+ spec.email = [""]
11
+
12
+ spec.summary = %q{Methods for processing ANSI escape codes in strings.}
13
+ spec.description = %q{Methods for processing ANSI escape codes in strings.}
14
+ spec.homepage = "https://github.com/piotrmurach/strings"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir['{lib,spec}/**/*.rb', '{bin,tasks}/*', 'strings-ansi.gemspec']
18
+ spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/strings-ansi')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
11
+ task c: %w[ console ]
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strings-ansi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Methods for processing ANSI escape codes in strings.
56
+ email:
57
+ - ''
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - lib/strings-ansi.rb
69
+ - lib/strings/ansi.rb
70
+ - lib/strings/ansi/extensions.rb
71
+ - lib/strings/ansi/version.rb
72
+ - spec/spec_helper.rb
73
+ - spec/unit/ansi_spec.rb
74
+ - spec/unit/extensions_spec.rb
75
+ - spec/unit/only_ansi_spec.rb
76
+ - spec/unit/sanitize_spec.rb
77
+ - strings-ansi.gemspec
78
+ - tasks/console.rake
79
+ - tasks/coverage.rake
80
+ - tasks/spec.rake
81
+ homepage: https://github.com/piotrmurach/strings
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.7.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Methods for processing ANSI escape codes in strings.
105
+ test_files: []