Browse Source

wrap return carriage

main
pvincent 6 months ago
parent
commit
37f8658372
  1. 4
      TODO.md
  2. 4
      app/controllers/scores_controller.rb
  3. 27
      lib/formatters/wrapper.rb
  4. 5
      test/application_system_test_case.rb
  5. 0
      test/channels/application_cable/connection_old.rb
  6. 20
      test/controllers/scores_controller_old.rb
  7. 29
      test/formatters/wrapper2_test.rb
  8. 7
      test/models/score_old.rb
  9. 7
      test/models/score_test.rb
  10. 0
      test/system/scores_old.rb

4
TODO.md

@ -1,6 +1,10 @@
TODO TODO
==== ====
* [ ] Minitest
* [ ] Formatter
* [x] wrap warning containing \n
* [ ] "\e[0;35mhello".length => 12 instead of 5!
* [ ] Tailwind * [ ] Tailwind
* [ ] copy TailAdmin (or WindMill) layout * [ ] copy TailAdmin (or WindMill) layout
* [ ] https://demo.tailadmin.com/crm * [ ] https://demo.tailadmin.com/crm

4
app/controllers/scores_controller.rb

@ -14,7 +14,7 @@ class ScoresController < ApplicationController
# logger.info 'this is an information', { four: 4, five: 5 } # logger.info 'this is an information', { four: 4, five: 5 }
# logger.debug BigDecimal('0.0003') # logger.debug BigDecimal('0.0003')
# logger.warn 'scores are', @scores # 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.error 'this is an error message'
# logger.info 'end of normal message' # logger.info 'end of normal message'
# logger.debug @scores # logger.debug @scores
@ -66,7 +66,7 @@ class ScoresController < ApplicationController
private private
def do_an_exception def do_an_exception
raise 'Unable to destroy this score'
raise 'Unable to destroy this score' * 5
end end
# Use callbacks to share common setup or constraints between actions. # Use callbacks to share common setup or constraints between actions.

27
lib/formatters/wrapper.rb

@ -1,5 +1,7 @@
class Wrapper class Wrapper
def self.wrap(text, prefix = ' > ', length = 80) def self.wrap(text, prefix = ' > ', length = 80)
text = carriage_return_filler(text.chomp(''), length)
pure = '' pure = ''
ansi_code = {} ansi_code = {}
while (md = text.match(/\e\[\d+;?\d*m/)) while (md = text.match(/\e\[\d+;?\d*m/))
@ -41,6 +43,31 @@ class Wrapper
final final
end 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) def self.last_ansi_by_range(ansi_code, last_code, offset, pos)
pos.downto(offset) do |i| pos.downto(offset) do |i|
next unless ansi_code.has_key?(i) next unless ansi_code.has_key?(i)

5
test/application_system_test_case.rb

@ -1,5 +0,0 @@
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end

0
test/channels/application_cable/connection_test.rb → test/channels/application_cable/connection_old.rb

20
test/controllers/scores_controller_test.rb → test/controllers/scores_controller_old.rb

@ -1,45 +1,45 @@
require "test_helper"
require 'test_helper'
class ScoresControllerTest < ActionDispatch::IntegrationTest class ScoresControllerTest < ActionDispatch::IntegrationTest
setup do setup do
@score = scores(:one) @score = scores(:one)
end end
test "should get index" do
test 'should get index' do
get scores_url get scores_url
assert_response :success assert_response :success
end end
test "should get new" do
test 'should get new' do
get new_score_url get new_score_url
assert_response :success assert_response :success
end 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 } } post scores_url, params: { score: { grade: @score.grade, name: @score.name } }
end end
assert_redirected_to score_url(Score.last) assert_redirected_to score_url(Score.last)
end end
test "should show score" do
test 'should show score' do
get score_url(@score) get score_url(@score)
assert_response :success assert_response :success
end end
test "should get edit" do
test 'should get edit' do
get edit_score_url(@score) get edit_score_url(@score)
assert_response :success assert_response :success
end end
test "should update score" do
test 'should update score' do
patch score_url(@score), params: { score: { grade: @score.grade, name: @score.name } } patch score_url(@score), params: { score: { grade: @score.grade, name: @score.name } }
assert_redirected_to score_url(@score) assert_redirected_to score_url(@score)
end 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) delete score_url(@score)
end end

29
test/formatters/test_wrapper.rb → test/formatters/wrapper2_test.rb

@ -1,7 +1,8 @@
require_relative '../../lib/formatters/wrapper' require_relative '../../lib/formatters/wrapper'
require 'test/unit'
class TestWrapper < Test::Unit::TestCase
require 'minitest/autorun'
class Wrapper2Test < Minitest::Test
def test_wrap_score def test_wrap_score
assert_wrap("\ assert_wrap("\
********************\n\ ********************\n\
@ -28,18 +29,30 @@ IMIT\", 1]]", "\
20) 20)
end 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 private
def assert_wrap(expectation, text, length, prefix = '') def assert_wrap(expectation, text, length, prefix = '')
show_with_style(text, length, prefix)
assert_equal(expectation, prefix + Wrapper.wrap(text, prefix, length - prefix.length)) assert_equal(expectation, prefix + Wrapper.wrap(text, prefix, length - prefix.length))
test_with_style(text, length, prefix)
end 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 puts
end end
end end

7
test/models/score_old.rb

@ -0,0 +1,7 @@
require 'test_helper'
class ScoreTest < ActiveSupport::TestCase
test 'the truth' do
assert true
end
end

7
test/models/score_test.rb

@ -1,7 +0,0 @@
require "test_helper"
class ScoreTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0
test/system/scores_test.rb → test/system/scores_old.rb

Loading…
Cancel
Save