Skip to content

Commit

Permalink
Create ANSIStyleManager, ANSI, VersionManager to have a easy way to c…
Browse files Browse the repository at this point in the history
…olorize terminal colors with .ass.txt files. (#12)
  • Loading branch information
MarcioFPaludo authored Jun 30, 2024
1 parent 69a617a commit eadad79
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 53 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
name: Release Gem

on:
push:
tags:
- 'v*.*.*'
branches:
- main
release:
types:
- created

permissions:
contents: read
Expand All @@ -14,9 +12,6 @@ permissions:
jobs:
tests:
uses: ./.github/workflows/ruby-tests.yml
publish:
needs: tests
uses: ./.github/workflows/publish-gem.yml
documentation:
needs: publish
needs: tests
uses: ./.github/workflows/create-doc-page.yml
69 changes: 69 additions & 0 deletions .github/workflows/version-bump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Bump Version

on:
pull_request:
types:
- closed

jobs:
bump_version:
name: Build + Publish
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
if: >
github.event.pull_request.merged == true &&
contains(join(github.event.pull_request.labels.*.name, ','), 'version:major') ||
contains(join(github.event.pull_request.labels.*.name, ','), 'version:minor') ||
contains(join(github.event.pull_request.labels.*.name, ','), 'version:patch')
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6.10

- name: Bump Version
id: bump_version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(jq --raw-output .number "$GITHUB_EVENT_PATH")
LABELS=$(curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels | jq -r '.[].name')
echo "PR Labels: $LABELS"
for LABEL in $LABELS; do
case "$LABEL" in
"version:major")
rake version:major
;;
"version:minor")
rake version:minor
;;
"version:patch")
rake version:patch
;;
*)
echo "No matching version bump task for label $LABEL"
;;
esac
done
- name: Commit and push changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add lib/version.rb
git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}"
git push
- name: Create new tag
run: |
new_version=${{ steps.bump_version.outputs.new_version }}
git tag "v$new_version"
git push origin "v$new_version"
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ build-iPhoneSimulator/
/vendor/bundle
/lib/bundler/man/

# Local development script ignored by default since it's only for local development.
/development.rb
# Local development folder ignored by default since it's only for local development.
/development

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
Expand Down
12 changes: 7 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in mp_utils.gemspec
gemspec

group :test do
group :tests do
gem 'rspec', require: false
gem 'rubocop-rspec', require: false
gem 'simplecov', require: false
end

gem 'rake'
gem 'rubocop'
gem 'solargraph'
gem 'yard'
group :development do
gem 'rake'
gem 'rubocop'
gem 'solargraph'
gem 'yard'
end
53 changes: 29 additions & 24 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
# frozen_string_literal: true

require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'bundler/gem_tasks'
require 'rake/clean'
require 'rake'

CLEAN.include %w[pkg coverage *.gem]

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require_relative 'lib/mp_utils'
require_relative 'lib/version'

CLEAN.include %w[pkg coverage *.gem doc .yardoc]
RSpec::Core::RakeTask.new(:spec)

require 'rubocop/rake_task'

RuboCop::RakeTask.new

task default: %i[spec rubocop]

require_relative 'lib/version'
require 'rake'

namespace :version do
%i[major minor patch].each do |part|
desc "Bump #{part} version"
task part do
current_version = MPUtils::VERSION.split('.').map(&:to_i)
new_version = case part
when :major
[current_version[0] + 1, 0, 0]
when :minor
[current_version[0], current_version[1] + 1, 0]
when :patch
current_version[0..1] + [current_version[2] + 1]
end.join('.')

version = VersionManager.new(MPUtils::VERSION)
path = File.join('lib', 'version.rb')

case part
when :major
version.increment_major
when :minor
version.increment_minor
when :patch
version.increment_pathc
end

content = File.read(path)
puts content
content.gsub!(/VERSION.+'\d+\.\d+\.\d+'/, "VERSION = '#{new_version}'")
puts content
content.gsub!(/VERSION.+'#{MPUtils::VERSION}'/, "VERSION = '#{version}'")
File.open(path, 'w') { |file| file << content }

