| 1 |
# ------------------------------------------------------------------------------ |
| 2 |
# Interface and some basic functionality for all printers. |
| 3 |
# ------------------------------------------------------------------------------ |
| 4 |
class Printer: |
| 5 |
var _format_enabled = true |
| 6 |
var _disabled = false |
| 7 |
var _printer_name = 'NOT SET' |
| 8 |
var _show_name = false # used for debugging, set manually |
| 9 |
|
| 10 |
func get_format_enabled(): |
| 11 |
return _format_enabled |
| 12 |
|
| 13 |
func set_format_enabled(format_enabled): |
| 14 |
_format_enabled = format_enabled |
| 15 |
|
| 16 |
func send(text, fmt=null): |
| 17 |
if(_disabled): |
| 18 |
return |
| 19 |
|
| 20 |
var formatted = text |
| 21 |
if(fmt != null and _format_enabled): |
| 22 |
formatted = format_text(text, fmt) |
| 23 |
|
| 24 |
if(_show_name): |
| 25 |
formatted = str('(', _printer_name, ')') + formatted |
| 26 |
|
| 27 |
_output(formatted) |
| 28 |
|
| 29 |
func get_disabled(): |
| 30 |
return _disabled |
| 31 |
|
| 32 |
func set_disabled(disabled): |
| 33 |
_disabled = disabled |
| 34 |
|
| 35 |
# -------------------- |
| 36 |
# Virtual Methods (some have some default behavior) |
| 37 |
# -------------------- |
| 38 |
func _output(text): |
| 39 |
pass |
| 40 |
|
| 41 |
func format_text(text, fmt): |
| 42 |
return text |
| 43 |
|
| 44 |
# ------------------------------------------------------------------------------ |
| 45 |
# Responsible for sending text to a GUT gui. |
| 46 |
# ------------------------------------------------------------------------------ |
| 47 |
class GutGuiPrinter: |
| 48 |
extends Printer |
| 49 |
var _gut = null |
| 50 |
|
| 51 |
var _colors = { |
| 52 |
red = Color.red, |
| 53 |
yellow = Color.yellow, |
| 54 |
green = Color.green |
| 55 |
} |
| 56 |
|
| 57 |
func _init(): |
| 58 |
_printer_name = 'gui' |
| 59 |
|
| 60 |
func _wrap_with_tag(text, tag): |
| 61 |
return str('[', tag, ']', text, '[/', tag, ']') |
| 62 |
|
| 63 |
func _color_text(text, c_word): |
| 64 |
return '[color=' + c_word + ']' + text + '[/color]' |
| 65 |
|
| 66 |
func format_text(text, fmt): |
| 67 |
var box = _gut.get_gui().get_text_box() |
| 68 |
|
| 69 |
if(fmt == 'bold'): |
| 70 |
box.push_bold() |
| 71 |
elif(fmt == 'underline'): |
| 72 |
box.push_underline() |
| 73 |
elif(_colors.has(fmt)): |
| 74 |
box.push_color(_colors[fmt]) |
| 75 |
else: |
| 76 |
# just pushing something to pop. |
| 77 |
box.push_normal() |
| 78 |
|
| 79 |
box.add_text(text) |
| 80 |
box.pop() |
| 81 |
|
| 82 |
return '' |
| 83 |
|
| 84 |
func _output(text): |
| 85 |
_gut.get_gui().get_text_box().add_text(text) |
| 86 |
|
| 87 |
func get_gut(): |
| 88 |
return _gut |
| 89 |
|
| 90 |
func set_gut(gut): |
| 91 |
_gut = gut |
| 92 |
|
| 93 |
# This can be very very slow when the box has a lot of text. |
| 94 |
func clear_line(): |
| 95 |
var box = _gut.get_gui().get_text_box() |
| 96 |
box.remove_line(box.get_line_count() - 1) |
| 97 |
box.update() |
| 98 |
|
| 99 |
# ------------------------------------------------------------------------------ |
| 100 |
# This AND TerminalPrinter should not be enabled at the same time since it will |
| 101 |
# result in duplicate output. printraw does not print to the console so i had |
| 102 |
# to make another one. |
| 103 |
# ------------------------------------------------------------------------------ |
| 104 |
class ConsolePrinter: |
| 105 |
extends Printer |
| 106 |
var _buffer = '' |
| 107 |
|
| 108 |
func _init(): |
| 109 |
_printer_name = 'console' |
| 110 |
|
| 111 |
# suppresses output until it encounters a newline to keep things |
| 112 |
# inline as much as possible. |
| 113 |
func _output(text): |
| 114 |
if(text.ends_with("\n")): |
| 115 |
print(_buffer + text.left(text.length() -1)) |
| 116 |
_buffer = '' |
| 117 |
else: |
| 118 |
_buffer += text |
| 119 |
|
| 120 |
# ------------------------------------------------------------------------------ |
| 121 |
# Prints text to terminal, formats some words. |
| 122 |
# ------------------------------------------------------------------------------ |
| 123 |
class TerminalPrinter: |
| 124 |
extends Printer |
| 125 |
|
| 126 |
var escape = PoolByteArray([0x1b]).get_string_from_ascii() |
| 127 |
var cmd_colors = { |
| 128 |
red = escape + '[31m', |
| 129 |
yellow = escape + '[33m', |
| 130 |
green = escape + '[32m', |
| 131 |
|
| 132 |
underline = escape + '[4m', |
| 133 |
bold = escape + '[1m', |
| 134 |
|
| 135 |
default = escape + '[0m', |
| 136 |
|
| 137 |
clear_line = escape + '[2K' |
| 138 |
} |
| 139 |
|
| 140 |
func _init(): |
| 141 |
_printer_name = 'terminal' |
| 142 |
|
| 143 |
func _output(text): |
| 144 |
# Note, printraw does not print to the console. |
| 145 |
printraw(text) |
| 146 |
|
| 147 |
func format_text(text, fmt): |
| 148 |
return cmd_colors[fmt] + text + cmd_colors.default |
| 149 |
|
| 150 |
func clear_line(): |
| 151 |
send(cmd_colors.clear_line) |
| 152 |
|
| 153 |
func back(n): |
| 154 |
send(escape + str('[', n, 'D')) |
| 155 |
|
| 156 |
func forward(n): |
| 157 |
send(escape + str('[', n, 'C')) |