1 /** 2 * Consoleur: a package for interaction with character-oriented terminal emulators 3 * 4 * Copyright: Maxim Freck, 2017. 5 * Authors: Maxim Freck <maxim@freck.pp.ru> 6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 7 */ 8 module consoleur.commands.posix; 9 version(Posix) { 10 11 /** 12 * Invocable commands 13 */ 14 enum TermCommand: size_t { 15 clearScreen = 0, 16 17 keypadLocal, 18 keypadXmit, 19 20 enterCa, 21 exitCa, 22 23 cursorHide, 24 cursorShow, 25 26 saveCursorPosition, 27 restoreCursorPosition 28 } 29 30 private string[TermCommand.max+1] commands; 31 32 /******* 33 * Invokes special terminal command if available 34 * 35 * Params: 36 * cmd = Terminal command 37 * 38 * Return: true if a command is available, false otherwise 39 */ 40 pragma(inline): 41 bool invokeCommand(TermCommand cmd) @safe 42 { 43 import std.stdio: write; 44 45 if (cmd >= commands.length || commands[cmd].length == 0) return false; 46 47 write(commands[cmd]); 48 return true; 49 } 50 51 private shared static this() @safe 52 { 53 import consoleur.terminfo: CapString, Terminfo, TerminfoStatus; 54 55 auto termInfo = Terminfo.getActual(); 56 bool loaded = false; 57 58 if (termInfo.getStatus() >= TerminfoStatus.stringsLoaded) loaded = true; 59 60 void setCommand(TermCommand cmd, CapString cap, string dflt) { 61 if (!loaded) { 62 commands[cmd] = dflt; 63 return; 64 } 65 66 immutable str = termInfo.get(cap); 67 commands[cmd] = (str.length == 0) ? dflt : str; 68 } 69 70 setCommand(TermCommand.clearScreen, CapString.clearScreen, "\x1b[2J"); 71 72 setCommand(TermCommand.keypadLocal, CapString.keypadLocal, "\x1b[?1l\x1b>"); 73 setCommand(TermCommand.keypadXmit, CapString.keypadXmit, "\x1b[?1h\x1b="); 74 75 setCommand(TermCommand.enterCa, CapString.enterCaMode, "\x1b[?1049h"); 76 setCommand(TermCommand.exitCa, CapString.exitCaMode, "\x1b[?1049l"); 77 78 setCommand(TermCommand.cursorHide, CapString.cursorInvisible, "\x1b[?25l"); 79 setCommand(TermCommand.cursorShow, CapString.cursorNormal, "\x1b[?25h"); 80 81 setCommand(TermCommand.cursorHide, CapString.cursorInvisible, "\x1b[?25l"); 82 setCommand(TermCommand.cursorShow, CapString.cursorNormal, "\x1b[?25h"); 83 84 setCommand(TermCommand.saveCursorPosition, CapString.saveCursor, "\x1b[s"); 85 setCommand(TermCommand.restoreCursorPosition, CapString.restoreCursor, "\x1b[u"); 86 } 87 88 }