View Javadoc
1   /*
2    * Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.buildplan.display;
17  
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.StringJoiner;
22  import org.apache.maven.lifecycle.DefaultLifecycles;
23  import org.apache.maven.plugin.MojoExecution;
24  import org.apache.maven.shared.utils.logging.MessageUtils;
25  
26  public class ListTableDescriptor extends AbstractTableDescriptor {
27  
28      private static final int ANSI_COLOR_CODES_LENGTH =
29              MessageUtils.buffer().mojo("").toString().length();
30  
31      private int pluginSize;
32      private int versionSize;
33      private int phaseSize;
34      private int lifecycleSize;
35      private int executionIdSize;
36      private int goalSize;
37  
38      public static ListTableDescriptor of(Collection<MojoExecution> executions, DefaultLifecycles defaultLifecycles) {
39  
40          Map<TableColumn, Integer> maxSize = findMaxSize(executions, defaultLifecycles, TableColumn.values());
41  
42          return new ListTableDescriptor()
43                  .setPluginSize(maxSize.get(TableColumn.ARTIFACT_ID))
44                  .setVersionSize(maxSize.get(TableColumn.VERSION))
45                  .setPhaseSize(maxSize.get(TableColumn.PHASE))
46                  .setLifecycleSize(maxSize.get(TableColumn.LIFECYCLE))
47                  .setGoalSize(maxSize.get(TableColumn.GOAL))
48                  .setExecutionIdSize(maxSize.get(TableColumn.EXECUTION_ID));
49      }
50  
51      public String rowFormat() {
52          return columns(getPluginSize());
53      }
54  
55      private String columns(int pluginSize) {
56          StringBuilder builder = new StringBuilder();
57          if (lifecycleSize > 0) {
58              builder.append(FORMAT_LEFT_ALIGN)
59                      .append(getLifecycleSize())
60                      .append(FORMAT_STRING)
61                      .append(SEPARATOR);
62          }
63          builder.append(FORMAT_LEFT_ALIGN)
64                  .append(getPhaseSize())
65                  .append(FORMAT_STRING)
66                  .append(SEPARATOR)
67                  .append(FORMAT_LEFT_ALIGN)
68                  .append(pluginSize)
69                  .append(FORMAT_STRING)
70                  .append(SEPARATOR)
71                  .append(FORMAT_LEFT_ALIGN)
72                  .append(getVersionSize())
73                  .append(FORMAT_STRING)
74                  .append(SEPARATOR);
75  
76          builder.append(FORMAT_LEFT_ALIGN)
77                  .append(getGoalSize())
78                  .append(FORMAT_STRING)
79                  .append(SEPARATOR)
80                  .append(FORMAT_LEFT_ALIGN)
81                  .append(getExecutionIdSize())
82                  .append(FORMAT_STRING);
83          return builder.toString();
84      }
85  
86      public String titleFormat() {
87          if (MessageUtils.isColorEnabled()) {
88              return columns(getPluginSize() - ANSI_COLOR_CODES_LENGTH);
89          }
90          return columns(getPluginSize());
91      }
92  
93      public int width() {
94          return withSeparator(
95                  getPluginSize(),
96                  getVersionSize(),
97                  getPhaseSize(),
98                  getLifecycleSize(),
99                  getExecutionIdSize(),
100                 getGoalSize());
101     }
102 
103     private int withSeparator(int... ints) {
104         int width = Arrays.stream(ints).sum() + (ints.length - 1) * SEPARATOR.length();
105         return MessageUtils.isColorEnabled() ? width - ANSI_COLOR_CODES_LENGTH : width;
106     }
107 
108     @Override
109     public String toString() {
110         return new StringJoiner(", ", ListTableDescriptor.class.getSimpleName() + "[", "]")
111                 .add("Plugin column size=" + pluginSize)
112                 .add("Plugin version size=" + versionSize)
113                 .add("Phase column size=" + phaseSize)
114                 .add("Lifecycle column size=" + lifecycleSize)
115                 .add("Execution ID column size=" + executionIdSize)
116                 .add("Goal column size=" + goalSize)
117                 .add("width=" + width())
118                 .toString();
119     }
120 
121     public int getPluginSize() {
122         return pluginSize;
123     }
124 
125     public ListTableDescriptor setPluginSize(int pluginSize) {
126         this.pluginSize = pluginSize;
127         return this;
128     }
129 
130     private int getVersionSize() {
131         return versionSize;
132     }
133 
134     public ListTableDescriptor setVersionSize(int versionSize) {
135         this.versionSize = versionSize;
136         return this;
137     }
138 
139     public int getPhaseSize() {
140         return phaseSize;
141     }
142 
143     public ListTableDescriptor setPhaseSize(int phaseSize) {
144         this.phaseSize = phaseSize;
145         return this;
146     }
147 
148     public int getLifecycleSize() {
149         return lifecycleSize;
150     }
151 
152     public ListTableDescriptor setLifecycleSize(int lifecycleSize) {
153         this.lifecycleSize = lifecycleSize;
154         return this;
155     }
156 
157     public int getExecutionIdSize() {
158         return executionIdSize;
159     }
160 
161     public ListTableDescriptor setExecutionIdSize(int executionIdSize) {
162         this.executionIdSize = executionIdSize;
163         return this;
164     }
165 
166     public int getGoalSize() {
167         return goalSize;
168     }
169 
170     public ListTableDescriptor setGoalSize(int goalSize) {
171         this.goalSize = goalSize;
172         return this;
173     }
174 
175     public void hideLifecycle() {
176         this.lifecycleSize = -1 * SEPARATOR.length();
177     }
178 }