1 /* This file is part of the Amalthea library. 2 * 3 * Copyright (C) 2018-2020 Eugene 'Vindex' Stulin 4 * 5 * Distributed under the Boost Software License 1.0 or (at your option) 6 * the GNU Lesser General Public License 3.0 or later. 7 */ 8 9 module amalthea.terminal; 10 11 public import amalthea.libcore; 12 13 import std.file, std.stdio; 14 15 16 /******************************************************************************* 17 * Colors of foreground. 18 */ 19 enum FGColor { 20 black = "\033[30m", dark_gray = "\033[90m", 21 red = "\033[31m", light_red = "\033[91m", 22 green = "\033[32m", light_green = "\033[92m", 23 yellow = "\033[33m", light_yellow = "\033[93m", 24 blue = "\033[34m", light_blue = "\033[94m", 25 magenta = "\033[35m", light_magenta = "\033[95m", 26 cyan = "\033[36m", light_cyan = "\033[96m", 27 light_gray = "\033[37m", white = "\033[97m", 28 29 reset = "\033[0m", 30 empty = "" 31 } 32 33 34 /******************************************************************************* 35 * Output of color text. 36 */ 37 void cwrite(S...)(S args) { 38 File f = stdout; 39 static if (is(typeof(args[0]) == File)) { 40 f = args[0]; 41 } 42 string text; 43 FGColor lastColorMark; 44 foreach(i, arg; args) { 45 static if (is(typeof(arg) == FGColor)) { 46 lastColorMark = arg; 47 text ~= cast(string)(arg); 48 } else static if (!is(typeof(arg) == File)) { 49 text ~= to!string(arg); 50 } 51 } 52 f.write(text); 53 if (lastColorMark != FGColor.empty) 54 f.write(cast(string)FGColor.reset); 55 f.flush(); 56 } 57 58 59 /******************************************************************************* 60 * Output of color text with line ending. 61 */ 62 void cwriteln(S...)(S args) { 63 cwrite(args, "\n"); 64 } 65 66 67 private extern(C) int isatty(int); 68 69 /******************************************************************************* 70 * Wrapper for `isatty` (see: `man isatty`). 71 * Tests whether a file descriptor refers to a terminal. 72 * Params: 73 * f = File object. Usually, it is equal to stdout, stderr or stdin. 74 */ 75 bool isTTY(File f = stdout) { 76 return cast(bool)isatty(f.fileno); 77 } 78 alias isTerminal = isTTY; 79 80 81 /******************************************************************************* 82 * This function clears current terminal screen. 83 */ 84 void clearScreen() { 85 write("\033[H\033[2J"); 86 } 87