puts "Version bumped to #{new_version}"
system("echo \"::set-output name=new_version::#{version}\"")
end
end
end

namespace :doc do
desc 'Bump version'
task :test do
system('yard doc')
system('open "http://localhost:8808"')
system('yard server')
end
end
13 changes: 8 additions & 5 deletions lib/mp_utils.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

require 'utils/key'
require 'utils/array'
require 'utils/message'
require 'utils/question'
require 'resources/path_helper'
require_relative 'utils/key'
require_relative 'utils/ansi'
require_relative 'utils/array'
require_relative 'utils/message'
require_relative 'utils/question'
require_relative 'utils/version_manager'
require_relative 'utils/ansi_style_manager'
require_relative 'resources/path_helper'
125 changes: 125 additions & 0 deletions lib/utils/ansi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true

require_relative 'constants'

# The ANSI class is responsible for generating ANSI codes for text styling in terminals.
# It allows the combination of multiple style and color codes.
#
# @example
# ansi = ANSI.new(:bold)
# puts ansi.to_s # => "\e[1m"
#
# @example
# ansi = ANSI.new([:bold, :red])
# puts ansi.to_s # => "\e[1;31m"
class ANSI
# Hash that maps style and color names to their respective ANSI codes.
CODES_HASH = {
reset_all: 0,

# Effects
bold: 1,
faint: 2,
italic: 3,
underlined: 4,
blinking: 5,
inverse: 7,
hidden: 8,
strike: 9,
plain: 21,

# Remove Effects
remove_bold: 22,
remove_faint: 22,
remove_italic: 23,
remove_underlined: 24,
remove_blinking: 25,
remove_inverse: 27,
remove_hidden: 28,
remove_strike: 29,
remove_plain: 24,

# Colors
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
reset_color: 39,
rgb_format: [38, 2],
numeric_format: [38, 5],

# Background Colors
background_black: 40,
background_red: 41,
background_green: 42,
background_yellow: 43,
background_blue: 44,
background_magenta: 45,
background_cyan: 46,
background_white: 47,
background_rgb_format: [48, 2],
background_numeric_format: [48, 5],
reset_background: 49
}.freeze

# @return [Array<Integer>] the ANSI codes stored in the instance.
attr_reader :codes

# Initializes a new instance of the ANSI class.
#
# @param code [Array<Symbol>, Symbol, String] One or more ANSI codes represented as symbols, integers, or strings.
def initialize(code)
@codes = if code.is_a?(Array)
ANSI.normalize_array_codes(code)
elsif code.is_a?(Symbol)
[CODES_HASH[code]]
else
ANSI.normalize_array_codes(code.split(';'))
end
end

# Adds ANSI codes from another instance of the ANSI class.
#
# @param ansi [ANSI] The instance of the ANSI class whose codes will be added.
# @return [ANSI] A new instance of the ANSI class with the combined codes.
# @raise [RuntimeError] If the argument is not an instance of the ANSI class.
def append(ansi)
raise 'Needs be an instance of ANSI' unless ansi.is_a?(ANSI)

ANSI.new(@codes.union(ansi.codes))
end

# Adds ANSI codes from another instance of the ANSI class and updates the current instance.
#
# @param ansi [ANSI] The instance of the ANSI class whose codes will be added.
# @return [void]
def append!(ansi)
@codes = append(ansi).codes
end

# Converts the ANSI codes stored in the instance to a formatted string.
#
# @return [String] The formatted string with the ANSI codes.
def to_s
"\e[#{@codes.flatten.join(';')}m"
end

# Normalizes an array of codes, converting symbols and strings to their respective ANSI codes.
#
# @param array [Array<Symbol, String, Integer>] An array of codes to be normalized.
# @return [Array<Integer>] The normalized array of ANSI codes.
def self.normalize_array_codes(array)
array.map do |value|
next normalize_array_codes(value) if value.is_a?(Array)
next value if value.is_a?(Integer)
next CODES_HASH[value] if value.is_a?(Symbol)
next value.to_i if value.match?(CONSTANTS::ONLY_DIGITS_REGEX)

CODES_HASH[value.to_sym]
end
end
end
Loading

0 comments on commit eadad79

Please sign in to comment.