From 6cd5facb982a4e69ead7e1947dbf7c2cf326c264 Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Mon, 15 Aug 2022 10:35:01 +1200 Subject: [PATCH] Deprecate the cols and rows properties of Terminal As cols and rows are read only (i.e. automatically determined by rect and font size) there is no need for the properties to be exposed. Instead, users can get the calculated cols and rows using the get_cols() and get_rows() methods. --- .../editor_plugins/terminal/terminal_panel.gd | 4 +- addons/godot_xterm/pty.gd | 2 +- addons/godot_xterm/terminal.gd | 60 +++++++++++------ docs/CHANGELOG.md | 5 ++ docs/api/terminal.md | 67 ++++++++++++------- examples/menu/menu.gd | 4 +- test/platform/unix/unix.test.gd | 4 +- test/scenes/basic.gd | 2 +- 8 files changed, 93 insertions(+), 55 deletions(-) diff --git a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd index 2d435eb..37966d7 100644 --- a/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd +++ b/addons/godot_xterm/editor_plugins/terminal/terminal_panel.gd @@ -265,8 +265,8 @@ func _on_Panel_resized(): var size = tab_container.rect_size if tabs.get_tab_count() > 0: var terminal = tab_container.get_child(tabs.current_tab) - var cols = terminal.cols - var rows = terminal.rows + var cols = terminal.get_cols() + var rows = terminal.get_rows() size_label.text = "Size: %d cols; %d rows\n(%d x %d px)" % [cols, rows, size.x, size.y] else: size_label.text = "Size:\n(%d x %d px)" % [size.x, size.y] diff --git a/addons/godot_xterm/pty.gd b/addons/godot_xterm/pty.gd index 11185cc..2126073 100644 --- a/addons/godot_xterm/pty.gd +++ b/addons/godot_xterm/pty.gd @@ -102,7 +102,7 @@ func _set_terminal(value: _Terminal): return # Connect the new terminal. - resize(_terminal.cols, _terminal.rows) + resize(_terminal.get_cols(), _terminal.get_rows()) if not _terminal.is_connected("size_changed", self, "resizev"): _terminal.connect("size_changed", self, "resizev") if not _terminal.is_connected("data_sent", self, "write"): diff --git a/addons/godot_xterm/terminal.gd b/addons/godot_xterm/terminal.gd index 3b789d4..5340bf4 100644 --- a/addons/godot_xterm/terminal.gd +++ b/addons/godot_xterm/terminal.gd @@ -26,8 +26,8 @@ enum SelectionMode { export(UpdateMode) var update_mode = UpdateMode.AUTO setget set_update_mode -var cols = 2 -var rows = 2 +var cols = 2 setget _set_cols, _get_cols # Deprecated. +var rows = 2 setget _set_rows, _get_rows # Deprecated. # If true, text in the terminal will be copied to the clipboard when selected. export(bool) var copy_on_selection @@ -44,6 +44,9 @@ export var bell_cooldown: float = 0.1 export var blink_on_time: float = 0.6 export var blink_off_time: float = 0.3 +var _cols := 2 +var _rows := 2 + var _default_theme: Theme = preload("./themes/default.tres") var _viewport: Viewport = preload("./nodes/terminal/viewport.tscn").instance() var _native_terminal: Control = _viewport.get_node("Terminal") @@ -55,12 +58,6 @@ var _selecting := false var _selecting_mode: int = SelectionMode.NONE var _selection_timer := Timer.new() -var _dirty := false - -var buffer := StreamPeerBuffer.new() - -var times = 0 - var _buffer := [] @@ -69,12 +66,38 @@ func set_update_mode(value): _native_terminal.update_mode = value -func get_rows() -> int: - return 0 - - func get_cols() -> int: - return 0 + return _cols + + +func _get_cols() -> int: + push_warning( + "The 'cols' property of Terminal is deprecated and will be removed in a future version. Please use the `get_cols()` method instead." + ) + return get_cols() + + +func _set_cols(_value) -> void: + push_error( + "The 'cols' property of Terminal is read-only and determined by rect_size and the theme's font size." + ) + + +func get_rows() -> int: + return _rows + + +func _get_rows() -> int: + push_warning( + "The 'rows' property of Terminal is deprecated and will be removed in a future version. Please use the `get_rows()` method instead." + ) + return get_rows() + + +func _set_rows(_value) -> void: + push_error( + "The 'rows' property of Terminal is read-only and determined by rect_size and the theme's font size." + ) func write(data) -> void: @@ -277,8 +300,8 @@ func _on_key_pressed(data: PoolByteArray, event: InputEventKey): func _on_size_changed(new_size: Vector2): - cols = new_size.x - rows = new_size.y + _cols = new_size.x + _rows = new_size.y emit_signal("size_changed", new_size) @@ -291,10 +314,3 @@ func _on_bell(): func _mouse_to_cell(pos: Vector2) -> Vector2: return Vector2(pos / _native_terminal.cell_size) - - -func _set_size_warning(value): - if value: - push_warning( - "Terminal cols and rows are read only and determined by the font and rect sizes." - ) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 84ae8b2..23dbd4e 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/lihop/godot-xterm/compare/v2.1.1...HEAD) +### Changed +- Deprecated the `cols` and `rows` properties of Terminal. + These properties will be removed in a future version. + Please use `get_cols()` and `get_rows()` instead. + ### Removed - Dropped support for Godot version 3.3.x. diff --git a/docs/api/terminal.md b/docs/api/terminal.md index f61fdc8..8167bbc 100644 --- a/docs/api/terminal.md +++ b/docs/api/terminal.md @@ -47,19 +47,21 @@ For example if the string `"\u001b[38;2;0;255;0;mA"` was written to the terminal |--------------|--------------------------------------|---------| | [bool] | [bell_muted](#prop-bell_muted) | `false` | | [float] | [bell_cooldown](#prop-bell_cooldown) | `0.1` | -| [int] | [cols](#prop-cols) | `2` | -| [int] | [rows](#prop-rows) | `2` | +| [int] | [cols](#prop-cols) *(deprecated)* | `2` | +| [int] | [rows](#prop-rows) *(deprecated)* | `2` | | [UpdateMode] | [update_mode](#prop-update_mode) | `AUTO` | ## Methods -| Returns | Signature | -|----------|-----------------------------------------------------------------| -| void | [clear](#mthd-clear) **()** | -| [String] | [copy_all](#mthd-copy_all) **()** | -| [String] | [copy_selection](#mthd-copy_selection) **()** | -| void | [write](#mthd-write) **(** [String]\|[PoolByteArray] data **)** | +| Returns | Signature | +|----------|-------------------------------------------------------------------| +| void | [clear](#mthd-clear) **()** | +| [String] | [copy_all](#mthd-copy_all) **()** | +| [String] | [copy_selection](#mthd-copy_selection) **()** | +| [int] | [get_cols](#mthd-get_cols) **()** | +| [int] | [get_rows](#mthd-get_rows) **()** | +| void | [write](#mthd-write) **(** [String] \| [PoolByteArray] data **)** | ## Signals @@ -105,19 +107,6 @@ For example if the string `"\u001b[38;2;0;255;0;mA"` was written to the terminal ## Property Descriptions -- [int] **rows** - - | | | - |-----------|------------| - | *Default* | `2` | - | *Setter* | None | - | *Getter* | None | - - The number of rows in the terminal's rect. - When using a monospace font, this is typically the number of characters that can fit from the top to the bottom. - It will automatically update as the Control's rect_size changes, and therefore shouldn't be used to set the size of the terminal directly. - ---- - [int] **cols** @@ -125,11 +114,23 @@ For example if the string `"\u001b[38;2;0;255;0;mA"` was written to the terminal |-----------|------------| | *Default* | `2` | | *Setter* | None | - | *Getter* | None | + | *Getter* | get_cols() | + + *Deprecated*. This property is deprecated and will be removed in a future version. + Please use [get_cols](#mthd-get_cols) instead. + +--- + +- [int] **rows** ***Deprecated*** + + | | | + |-----------|------------| + | *Default* | `2` | + | *Setter* | None | + | *Getter* | get_rows() | - The number of columns in the terminal's rect. - When using a monospace font, this is typically the number of characters that can fit from one side to another. - It will automatically update as the Control's rect_size changes, and therefore shouldn't be used to set the size of the terminal directly. + *Deprecated*. This property is deprecated and will be removed in a future version. + Please use the [get_rows](#mthd-get_rows) instead. --- @@ -195,6 +196,22 @@ For example if the string `"\u001b[38;2;0;255;0;mA"` was written to the terminal --- +- [int] **get_cols** **()** + + Returns the width of the terminal in characters. + When using a monospace font, this is the number of visible characters that can fit from one side of the terminal to the other in a single row. + It will automatically update according to the terminal's rect_size and theme's font size. + +--- + +- [int] **get_rows** **()** + + Returns the height of the terminal in characters. + When using a monospace font, this is the number of visible characters that can fit from the top of the terminal to the bottom in a single column. + It will automatically update according to the terminal's rect_size and theme's font size. + +--- + - void **write** **(** [String] \| [PoolByteArray] data **)** Writes data to the terminal emulator. Accepts either a [String] or [PoolByteArray]. diff --git a/examples/menu/menu.gd b/examples/menu/menu.gd index 6b66337..05e8db5 100644 --- a/examples/menu/menu.gd +++ b/examples/menu/menu.gd @@ -52,7 +52,7 @@ func _ready(): func draw_all(_size = Vector2.ZERO): - offset = int(floor(($Terminal.cols / 2.0) - (TITLE_WIDTH / 2.0))) + offset = int(floor(($Terminal.get_cols() / 2.0) - (TITLE_WIDTH / 2.0))) tput.reset() row = 5 tput.civis() # Hide the cursor. @@ -94,7 +94,7 @@ func draw_menu(): var item = menu_items[i] if not col_offset: - col_offset = int(floor(($Terminal.cols / 2) - (item.name.length() / 2))) + col_offset = int(floor(($Terminal.get_cols() / 2) - (item.name.length() / 2))) tput.cup(row, offset) diff --git a/test/platform/unix/unix.test.gd b/test/platform/unix/unix.test.gd index c2f8c40..692178c 100644 --- a/test/platform/unix/unix.test.gd +++ b/test/platform/unix/unix.test.gd @@ -212,12 +212,12 @@ class TestPTYSize: assert_eq( stty_rows, - terminal.rows, + terminal.get_rows(), "Expected stty to report correct number of rows for layout '%s'" % s ) assert_eq( stty_cols, - terminal.cols, + terminal.get_cols(), "Expected stty to report correct number of columns for layout '%s'" % s ) diff --git a/test/scenes/basic.gd b/test/scenes/basic.gd index 3621181..841891a 100644 --- a/test/scenes/basic.gd +++ b/test/scenes/basic.gd @@ -4,7 +4,7 @@ onready var terminal = $Terminal func _ready(): - print("terminal size; rows %d; cols %d;" % [terminal.rows, terminal.cols]) + print("terminal size; rows %d; cols %d;" % [terminal.get_rows(), terminal.get_cols()]) terminal.write("h") yield(get_tree().create_timer(1), "timeout") terminal.write(" i")