Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2394 - Expose terminal cursor #2397

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions doc/source/structures/misc/terminal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ Structure
- get and set
- Height of a character cell in pixels.

* - :attr:`CURSORCOL`
- :struct:`Scalar`
- get and set
- Current cursor column, between 0 and WIDTH-1.

* - :attr:`CURSORROW`
- :struct:`Scalar`
- get and set
- Current cursor row, between 0 and HEIGHT-1.

* - :meth:`PUT(string)`
- None
- Method Call
- Output string without newline.

* - :attr:`INPUT`
- :struct:`TerminalInput`
- get
Expand Down Expand Up @@ -166,6 +181,26 @@ Structure
divisible by 2. If you try to set it to any other value, it
will snap to the allowed range and increment.

.. attribute:: Terminal:CURSORCOL

:access: Get/Set
:type: :struct:`Scalar`

Current cursor column, between 0 and WIDTH-1.

.. attribute:: Terminal:CURSORROW

:access: Get/Set
:type: :struct:`Scalar`

Current cursor row, between 0 and HEIGHT-1.

.. method:: Terminal:PUT(text)

:parameter text: (string) Text to print

Put string at current cursor position (without implied newline).

.. attribute:: Terminal:INPUT

:access: Get
Expand Down
22 changes: 22 additions & 0 deletions kerboscript_tests/terminalcursor.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@lazyglobal off.

local teststring is "This is a teststring".
local words is teststring:split(" ").

print "The following lines should all look identical".
print teststring. //comparison, do not go back to this line
print words[0].
set terminal:cursorcol to words[0]:length + 1.
set terminal:cursorrow to terminal:cursorrow - 1.
print words[1].
set terminal:cursorrow to terminal:cursorrow - 1.
set terminal:cursorcol to words[0]:length + words[1]:length + 2.
print words[2] + " " + words[3].
print words[0] + " " + words[1].
set terminal:cursorrow to terminal:cursorrow - 2.
set terminal:cursorcol to words[0]:length + words[1]:length +2.
terminal:put(words[2]).
set terminal:cursorrow to terminal:cursorrow + 1.
set terminal:cursorcol to terminal:cursorcol - words[2]:length.
terminal:put(words[2]).
print " " + words[3].
9 changes: 9 additions & 0 deletions src/kOS.Safe/Encapsulation/TerminalStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Screen;
using kOS.Safe.Execution;
using kOS.Safe.Utilities;

namespace kOS.Safe.Encapsulation
{
Expand Down Expand Up @@ -161,6 +162,14 @@ private void InitializeSuffixes()
"Character height on in-game terminal screen in pixels"));
AddSuffix("RESIZEWATCHERS", new NoArgsSuffix<UniqueSetValue<UserDelegate>>(() => resizeWatchers));
AddSuffix("INPUT", new Suffix<TerminalInput>(GetTerminalInputInstance));
AddSuffix("CURSORCOL", new SetSuffix<ScalarValue>(() => Shared.Screen.CursorColumnShow,
value => Shared.Screen.MoveCursor(Shared.Screen.CursorRowShow, (int)KOSMath.Clamp(value,0,Shared.Screen.ColumnCount)),
"Current cursor column, between 0 and WIDTH-1."));
AddSuffix("CURSORROW", new SetSuffix<ScalarValue>(() => Shared.Screen.CursorRowShow,
value => Shared.Screen.MoveCursor((int)KOSMath.Clamp(value,0,Shared.Screen.RowCount), Shared.Screen.CursorColumnShow),
"Current cursor row, between 0 and HEIGHT-1."));
AddSuffix("PUT", new OneArgsSuffix<StringValue>(value => Shared.Screen.Print(value,false),
"Put string at current cursor position (without implied newline)."));
}

private void CannotSetWidth(ScalarValue newWidth)
Expand Down
4 changes: 2 additions & 2 deletions src/kOS.Safe/Screen/TextEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using System;
using kOS.Safe.UserIO;
Expand All @@ -12,7 +12,7 @@ public class TextEditor : ScreenBuffer
private int cursorColumnBuffer;
private int cursorRowBuffer;

public override int CursorColumnShow { get { return cursorColumnBuffer; } }
public override int CursorColumnShow { get { return CursorColumn + cursorColumnBuffer; } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This might explain a few other weird behaviours I've seen in the past.

public override int CursorRowShow { get { return CursorRow + cursorRowBuffer; } }

protected StringBuilder LineBuilder { get; set; }
Expand Down