Method: Pindo::TaskSystem::MultiLineTaskDisplay#render

Defined in:
lib/pindo/module/task/output/multi_line_task_display.rb

#render(max_lines: nil, max_width: nil) ⇒ String

渲染为终端字符串

Parameters:

  • max_lines (Integer, nil) (defaults to: nil)

    最大显示行数(可选覆盖默认值)

  • max_width (Integer, nil) (defaults to: nil)

    最大显示宽度(可选,用于截断长行以防止换行破坏布局)

Returns:

  • (String)

    终端显示字符串



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pindo/module/task/output/multi_line_task_display.rb', line 101

def render(max_lines: nil, max_width: nil)
  @mutex.synchronize do
    lines_output = []

    # 状态图标和颜色
    icon = case @status
           when STATUS_RUNNING then '⣾'
           when STATUS_SUCCESS then '✔'
           when STATUS_ERROR then '✖'
           when STATUS_PENDING then '⊙'
           else '?'
           end

    color_code = case @status
                 when STATUS_SUCCESS then "\e[32m"  # 绿色
                 when STATUS_ERROR then "\e[31m"    # 红色
                 when STATUS_PENDING then "\e[33m"  # 黄色
                 else "\e[34m"                      # 蓝色(运行中)
                 end

    # 任务名称行
    # 截断任务名称(保留一些空间给图标)
    task_line = "  #{color_code}#{icon} #{@task_name}\e[0m"
    lines_output << task_line

    # 消息行(树形结构)
    display_lines = max_lines ? @lines.last(max_lines) : @lines
    display_lines.each_with_index do |line, index|
      prefix = if index == display_lines.size - 1
                 '└─'  # 最后一行
               else
                 '├─'  # 中间行
               end
      
      full_line = "    #{prefix} #{line}"
      
      # 如果指定了最大宽度,进行截断
      if max_width && max_width > 0
        # 简单截断防止换行
        # 注意:这里假设全角字符占2宽度的情况未被完美处理,但对于防止严重错乱已足够
        if full_line.length > max_width
           # 保留最后 3 个字符为 "..."
           full_line = full_line[0, max_width - 3] + "..."
        end
      end

      lines_output << full_line
    end

    lines_output.join("\n")
  end
end