From 37f86583722479697998220ae63e87de1edf75df Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 14 May 2024 00:18:51 +0400 Subject: [PATCH] wrap return carriage --- TODO.md | 4 +++ app/controllers/scores_controller.rb | 4 +-- lib/formatters/wrapper.rb | 27 +++++++++++++++++ test/application_system_test_case.rb | 5 ---- .../{connection_test.rb => connection_old.rb} | 0 ...oller_test.rb => scores_controller_old.rb} | 20 ++++++------- .../{test_wrapper.rb => wrapper2_test.rb} | 29 ++++++++++++++----- test/models/score_old.rb | 7 +++++ test/models/score_test.rb | 7 ----- test/system/{scores_test.rb => scores_old.rb} | 0 10 files changed, 71 insertions(+), 32 deletions(-) delete mode 100644 test/application_system_test_case.rb rename test/channels/application_cable/{connection_test.rb => connection_old.rb} (100%) rename test/controllers/{scores_controller_test.rb => scores_controller_old.rb} (69%) rename test/formatters/{test_wrapper.rb => wrapper2_test.rb} (52%) create mode 100644 test/models/score_old.rb delete mode 100644 test/models/score_test.rb rename test/system/{scores_test.rb => scores_old.rb} (100%) diff --git a/TODO.md b/TODO.md index adc90f9..1f9cd44 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,10 @@ TODO ==== +* [ ] Minitest +* [ ] Formatter + * [x] wrap warning containing \n + * [ ] "\e[0;35mhello".length => 12 instead of 5! * [ ] Tailwind * [ ] copy TailAdmin (or WindMill) layout * [ ] https://demo.tailadmin.com/crm diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 89d9e04..0de2ca3 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -14,7 +14,7 @@ class ScoresController < ApplicationController # logger.info 'this is an information', { four: 4, five: 5 } # logger.debug BigDecimal('0.0003') # logger.warn 'scores are', @scores - # logger.warn 'this is a warning' + logger.warn "this is a warning\n yop\n cool" # logger.error 'this is an error message' # logger.info 'end of normal message' # logger.debug @scores @@ -66,7 +66,7 @@ class ScoresController < ApplicationController private def do_an_exception - raise 'Unable to destroy this score' + raise 'Unable to destroy this score' * 5 end # Use callbacks to share common setup or constraints between actions. diff --git a/lib/formatters/wrapper.rb b/lib/formatters/wrapper.rb index 0ae5f81..51fe684 100644 --- a/lib/formatters/wrapper.rb +++ b/lib/formatters/wrapper.rb @@ -1,5 +1,7 @@ class Wrapper def self.wrap(text, prefix = ' > ', length = 80) + text = carriage_return_filler(text.chomp(''), length) + pure = '' ansi_code = {} while (md = text.match(/\e\[\d+;?\d*m/)) @@ -41,6 +43,31 @@ class Wrapper final end + private + + def self.carriage_return_filler(text, length) + lines = text.split("\n") + return text if lines.count < 2 + + last_ansi = nil + final = lines.map do |line| + fill_count = length - line.length + current = line + result = last_ansi ? "#{last_ansi}#{line}" : line + last_ansi = nil if last_ansi == "\e[0m" + while (md = current.match(/\e\[\d+;?\d*m/)) + last_ansi = md.to_s + fill_count += last_ansi.length + current = md.post_match + end + result = "#{result}\e[0m" if last_ansi + next result if fill_count < 1 + + "#{result}#{' ' * fill_count}" + end.join + last_ansi ? "#{final}\e[0m" : final + end + def self.last_ansi_by_range(ansi_code, last_code, offset, pos) pos.downto(offset) do |i| next unless ansi_code.has_key?(i) diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb deleted file mode 100644 index d19212a..0000000 --- a/test/application_system_test_case.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "test_helper" - -class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - driven_by :selenium, using: :chrome, screen_size: [1400, 1400] -end diff --git a/test/channels/application_cable/connection_test.rb b/test/channels/application_cable/connection_old.rb similarity index 100% rename from test/channels/application_cable/connection_test.rb rename to test/channels/application_cable/connection_old.rb diff --git a/test/controllers/scores_controller_test.rb b/test/controllers/scores_controller_old.rb similarity index 69% rename from test/controllers/scores_controller_test.rb rename to test/controllers/scores_controller_old.rb index 911b107..a6691a4 100644 --- a/test/controllers/scores_controller_test.rb +++ b/test/controllers/scores_controller_old.rb @@ -1,45 +1,45 @@ -require "test_helper" +require 'test_helper' class ScoresControllerTest < ActionDispatch::IntegrationTest setup do @score = scores(:one) end - test "should get index" do + test 'should get index' do get scores_url assert_response :success end - test "should get new" do + test 'should get new' do get new_score_url assert_response :success end - test "should create score" do - assert_difference("Score.count") do + test 'should create score' do + assert_difference('Score.count') do post scores_url, params: { score: { grade: @score.grade, name: @score.name } } end assert_redirected_to score_url(Score.last) end - test "should show score" do + test 'should show score' do get score_url(@score) assert_response :success end - test "should get edit" do + test 'should get edit' do get edit_score_url(@score) assert_response :success end - test "should update score" do + test 'should update score' do patch score_url(@score), params: { score: { grade: @score.grade, name: @score.name } } assert_redirected_to score_url(@score) end - test "should destroy score" do - assert_difference("Score.count", -1) do + test 'should destroy score' do + assert_difference('Score.count', -1) do delete score_url(@score) end diff --git a/test/formatters/test_wrapper.rb b/test/formatters/wrapper2_test.rb similarity index 52% rename from test/formatters/test_wrapper.rb rename to test/formatters/wrapper2_test.rb index e6895c2..98d11e9 100644 --- a/test/formatters/test_wrapper.rb +++ b/test/formatters/wrapper2_test.rb @@ -1,7 +1,8 @@ require_relative '../../lib/formatters/wrapper' -require 'test/unit' -class TestWrapper < Test::Unit::TestCase +require 'minitest/autorun' + +class Wrapper2Test < Minitest::Test def test_wrap_score assert_wrap("\ ********************\n\ @@ -28,18 +29,30 @@ IMIT\", 1]]", "\ 20) end + def test_carriage_return + assert_wrap('text', "text\n\n", 10) + assert_wrap('first ', "first \n", 10) + assert_wrap('first ', "first \n", 10) + assert_wrap('first ', "first \n", 10) + assert_wrap("first \n second ", "first\n second\n", 10) + + assert_wrap("\e[0;35mhello\e[0m \n\e[0;35m\e[0m\n\e[0;35myop", "\e[0;35mhello\n\nyop", 10) + end + private def assert_wrap(expectation, text, length, prefix = '') + show_with_style(text, length, prefix) assert_equal(expectation, prefix + Wrapper.wrap(text, prefix, length - prefix.length)) - test_with_style(text, length, prefix) end - def test_with_style(text, length, prefix = '') - begint = "my text is:\n" - endoft = "a final is:\n" - puts "#{begint}#{prefix}#{text}\e[0m" - puts "#{endoft}#{prefix}#{Wrapper.wrap(text, prefix, length - prefix.length)}\e[0m" + def show_with_style(text, length, prefix = '') + expectation = Wrapper.wrap(text, prefix, length - prefix.length) + puts "original_o is:\n#{prefix}[#{text}]\e[0m" + puts "original_i is:\n#{prefix}[#{text.inspect}]\e[0m" + puts + puts "final_o is:\n#{prefix}[#{expectation}]\e[0m" + puts "final_i is:\n#{prefix}[#{expectation.inspect}]\e[0m" puts end end diff --git a/test/models/score_old.rb b/test/models/score_old.rb new file mode 100644 index 0000000..42a27fd --- /dev/null +++ b/test/models/score_old.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ScoreTest < ActiveSupport::TestCase + test 'the truth' do + assert true + end +end diff --git a/test/models/score_test.rb b/test/models/score_test.rb deleted file mode 100644 index eeb5563..0000000 --- a/test/models/score_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "test_helper" - -class ScoreTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/system/scores_test.rb b/test/system/scores_old.rb similarity index 100% rename from test/system/scores_test.rb rename to test/system/scores_old.rb