Welcome to mirror list, hosted at ThFree Co, Russian Federation.

style.rb « ansi2json « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d38ea55866bd7b07abac04371305211df3fd6de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Gitlab
  module Ci
    module Ansi2json
      class Style
        attr_reader :fg, :bg, :mask

        def initialize(fg: nil, bg: nil, mask: 0)
          @fg = fg
          @bg = bg
          @mask = mask

          update_formats
        end

        def update(ansi_commands)
          # treat e\[m as \e[0m
          ansi_commands = ['0'] if ansi_commands.empty?

          evaluate_stack_command(ansi_commands)
        end

        def set?
          @fg || @bg || @formats.any?
        end

        def reset!
          @fg = nil
          @bg = nil
          @mask = 0
          @formats = []
        end

        def ==(other)
          self.to_h == other.to_h
        end

        def to_s
          [@fg, @bg, @formats].flatten.compact.join(' ')
        end

        def to_h
          { fg: @fg, bg: @bg, mask: @mask }
        end

        private

        def evaluate_stack_command(ansi_commands)
          command = ansi_commands.shift
          return unless command

          if changes = Gitlab::Ci::Ansi2json::Parser.new(command, ansi_commands).changes
            apply_changes(changes)
          end

          evaluate_stack_command(ansi_commands)
        end

        def apply_changes(changes)
          case
          when changes[:reset]
            reset!
          when changes.key?(:fg)
            @fg = changes[:fg]
          when changes.key?(:bg)
            @bg = changes[:bg]
          when changes[:enable]
            @mask |= changes[:enable]
          when changes[:disable]
            @mask &= ~changes[:disable]
          else
            return
          end

          update_formats
        end

        def update_formats
          # Most terminals show bold colored text in the light color variant
          # Let's mimic that here
          if @fg.present? && Gitlab::Ci::Ansi2json::Parser.bold?(@mask)
            @fg = @fg.sub(/fg-([a-z]{2,}+)/, 'fg-l-\1')
          end

          @formats = Gitlab::Ci::Ansi2json::Parser.matching_formats(@mask)
        end
      end
    end
  end
end