2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Interface and some basic functionality for all printers.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Printer:
|
|
|
|
var _format_enabled = true
|
|
|
|
var _disabled = false
|
2023-01-20 23:34:39 +01:00
|
|
|
var _printer_name = "NOT SET"
|
|
|
|
var _show_name = false # used for debugging, set manually
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func get_format_enabled():
|
|
|
|
return _format_enabled
|
|
|
|
|
|
|
|
func set_format_enabled(format_enabled):
|
|
|
|
_format_enabled = format_enabled
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
func send(text, fmt = null):
|
|
|
|
if _disabled:
|
2022-06-02 04:13:43 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
var formatted = text
|
2023-01-20 23:34:39 +01:00
|
|
|
if fmt != null and _format_enabled:
|
2022-06-02 04:13:43 +02:00
|
|
|
formatted = format_text(text, fmt)
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if _show_name:
|
|
|
|
formatted = str("(", _printer_name, ")") + formatted
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
_output(formatted)
|
|
|
|
|
|
|
|
func get_disabled():
|
|
|
|
return _disabled
|
|
|
|
|
|
|
|
func set_disabled(disabled):
|
|
|
|
_disabled = disabled
|
|
|
|
|
|
|
|
# --------------------
|
|
|
|
# Virtual Methods (some have some default behavior)
|
|
|
|
# --------------------
|
|
|
|
func _output(text):
|
|
|
|
pass
|
|
|
|
|
|
|
|
func format_text(text, fmt):
|
|
|
|
return text
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Responsible for sending text to a GUT gui.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class GutGuiPrinter:
|
|
|
|
extends Printer
|
2023-01-07 20:26:17 +01:00
|
|
|
var _textbox = null
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
var _colors = {red = Color.RED, yellow = Color.YELLOW, green = Color.GREEN}
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _init():
|
2023-01-20 23:34:39 +01:00
|
|
|
_printer_name = "gui"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _wrap_with_tag(text, tag):
|
2023-01-20 23:34:39 +01:00
|
|
|
return str("[", tag, "]", text, "[/", tag, "]")
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _color_text(text, c_word):
|
2023-01-20 23:34:39 +01:00
|
|
|
return "[color=" + c_word + "]" + text + "[/color]"
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
# Remember, we have to use push and pop because the output from the tests
|
|
|
|
# can contain [] in it which can mess up the formatting. There is no way
|
|
|
|
# as of 3.4 that you can get the bbcode out of RTL when using push and pop.
|
|
|
|
#
|
|
|
|
# The only way we could get around this is by adding in non-printable
|
|
|
|
# whitespace after each "[" that is in the text. Then we could maybe do
|
|
|
|
# this another way and still be able to get the bbcode out, or generate it
|
|
|
|
# at the same time in a buffer (like we tried that one time).
|
|
|
|
#
|
|
|
|
# Since RTL doesn't have good search and selection methods, and those are
|
|
|
|
# really handy in the editor, it isn't worth making bbcode that can be used
|
|
|
|
# there as well.
|
|
|
|
#
|
|
|
|
# You'll try to get it so the colors can be the same in the editor as they
|
|
|
|
# are in the output. Good luck, and I hope I typed enough to not go too
|
|
|
|
# far that rabbit hole before finding out it's not worth it.
|
2022-06-02 04:13:43 +02:00
|
|
|
func format_text(text, fmt):
|
2023-01-20 23:34:39 +01:00
|
|
|
if _textbox == null:
|
2023-01-07 20:26:17 +01:00
|
|
|
return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if fmt == "bold":
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.push_bold()
|
2023-01-20 23:34:39 +01:00
|
|
|
elif fmt == "underline":
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.push_underline()
|
2023-01-20 23:34:39 +01:00
|
|
|
elif _colors.has(fmt):
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.push_color(_colors[fmt])
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
|
|
|
# just pushing something to pop.
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.push_normal()
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.add_text(text)
|
|
|
|
_textbox.pop()
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
return ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _output(text):
|
2023-01-20 23:34:39 +01:00
|
|
|
if _textbox == null:
|
2023-01-07 20:26:17 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
_textbox.add_text(text)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func get_textbox():
|
|
|
|
return _textbox
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func set_textbox(textbox):
|
|
|
|
_textbox = textbox
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# This can be very very slow when the box has a lot of text.
|
|
|
|
func clear_line():
|
2023-01-07 20:26:17 +01:00
|
|
|
_textbox.remove_line(_textbox.get_line_count() - 1)
|
|
|
|
_textbox.queue_redraw()
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func get_bbcode():
|
|
|
|
return _textbox.text
|
|
|
|
|
|
|
|
func get_disabled():
|
|
|
|
return _disabled and _textbox != null
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# This AND TerminalPrinter should not be enabled at the same time since it will
|
|
|
|
# result in duplicate output. printraw does not print to the console so i had
|
|
|
|
# to make another one.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class ConsolePrinter:
|
|
|
|
extends Printer
|
2023-01-20 23:34:39 +01:00
|
|
|
var _buffer = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _init():
|
2023-01-20 23:34:39 +01:00
|
|
|
_printer_name = "console"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# suppresses output until it encounters a newline to keep things
|
|
|
|
# inline as much as possible.
|
|
|
|
func _output(text):
|
2023-01-20 23:34:39 +01:00
|
|
|
if text.ends_with("\n"):
|
|
|
|
print(_buffer + text.left(text.length() - 1))
|
|
|
|
_buffer = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
|
|
|
_buffer += text
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Prints text to terminal, formats some words.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class TerminalPrinter:
|
|
|
|
extends Printer
|
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
var escape = PackedByteArray([0x1b]).get_string_from_ascii()
|
2023-01-20 23:34:39 +01:00
|
|
|
var cmd_colors = {
|
|
|
|
red = escape + "[31m",
|
|
|
|
yellow = escape + "[33m",
|
|
|
|
green = escape + "[32m",
|
|
|
|
underline = escape + "[4m",
|
|
|
|
bold = escape + "[1m",
|
|
|
|
default = escape + "[0m",
|
|
|
|
clear_line = escape + "[2K"
|
2022-06-02 04:13:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func _init():
|
2023-01-20 23:34:39 +01:00
|
|
|
_printer_name = "terminal"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _output(text):
|
|
|
|
# Note, printraw does not print to the console.
|
|
|
|
printraw(text)
|
|
|
|
|
|
|
|
func format_text(text, fmt):
|
|
|
|
return cmd_colors[fmt] + text + cmd_colors.default
|
|
|
|
|
|
|
|
func clear_line():
|
|
|
|
send(cmd_colors.clear_line)
|
|
|
|
|
|
|
|
func back(n):
|
2023-01-20 23:34:39 +01:00
|
|
|
send(escape + str("[", n, "D"))
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func forward(n):
|
2023-01-20 23:34:39 +01:00
|
|
|
send(escape + str("[", n, "C"))
|