diff --git a/lib/travis/api/app/responders/plain.rb b/lib/travis/api/app/responders/plain.rb index 83bbcd30..89328c96 100644 --- a/lib/travis/api/app/responders/plain.rb +++ b/lib/travis/api/app/responders/plain.rb @@ -16,7 +16,15 @@ module Travis::Api::App::Responders headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}") endpoint.content_type 'text/plain' - halt resource.content + halt(params[:deansi] ? clear_ansi(resource.content) : resource.content) end + + private + + def clear_ansi(content) + content.gsub(/\r\r/, "\r") + .gsub(/^.*\r(?!$)/, '') + .gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/m, '') + end end end diff --git a/spec/unit/endpoint/artifacts_spec.rb b/spec/unit/endpoint/artifacts_spec.rb index 8210f7cd..b701cff7 100644 --- a/spec/unit/endpoint/artifacts_spec.rb +++ b/spec/unit/endpoint/artifacts_spec.rb @@ -26,5 +26,19 @@ describe Travis::Api::App::Endpoint::Artifacts do response.body.should == artifact.content response.headers['Content-Disposition'].should == "attachment; filename=\"#{artifact.id}\"" end + + describe 'with deansi param' do + let(:content) { + "Fetching (0%)\rFetching (10%)\rFetching (100%)\n\e[32m" + } + let(:artifact) { Factory(:log, :content => content) } + + it 'clears ansi escape control characters' do + response = get("/artifacts/#{id}.txt", {'deansi' => true}) + + response.should be_ok + response.body.should == "Fetching (100%)\n" + end + end end end