View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.fusesource.jansi;
20  
21  import java.util.ArrayList;
22  
23  /**
24   * Provides a fluent API for generating
25   * <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences">ANSI escape sequences</a>.
26   *
27   * This class comes from Jansi and is provided for backward compatibility
28   * with maven-shared-utils; Maven itself no longer ships Jansi.
29   *
30   * @see AnsiConsole
31   */
32  @Deprecated
33  @SuppressWarnings("unused")
34  public class Ansi implements Appendable {
35  
36      private static final char FIRST_ESC_CHAR = 27;
37      private static final char SECOND_ESC_CHAR = '[';
38  
39      /**
40       * <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#Colors">ANSI 8 colors</a> for fluent API
41       */
42      public enum Color {
43          BLACK(0, "BLACK"),
44          RED(1, "RED"),
45          GREEN(2, "GREEN"),
46          YELLOW(3, "YELLOW"),
47          BLUE(4, "BLUE"),
48          MAGENTA(5, "MAGENTA"),
49          CYAN(6, "CYAN"),
50          WHITE(7, "WHITE"),
51          DEFAULT(9, "DEFAULT");
52  
53          private final int value;
54          private final String name;
55  
56          Color(int index, String name) {
57              this.value = index;
58              this.name = name;
59          }
60  
61          @Override
62          public String toString() {
63              return name;
64          }
65  
66          public int value() {
67              return value;
68          }
69  
70          public int fg() {
71              return value + 30;
72          }
73  
74          public int bg() {
75              return value + 40;
76          }
77  
78          public int fgBright() {
79              return value + 90;
80          }
81  
82          public int bgBright() {
83              return value + 100;
84          }
85      }
86  
87      /**
88       * Display attributes, also known as
89       * <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters">SGR
90       * (Select Graphic Rendition) parameters</a>.
91       */
92      public enum Attribute {
93          RESET(0, "RESET"),
94          INTENSITY_BOLD(1, "INTENSITY_BOLD"),
95          INTENSITY_FAINT(2, "INTENSITY_FAINT"),
96          ITALIC(3, "ITALIC_ON"),
97          UNDERLINE(4, "UNDERLINE_ON"),
98          BLINK_SLOW(5, "BLINK_SLOW"),
99          BLINK_FAST(6, "BLINK_FAST"),
100         NEGATIVE_ON(7, "NEGATIVE_ON"),
101         CONCEAL_ON(8, "CONCEAL_ON"),
102         STRIKETHROUGH_ON(9, "STRIKETHROUGH_ON"),
103         UNDERLINE_DOUBLE(21, "UNDERLINE_DOUBLE"),
104         INTENSITY_BOLD_OFF(22, "INTENSITY_BOLD_OFF"),
105         ITALIC_OFF(23, "ITALIC_OFF"),
106         UNDERLINE_OFF(24, "UNDERLINE_OFF"),
107         BLINK_OFF(25, "BLINK_OFF"),
108         NEGATIVE_OFF(27, "NEGATIVE_OFF"),
109         CONCEAL_OFF(28, "CONCEAL_OFF"),
110         STRIKETHROUGH_OFF(29, "STRIKETHROUGH_OFF");
111 
112         private final int value;
113         private final String name;
114 
115         Attribute(int index, String name) {
116             this.value = index;
117             this.name = name;
118         }
119 
120         @Override
121         public String toString() {
122             return name;
123         }
124 
125         public int value() {
126             return value;
127         }
128     }
129 
130     /**
131      * ED (Erase in Display) / EL (Erase in Line) parameter (see
132      * <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences">CSI sequence J and K</a>)
133      * @see Ansi#eraseScreen(Erase)
134      * @see Ansi#eraseLine(Erase)
135      */
136     public enum Erase {
137         FORWARD(0, "FORWARD"),
138         BACKWARD(1, "BACKWARD"),
139         ALL(2, "ALL");
140 
141         private final int value;
142         private final String name;
143 
144         Erase(int index, String name) {
145             this.value = index;
146             this.name = name;
147         }
148 
149         @Override
150         public String toString() {
151             return name;
152         }
153 
154         public int value() {
155             return value;
156         }
157     }
158 
159     @FunctionalInterface
160     public interface Consumer {
161         void apply(Ansi ansi);
162     }
163 
164     private static volatile boolean enabled = true;
165 
166     public static boolean isEnabled() {
167         return org.apache.maven.jline.MessageUtils.isColorEnabled() && enabled;
168     }
169 
170     public static void setEnabled(final boolean flag) {
171         enabled = flag;
172     }
173 
174     public static Ansi ansi() {
175         if (isEnabled()) {
176             return new Ansi();
177         } else {
178             return new NoAnsi();
179         }
180     }
181 
182     public static Ansi ansi(StringBuilder builder) {
183         if (isEnabled()) {
184             return new Ansi(builder);
185         } else {
186             return new NoAnsi(builder);
187         }
188     }
189 
190     public static Ansi ansi(int size) {
191         if (isEnabled()) {
192             return new Ansi(size);
193         } else {
194             return new NoAnsi(size);
195         }
196     }
197 
198     private static class NoAnsi extends Ansi {
199         NoAnsi() {
200             super();
201         }
202 
203         NoAnsi(int size) {
204             super(size);
205         }
206 
207         NoAnsi(StringBuilder builder) {
208             super(builder);
209         }
210 
211         @Override
212         public Ansi fg(Color color) {
213             return this;
214         }
215 
216         @Override
217         public Ansi bg(Color color) {
218             return this;
219         }
220 
221         @Override
222         public Ansi fgBright(Color color) {
223             return this;
224         }
225 
226         @Override
227         public Ansi bgBright(Color color) {
228             return this;
229         }
230 
231         @Override
232         public Ansi fg(int color) {
233             return this;
234         }
235 
236         @Override
237         public Ansi fgRgb(int r, int g, int b) {
238             return this;
239         }
240 
241         @Override
242         public Ansi bg(int color) {
243             return this;
244         }
245 
246         @Override
247         public Ansi bgRgb(int r, int g, int b) {
248             return this;
249         }
250 
251         @Override
252         public Ansi a(Attribute attribute) {
253             return this;
254         }
255 
256         @Override
257         public Ansi cursor(int row, int column) {
258             return this;
259         }
260 
261         @Override
262         public Ansi cursorToColumn(int x) {
263             return this;
264         }
265 
266         @Override
267         public Ansi cursorUp(int y) {
268             return this;
269         }
270 
271         @Override
272         public Ansi cursorRight(int x) {
273             return this;
274         }
275 
276         @Override
277         public Ansi cursorDown(int y) {
278             return this;
279         }
280 
281         @Override
282         public Ansi cursorLeft(int x) {
283             return this;
284         }
285 
286         @Override
287         public Ansi cursorDownLine() {
288             return this;
289         }
290 
291         @Override
292         public Ansi cursorDownLine(final int n) {
293             return this;
294         }
295 
296         @Override
297         public Ansi cursorUpLine() {
298             return this;
299         }
300 
301         @Override
302         public Ansi cursorUpLine(final int n) {
303             return this;
304         }
305 
306         @Override
307         public Ansi eraseScreen() {
308             return this;
309         }
310 
311         @Override
312         public Ansi eraseScreen(Erase kind) {
313             return this;
314         }
315 
316         @Override
317         public Ansi eraseLine() {
318             return this;
319         }
320 
321         @Override
322         public Ansi eraseLine(Erase kind) {
323             return this;
324         }
325 
326         @Override
327         public Ansi scrollUp(int rows) {
328             return this;
329         }
330 
331         @Override
332         public Ansi scrollDown(int rows) {
333             return this;
334         }
335 
336         @Override
337         public Ansi saveCursorPosition() {
338             return this;
339         }
340 
341         @Override
342         @Deprecated
343         public Ansi restorCursorPosition() {
344             return this;
345         }
346 
347         @Override
348         public Ansi restoreCursorPosition() {
349             return this;
350         }
351 
352         @Override
353         public Ansi reset() {
354             return this;
355         }
356     }
357 
358     private final StringBuilder builder;
359     private final ArrayList<Integer> attributeOptions = new ArrayList<>(5);
360 
361     public Ansi() {
362         this(new StringBuilder(80));
363     }
364 
365     public Ansi(Ansi parent) {
366         this(new StringBuilder(parent.builder));
367         attributeOptions.addAll(parent.attributeOptions);
368     }
369 
370     public Ansi(int size) {
371         this(new StringBuilder(size));
372     }
373 
374     public Ansi(StringBuilder builder) {
375         this.builder = builder;
376     }
377 
378     public Ansi fg(Color color) {
379         attributeOptions.add(color.fg());
380         return this;
381     }
382 
383     public Ansi fg(int color) {
384         attributeOptions.add(38);
385         attributeOptions.add(5);
386         attributeOptions.add(color & 0xff);
387         return this;
388     }
389 
390     public Ansi fgRgb(int color) {
391         return fgRgb(color >> 16, color >> 8, color);
392     }
393 
394     public Ansi fgRgb(int r, int g, int b) {
395         attributeOptions.add(38);
396         attributeOptions.add(2);
397         attributeOptions.add(r & 0xff);
398         attributeOptions.add(g & 0xff);
399         attributeOptions.add(b & 0xff);
400         return this;
401     }
402 
403     public Ansi fgBlack() {
404         return this.fg(Color.BLACK);
405     }
406 
407     public Ansi fgBlue() {
408         return this.fg(Color.BLUE);
409     }
410 
411     public Ansi fgCyan() {
412         return this.fg(Color.CYAN);
413     }
414 
415     public Ansi fgDefault() {
416         return this.fg(Color.DEFAULT);
417     }
418 
419     public Ansi fgGreen() {
420         return this.fg(Color.GREEN);
421     }
422 
423     public Ansi fgMagenta() {
424         return this.fg(Color.MAGENTA);
425     }
426 
427     public Ansi fgRed() {
428         return this.fg(Color.RED);
429     }
430 
431     public Ansi fgYellow() {
432         return this.fg(Color.YELLOW);
433     }
434 
435     public Ansi bg(Color color) {
436         attributeOptions.add(color.bg());
437         return this;
438     }
439 
440     public Ansi bg(int color) {
441         attributeOptions.add(48);
442         attributeOptions.add(5);
443         attributeOptions.add(color & 0xff);
444         return this;
445     }
446 
447     public Ansi bgRgb(int color) {
448         return bgRgb(color >> 16, color >> 8, color);
449     }
450 
451     public Ansi bgRgb(int r, int g, int b) {
452         attributeOptions.add(48);
453         attributeOptions.add(2);
454         attributeOptions.add(r & 0xff);
455         attributeOptions.add(g & 0xff);
456         attributeOptions.add(b & 0xff);
457         return this;
458     }
459 
460     public Ansi bgCyan() {
461         return this.bg(Color.CYAN);
462     }
463 
464     public Ansi bgDefault() {
465         return this.bg(Color.DEFAULT);
466     }
467 
468     public Ansi bgGreen() {
469         return this.bg(Color.GREEN);
470     }
471 
472     public Ansi bgMagenta() {
473         return this.bg(Color.MAGENTA);
474     }
475 
476     public Ansi bgRed() {
477         return this.bg(Color.RED);
478     }
479 
480     public Ansi bgYellow() {
481         return this.bg(Color.YELLOW);
482     }
483 
484     public Ansi fgBright(Color color) {
485         attributeOptions.add(color.fgBright());
486         return this;
487     }
488 
489     public Ansi fgBrightBlack() {
490         return this.fgBright(Color.BLACK);
491     }
492 
493     public Ansi fgBrightBlue() {
494         return this.fgBright(Color.BLUE);
495     }
496 
497     public Ansi fgBrightCyan() {
498         return this.fgBright(Color.CYAN);
499     }
500 
501     public Ansi fgBrightDefault() {
502         return this.fgBright(Color.DEFAULT);
503     }
504 
505     public Ansi fgBrightGreen() {
506         return this.fgBright(Color.GREEN);
507     }
508 
509     public Ansi fgBrightMagenta() {
510         return this.fgBright(Color.MAGENTA);
511     }
512 
513     public Ansi fgBrightRed() {
514         return this.fgBright(Color.RED);
515     }
516 
517     public Ansi fgBrightYellow() {
518         return this.fgBright(Color.YELLOW);
519     }
520 
521     public Ansi bgBright(Color color) {
522         attributeOptions.add(color.bgBright());
523         return this;
524     }
525 
526     public Ansi bgBrightCyan() {
527         return this.bgBright(Color.CYAN);
528     }
529 
530     public Ansi bgBrightDefault() {
531         return this.bgBright(Color.DEFAULT);
532     }
533 
534     public Ansi bgBrightGreen() {
535         return this.bgBright(Color.GREEN);
536     }
537 
538     public Ansi bgBrightMagenta() {
539         return this.bgBright(Color.MAGENTA);
540     }
541 
542     public Ansi bgBrightRed() {
543         return this.bgBright(Color.RED);
544     }
545 
546     public Ansi bgBrightYellow() {
547         return this.bgBright(Color.YELLOW);
548     }
549 
550     public Ansi a(Attribute attribute) {
551         attributeOptions.add(attribute.value());
552         return this;
553     }
554 
555     /**
556      * Moves the cursor to row n, column m. The values are 1-based.
557      * Any values less than 1 are mapped to 1.
558      *
559      * @param row    row (1-based) from top
560      * @param column column (1 based) from left
561      * @return this Ansi instance
562      */
563     public Ansi cursor(final int row, final int column) {
564         return appendEscapeSequence('H', Math.max(1, row), Math.max(1, column));
565     }
566 
567     /**
568      * Moves the cursor to column n. The parameter n is 1-based.
569      * If n is less than 1 it is moved to the first column.
570      *
571      * @param x the index (1-based) of the column to move to
572      * @return this Ansi instance
573      */
574     public Ansi cursorToColumn(final int x) {
575         return appendEscapeSequence('G', Math.max(1, x));
576     }
577 
578     /**
579      * Moves the cursor up. If the parameter y is negative it moves the cursor down.
580      *
581      * @param y the number of lines to move up
582      * @return this Ansi instance
583      */
584     public Ansi cursorUp(final int y) {
585         return y > 0 ? appendEscapeSequence('A', y) : y < 0 ? cursorDown(-y) : this;
586     }
587 
588     /**
589      * Moves the cursor down. If the parameter y is negative it moves the cursor up.
590      *
591      * @param y the number of lines to move down
592      * @return this Ansi instance
593      */
594     public Ansi cursorDown(final int y) {
595         return y > 0 ? appendEscapeSequence('B', y) : y < 0 ? cursorUp(-y) : this;
596     }
597 
598     /**
599      * Moves the cursor right. If the parameter x is negative it moves the cursor left.
600      *
601      * @param x the number of characters to move right
602      * @return this Ansi instance
603      */
604     public Ansi cursorRight(final int x) {
605         return x > 0 ? appendEscapeSequence('C', x) : x < 0 ? cursorLeft(-x) : this;
606     }
607 
608     /**
609      * Moves the cursor left. If the parameter x is negative it moves the cursor right.
610      *
611      * @param x the number of characters to move left
612      * @return this Ansi instance
613      */
614     public Ansi cursorLeft(final int x) {
615         return x > 0 ? appendEscapeSequence('D', x) : x < 0 ? cursorRight(-x) : this;
616     }
617 
618     /**
619      * Moves the cursor relative to the current position. The cursor is moved right if x is
620      * positive, left if negative and down if y is positive and up if negative.
621      *
622      * @param x the number of characters to move horizontally
623      * @param y the number of lines to move vertically
624      * @return this Ansi instance
625      * @since 2.2
626      */
627     public Ansi cursorMove(final int x, final int y) {
628         return cursorRight(x).cursorDown(y);
629     }
630 
631     /**
632      * Moves the cursor to the beginning of the line below.
633      *
634      * @return this Ansi instance
635      */
636     public Ansi cursorDownLine() {
637         return appendEscapeSequence('E');
638     }
639 
640     /**
641      * Moves the cursor to the beginning of the n-th line below. If the parameter n is negative it
642      * moves the cursor to the beginning of the n-th line above.
643      *
644      * @param n the number of lines to move the cursor
645      * @return this Ansi instance
646      */
647     public Ansi cursorDownLine(final int n) {
648         return n < 0 ? cursorUpLine(-n) : appendEscapeSequence('E', n);
649     }
650 
651     /**
652      * Moves the cursor to the beginning of the line above.
653      *
654      * @return this Ansi instance
655      */
656     public Ansi cursorUpLine() {
657         return appendEscapeSequence('F');
658     }
659 
660     /**
661      * Moves the cursor to the beginning of the n-th line above. If the parameter n is negative it
662      * moves the cursor to the beginning of the n-th line below.
663      *
664      * @param n the number of lines to move the cursor
665      * @return this Ansi instance
666      */
667     public Ansi cursorUpLine(final int n) {
668         return n < 0 ? cursorDownLine(-n) : appendEscapeSequence('F', n);
669     }
670 
671     public Ansi eraseScreen() {
672         return appendEscapeSequence('J', Erase.ALL.value());
673     }
674 
675     public Ansi eraseScreen(final Erase kind) {
676         return appendEscapeSequence('J', kind.value());
677     }
678 
679     public Ansi eraseLine() {
680         return appendEscapeSequence('K');
681     }
682 
683     public Ansi eraseLine(final Erase kind) {
684         return appendEscapeSequence('K', kind.value());
685     }
686 
687     public Ansi scrollUp(final int rows) {
688         if (rows == Integer.MIN_VALUE) {
689             return scrollDown(Integer.MAX_VALUE);
690         }
691         return rows > 0 ? appendEscapeSequence('S', rows) : rows < 0 ? scrollDown(-rows) : this;
692     }
693 
694     public Ansi scrollDown(final int rows) {
695         if (rows == Integer.MIN_VALUE) {
696             return scrollUp(Integer.MAX_VALUE);
697         }
698         return rows > 0 ? appendEscapeSequence('T', rows) : rows < 0 ? scrollUp(-rows) : this;
699     }
700 
701     @Deprecated
702     public Ansi restorCursorPosition() {
703         return restoreCursorPosition();
704     }
705 
706     public Ansi saveCursorPosition() {
707         saveCursorPositionSCO();
708         return saveCursorPositionDEC();
709     }
710 
711     // SCO command
712     public Ansi saveCursorPositionSCO() {
713         return appendEscapeSequence('s');
714     }
715 
716     // DEC command
717     public Ansi saveCursorPositionDEC() {
718         builder.append(FIRST_ESC_CHAR);
719         builder.append('7');
720         return this;
721     }
722 
723     public Ansi restoreCursorPosition() {
724         restoreCursorPositionSCO();
725         return restoreCursorPositionDEC();
726     }
727 
728     // SCO command
729     public Ansi restoreCursorPositionSCO() {
730         return appendEscapeSequence('u');
731     }
732 
733     // DEC command
734     public Ansi restoreCursorPositionDEC() {
735         builder.append(FIRST_ESC_CHAR);
736         builder.append('8');
737         return this;
738     }
739 
740     public Ansi reset() {
741         return a(Attribute.RESET);
742     }
743 
744     public Ansi bold() {
745         return a(Attribute.INTENSITY_BOLD);
746     }
747 
748     public Ansi boldOff() {
749         return a(Attribute.INTENSITY_BOLD_OFF);
750     }
751 
752     public Ansi a(String value) {
753         flushAttributes();
754         builder.append(value);
755         return this;
756     }
757 
758     public Ansi a(boolean value) {
759         flushAttributes();
760         builder.append(value);
761         return this;
762     }
763 
764     public Ansi a(char value) {
765         flushAttributes();
766         builder.append(value);
767         return this;
768     }
769 
770     public Ansi a(char[] value, int offset, int len) {
771         flushAttributes();
772         builder.append(value, offset, len);
773         return this;
774     }
775 
776     public Ansi a(char[] value) {
777         flushAttributes();
778         builder.append(value);
779         return this;
780     }
781 
782     public Ansi a(CharSequence value, int start, int end) {
783         flushAttributes();
784         builder.append(value, start, end);
785         return this;
786     }
787 
788     public Ansi a(CharSequence value) {
789         flushAttributes();
790         builder.append(value);
791         return this;
792     }
793 
794     public Ansi a(double value) {
795         flushAttributes();
796         builder.append(value);
797         return this;
798     }
799 
800     public Ansi a(float value) {
801         flushAttributes();
802         builder.append(value);
803         return this;
804     }
805 
806     public Ansi a(int value) {
807         flushAttributes();
808         builder.append(value);
809         return this;
810     }
811 
812     public Ansi a(long value) {
813         flushAttributes();
814         builder.append(value);
815         return this;
816     }
817 
818     public Ansi a(Object value) {
819         flushAttributes();
820         builder.append(value);
821         return this;
822     }
823 
824     public Ansi a(StringBuffer value) {
825         flushAttributes();
826         builder.append(value);
827         return this;
828     }
829 
830     public Ansi newline() {
831         flushAttributes();
832         builder.append(System.lineSeparator());
833         return this;
834     }
835 
836     public Ansi format(String pattern, Object... args) {
837         flushAttributes();
838         builder.append(String.format(pattern, args));
839         return this;
840     }
841 
842     /**
843      * Applies another function to this Ansi instance.
844      *
845      * @param fun the function to apply
846      * @return this Ansi instance
847      * @since 2.2
848      */
849     public Ansi apply(Consumer fun) {
850         fun.apply(this);
851         return this;
852     }
853 
854     /**
855      * Renders Jansi markup, e.g. {@code @|bold,red text|@}, into ANSI escape sequences.
856      *
857      * @param text text
858      * @return this
859      * @since 2.2
860      */
861     public Ansi render(final String text) {
862         int i = 0;
863         int begin;
864         while ((begin = text.indexOf("@|", i)) >= 0) {
865             int space = text.indexOf(' ', begin);
866             int end = text.indexOf("|@", begin);
867             if (space < 0 || end < 0 || space > end) {
868                 break;
869             }
870             a(text.substring(i, begin));
871             for (String code : text.substring(begin + 2, space).split(",")) {
872                 renderCode(code.trim().toUpperCase(java.util.Locale.ROOT));
873             }
874             a(text.substring(space + 1, end)).reset();
875             i = end + 2;
876         }
877         return a(text.substring(i));
878     }
879 
880     private void renderCode(String code) {
881         boolean bg = code.startsWith("BG_");
882         String name = bg || code.startsWith("FG_") ? code.substring(3) : code;
883         for (Color color : Color.values()) {
884             if (color.name().equals(name)) {
885                 if (bg) {
886                     bg(color);
887                 } else {
888                     fg(color);
889                 }
890                 return;
891             }
892         }
893         for (Attribute attribute : Attribute.values()) {
894             if (attribute.name().equals(code) || attribute.name().equals(code + "_ON")) {
895                 a(attribute);
896                 return;
897             }
898         }
899         if (code.equals("BOLD")) {
900             bold();
901         } else if (code.equals("FAINT")) {
902             a(Attribute.INTENSITY_FAINT);
903         }
904     }
905 
906     /**
907      * String formats and renders the supplied arguments.
908      *
909      * @param text format
910      * @param args arguments
911      * @return this
912      * @since 2.2
913      */
914     public Ansi render(final String text, Object... args) {
915         return render(String.format(text, args));
916     }
917 
918     @Override
919     public String toString() {
920         flushAttributes();
921         return builder.toString();
922     }
923 
924     ///////////////////////////////////////////////////////////////////
925     // Private Helper Methods
926     ///////////////////////////////////////////////////////////////////
927 
928     private Ansi appendEscapeSequence(char command) {
929         flushAttributes();
930         builder.append(FIRST_ESC_CHAR);
931         builder.append(SECOND_ESC_CHAR);
932         builder.append(command);
933         return this;
934     }
935 
936     private Ansi appendEscapeSequence(char command, int option) {
937         flushAttributes();
938         builder.append(FIRST_ESC_CHAR);
939         builder.append(SECOND_ESC_CHAR);
940         builder.append(option);
941         builder.append(command);
942         return this;
943     }
944 
945     private Ansi appendEscapeSequence(char command, Object... options) {
946         flushAttributes();
947         return doAppendEscapeSequence(command, options);
948     }
949 
950     private void flushAttributes() {
951         if (attributeOptions.isEmpty()) {
952             return;
953         }
954         if (attributeOptions.size() == 1 && attributeOptions.get(0) == 0) {
955             builder.append(FIRST_ESC_CHAR);
956             builder.append(SECOND_ESC_CHAR);
957             builder.append('m');
958         } else {
959             doAppendEscapeSequence('m', attributeOptions.toArray());
960         }
961         attributeOptions.clear();
962     }
963 
964     private Ansi doAppendEscapeSequence(char command, Object... options) {
965         builder.append(FIRST_ESC_CHAR);
966         builder.append(SECOND_ESC_CHAR);
967         int size = options.length;
968         for (int i = 0; i < size; i++) {
969             if (i != 0) {
970                 builder.append(';');
971             }
972             if (options[i] != null) {
973                 builder.append(options[i]);
974             }
975         }
976         builder.append(command);
977         return this;
978     }
979 
980     @Override
981     public Ansi append(CharSequence csq) {
982         builder.append(csq);
983         return this;
984     }
985 
986     @Override
987     public Ansi append(CharSequence csq, int start, int end) {
988         builder.append(csq, start, end);
989         return this;
990     }
991 
992     @Override
993     public Ansi append(char c) {
994         builder.append(c);
995         return this;
996     }
997 }