1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package optkl.textmodel.terminal;
 26 
 27 import optkl.textmodel.tokens.Token;
 28 
 29 import java.io.PrintStream;
 30 import java.util.Arrays;
 31 import java.util.function.Consumer;
 32 import java.util.function.Function;
 33 
 34 public interface ANSI<T extends ANSI<T>> extends  Function<String, T> {
 35     T apply(String o);
 36     default T apply(char ch){
 37         return apply(""+ch);
 38     }
 39     default T apply(Token t) {
 40         return apply(t.asString());
 41     }
 42 
 43     default T self() {
 44         return (T) this;
 45     }
 46 
 47     default void skip(){
 48     }
 49 
 50     int BLACK = 30;
 51     int RED = 31;
 52     int GREEN = 32;
 53     int YELLOW = 33;
 54     int BLUE = 34;
 55     int PURPLE = 35;
 56     int CYAN = 36;
 57     int WHITE = 37;
 58 
 59     default T esc() {
 60         return apply("\033");
 61     }
 62 
 63     default T color(int c, Consumer<T> cc) {
 64         csi().ints(c).apply("m");
 65         cc.accept(self());
 66         return reset();
 67     }
 68 
 69     default T pass(Consumer<T> cc) {
 70         return color(ANSI.GREEN, cc);
 71     }
 72     default T pass(String string) {
 73         return pass(_->apply(string));
 74     }
 75     default T warn(Consumer<T> cc) {
 76         return color(ANSI.YELLOW, cc);
 77     }
 78     default T warn(String string) {
 79         return warn( _->apply(string));
 80     }
 81     default T fail(Consumer<T> cc) {
 82         return color(ANSI.RED, cc);
 83     }
 84     default T fail(String string) {
 85         return fail(_->apply(string));
 86     }
 87 
 88     default T color(int c1, int c2, Consumer<T> cc) {
 89         csi().ints(c1, c2).apply("m");
 90         cc.accept(self());
 91 
 92         return reset();
 93     }
 94     default T fg(int color, Consumer<T> cc) {
 95         return color(0, color, cc);
 96     }
 97     default T fg(int color, Token token) {
 98         return fg(color, $->$.apply(token));
 99     }
100 
101 
102     default T bold(int color, Consumer<T> cc) {
103         return color(1, color, cc);
104     }
105 
106 
107     default T boldAndBright(int color, Consumer<T> cc) {
108         return color(0, color + 60, cc);
109     }
110 
111 
112 
113     default T bright(int color, Consumer<T> cc) {
114         return color(1, color + 60, cc);
115     }
116 
117 
118 
119     default T bg(int color,Consumer<T> cc) {
120         return color(color + 10, cc);
121     }
122     default T bg(int color,Token token) {
123         return bg(color, $->$.apply(token));
124     }
125 
126     default T csi() {
127         return esc().apply("[");
128     }
129 
130     default T home(){
131         return csi().ints(0,0).apply("H");
132     }
133 
134     default T csiQuery() {
135         return csi().apply("?");
136     }
137 
138     default T ints(int... n) {
139         apply(String.valueOf(n[0]));
140         for (int i = 1; i < n.length; i++) {
141             apply(";" + n[i]);
142         }
143         return self();
144     }
145 
146 
147     default T inv() {
148         return csi().ints(7).apply("m");
149     }
150 
151     default T reset() {
152         return csi().ints(0).apply("m");
153     }
154 
155     default T inv(Consumer<T> c) {
156         inv();
157         c.accept(self());
158         return reset();
159     }
160 
161 
162 
163     record Adaptor(Consumer<String> consumer) implements ANSI<Adaptor> {
164         @Override
165         public Adaptor apply(String s) {
166             consumer.accept(s);
167             return self();
168         }
169     }
170 
171     default T repeat(String s, int count) {
172         return apply(s.repeat(count));
173     }
174 
175     default T fill(int cols, String s) {
176         return apply(s).repeat(" ", Math.max(0, cols - s.length()));
177     }
178 
179     class IMPL implements ANSI<IMPL> {
180         private final PrintStream printStream;
181 
182         @Override
183         public IMPL apply(String s) {
184             printStream.append(s);
185             return self();
186         }
187 
188         IMPL(PrintStream printStream) {
189             this.printStream = printStream;
190         }
191     }
192 
193     static IMPL of(PrintStream printStream) {
194         return new IMPL(printStream);
195     }
196 
197 
198      class DotImage {
199         final private ANSI<?> ansi;
200         final public int width;
201         final public int height;
202         final private byte[] bytes;
203         int charWidth;
204         int charHeight;
205         final private  char[] chars;
206         public DotImage(ANSI<?> ansi, int width, int height){
207             this.ansi = ansi;
208             this.width = width;
209             this.height = height;
210             this.bytes = new byte[width*height];
211             this.charWidth = width/2;
212             this.charHeight = height/4;
213             this.chars = new char[charWidth*charHeight];
214         }
215         public void set(int x, int y){
216             bytes[(y*width)+x]=1;//0xff;
217         }
218         void reset(int x, int y){
219             bytes[(y*width)+x]=0;
220         }
221         int i(int x, int y){
222             byte b = bytes[(y*width)+x];
223             return (int)(b<0?b+256:b);
224         }
225         /**
226          See the unicode mapping table here
227          https://images.app.goo.gl/ntxis4mKzn7GmrGb7
228          */
229         char dotchar(int bytebits){
230            // int mapped = (bytebits&0x07)|(bytebits&0x70)>>1|(bytebits&0x08)<<3|(bytebits&0x80);
231             //char brail = (char)(0x2800+(bytebits&0x07)|(bytebits&0x70)>>1|(bytebits&0x08)<<3|(bytebits&0x80));
232             return  (char)(0x2800+(bytebits&0x07)|(bytebits&0x70)>>1|(bytebits&0x08)<<3|(bytebits&0x80));
233         }
234         public DotImage home(){
235 
236             ansi.home();
237             return this;
238         }
239         public DotImage delay(int ms){
240             try{ Thread.sleep(ms); }catch(Throwable t){ }
241             return this;
242         }
243 
244         public DotImage clean(){
245             Arrays.fill(bytes,(byte)0);
246             Arrays.fill(chars,(char)' ');
247             return this;
248         }
249         public DotImage map(){
250             for (int cx = 0; cx<charWidth; cx++){
251                 for (int cy = 0; cy<charHeight; cy++){
252                     int bytebits=0;
253                     for (int dx=0;dx<2;dx++){
254                         for (int dy=0;dy<4;dy++){
255                             bytebits|=i(cx*2+dx,cy*4+dy)<<(dx*4+dy);
256                         }
257                     }
258                     chars[cy*charWidth+cx]= dotchar(bytebits);
259                 }
260             }
261             return this;
262         }
263 
264         public DotImage write(){
265             ansi.apply("+");
266             for (int i=0;i<charWidth; i++){
267                 ansi.apply("-");
268             }
269             ansi.apply("+\n|");
270             for (int i=0;i<chars.length; i++){
271                 if (i>0 && (i%charWidth)==0){
272                     ansi.apply("|\n|");
273                 }
274                 ansi.apply(Character.toString(chars[i]));
275             }
276             ansi.apply("|\n+");
277             for (int i=0;i<charWidth; i++){
278                 ansi.apply("-");
279             }
280             ansi.apply("+\n");
281             return this;
282         }
283     }
284     default DotImage img(int width, int height){
285         return new DotImage(this, width,height);
286     }
287 
288 }