1 /* 2 * hunt-console eases the creation of beautiful and testable command line interfaces. 3 * 4 * Copyright (C) 2018-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.console.formatter.OutputFormatterStyleStack; 13 14 import hunt.console.error.InvalidArgumentException; 15 import hunt.console.formatter.OutputFormatterStyle; 16 17 import hunt.collection.ArrayList; 18 import hunt.collection.List; 19 import hunt.console.formatter.DefaultOutputFormatterStyle; 20 import hunt.logging; 21 import std.string; 22 23 class OutputFormatterStyleStack 24 { 25 List!(OutputFormatterStyle) styles; 26 27 OutputFormatterStyle emptyStyle; 28 29 public this() 30 { 31 this(new DefaultOutputFormatterStyle()); 32 } 33 34 public this(OutputFormatterStyle emptyStyle) 35 { 36 this.emptyStyle = emptyStyle; 37 reset(); 38 } 39 40 public void reset() 41 { 42 styles = new ArrayList!(OutputFormatterStyle)(); 43 } 44 45 public void push(OutputFormatterStyle style) 46 { 47 // logDebug("push : ",(cast(DefaultOutputFormatterStyle)style).toHash); 48 styles.add(style); 49 } 50 51 public OutputFormatterStyle pop() 52 { 53 return pop(null); 54 } 55 56 public OutputFormatterStyle pop(OutputFormatterStyle style) 57 { 58 // logDebug("styles.size() : ",styles.size()); 59 if (styles.size() == 0) { 60 return emptyStyle; 61 } 62 63 if (style is null) { 64 return styles.removeAt(styles.size() - 1); 65 } 66 // logDebug("pop : ",(cast(DefaultOutputFormatterStyle)style).toHash); 67 68 69 OutputFormatterStyle stackedStyle; 70 for (int i = (styles.size() - 1); i >= 0; i--) { 71 stackedStyle = styles.get(i); 72 // logDebug("------",style.apply("").length," -----",stackedStyle.apply("").length," cmp : ",style.apply("") == stackedStyle.apply("")); 73 if (style.apply("").strip == (stackedStyle.apply("").strip)) { 74 auto substyles = new ArrayList!(OutputFormatterStyle)(); 75 for(int j = 0 ;j < i;j++) 76 { 77 substyles.add(styles.get(j)); 78 } 79 styles = substyles; 80 return stackedStyle; 81 } 82 } 83 84 throw new InvalidArgumentException("Incorrectly nested style tag found."); 85 } 86 87 public OutputFormatterStyle getCurrent() 88 { 89 if (styles.size() == 0) { 90 return emptyStyle; 91 } 92 93 return styles.get(styles.size() - 1); 94 } 95 96 public OutputFormatterStyle getEmptyStyle() 97 { 98 return emptyStyle; 99 } 100 101 public void setEmptyStyle(OutputFormatterStyle emptyStyle) 102 { 103 this.emptyStyle = emptyStyle; 104 } 105 }