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 all 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
7 changes: 6 additions & 1 deletion doc/source/commands/terminalgui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Terminal and game environment
PRINT 4+1.
PRINT "4 times 8 is: " + (4*8).

This is an alias for TERMINAL:PUTLN, see :ref:`terminal struct <terminal>`.

.. global:: SET TERMINAL:WIDTH. GET TERMINAL:WIDTH

Gets or sets the terminal's width in characters.
Expand All @@ -33,7 +35,10 @@ Terminal and game environment
:parameter col: (integer) column starting with zero (left)
:parameter line: (integer) line starting with zero (top)

Used in combination with :global:`PRINT`. Prints the selected text to the screen at specified location. Can print strings, or the result of an expression::
Used in combination with :global:`PRINT`. Prints the selected text to the screen at specified location.
This does **not** move the terminal cursor, as opposed to PRINT and most :ref:`terminal <terminal>` output methods.
This is an alias for TERMINAL:PUTAT, see :ref:`terminal struct <terminal>`.
Can print strings, or the result of an expression::

PRINT "Hello" AT(0,10).
PRINT 4+1 AT(0,10).
Expand Down
73 changes: 73 additions & 0 deletions doc/source/structures/misc/terminal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ 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:`MOVECURSOR(column,row)`
- None
- Method Call
- Move cursor to position.

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

* - :meth:`PUTLN(string)`
- None
- Method Call
- Output string with newline.

* - :meth:`PUTAT(text,column,row)`
- None
- Method Call
- Output string at position

* - :attr:`INPUT`
- :struct:`TerminalInput`
- get
Expand Down Expand Up @@ -166,6 +196,49 @@ 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:MOVECURSOR(column,row)

:parameter column: (scalar) column to move to.
:parameter row: (scalar) row to move to.

Move both coordinates of the cursor at once.

.. method:: Terminal:PUT(text)

:parameter text: (string) Text to print

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

.. method:: Terminal:PUTLN(text)

:parameter text: (string) Text to print

Put string at current cursor position (with implied newline).
This is an alias for :global:`PRINT`

.. method:: Terminal:PUTAT(text,column,row)

:parameter text: (string) Text to print
:parameter column: (scalar) Horizontal starting position
:parameter row: (scalar) Vertical starting position

Put string at position without moving the cursor.
This is an alias for PRINT AT.

.. attribute:: Terminal:INPUT

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

terminal:putln("enter text to echo:").
local line is "".
until false {
local c is terminal:input:getchar().
if(c = terminal:input:enter) {
terminal:putln(""). //linefeed
if(line:length > 0) {
terminal:putln(line).
set line to "".
} else {
break.
}
} else {
terminal:put(c).
set line to line + c.
}
}
17 changes: 17 additions & 0 deletions kerboscript_tests/terminalcursor/loadingbar.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@lazyglobal off.

//do a 25 s loading bar in the current line

local row is terminal:cursorrow.
terminal:putln(""). //linefeed the cursor

from {local progress is 0.} until progress = 100 step { set progress to progress+1. } do {
local bar is "".
local prog_per_bar is (terminal:width - 4) / 100.0.
from { local pos is 0.} until pos/prog_per_bar >= progress step { set pos to pos+1. } do {
terminal:putat("-", pos, row).
}
terminal:putat(progress:tostring():padleft(3), terminal:width-4, row).
terminal:putat("%", terminal:width-1, row).
wait 0.25.
}
22 changes: 22 additions & 0 deletions kerboscript_tests/terminalcursor/scrambledoutput.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.
terminal:putln(words[1]).
set terminal:cursorrow to terminal:cursorrow - 1.
set terminal:cursorcol to words[0]:length + words[1]:length + 2.
terminal:putln(words[2] + " " + words[3]).
print words[0] + " " + words[1].
terminal:putat(words[0], 0, terminal:cursorrow - 1).
terminal:movecursor(words[0]:length + words[1]:length +2, terminal:cursorrow - 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].
8 changes: 8 additions & 0 deletions src/kOS.Safe.Test/Execution/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public int CursorRowShow
}
}

public bool CursorVisible
{
get
{
return false;
}
}

public bool ReverseScreen
{
get
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe.Test/kOS.Safe.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>kOS.Safe.Test</RootNamespace>
<AssemblyName>kOS.Safe.Test</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down
15 changes: 15 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,20 @@ 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.AbsoluteCursorRow, (int)KOSMath.Clamp(value,0,Shared.Screen.ColumnCount)),
"Current cursor column, between 0 and WIDTH-1."));
AddSuffix("CURSORROW", new SetSuffix<ScalarValue>(() => Shared.Screen.AbsoluteCursorRow,
value => Shared.Screen.MoveCursor(value, Shared.Screen.CursorColumnShow),
"Current cursor row, between 0 and HEIGHT-1."));
AddSuffix("MOVECURSOR", new TwoArgsSuffix<ScalarValue, ScalarValue>((ScalarValue col, ScalarValue row) => Shared.Screen.MoveCursor(row, col),
"Move cursor to (column, row)."));
AddSuffix("PUT", new OneArgsSuffix<Structure>(value => Shared.Screen.Print(value.ToString(),false),
"Put string at current cursor position (without implied newline)."));
AddSuffix("PUTLN", new OneArgsSuffix<Structure>(value => Shared.Screen.Print(value.ToString()),
"Put string at current cursor position (with implied newline)."));
AddSuffix("PUTAT", new ThreeArgsSuffix<Structure, ScalarValue, ScalarValue>((Structure value, ScalarValue col, ScalarValue row) => Shared.Screen.PrintAt(value.ToString(), row, col),
"Put string at position without moving the cursor."));
}

private void CannotSetWidth(ScalarValue newWidth)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Execution/CPU.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using kOS.Safe.Binding;
using kOS.Safe.Binding;
using kOS.Safe.Callback;
using kOS.Safe.Compilation;
using kOS.Safe.Encapsulation;
Expand Down
1 change: 0 additions & 1 deletion src/kOS.Safe/Screen/IScreenBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public interface IScreenBuffer
void SetSize(int rowCount, int columnCount);
int ScrollVertical(int deltaRows);
void MoveCursor(int row, int column);
void MoveToNextLine();
void PrintAt(string textToPrint, int row, int column);
void Print(string textToPrint);
void Print(string textToPrint, bool addNewLine);
Expand Down
14 changes: 14 additions & 0 deletions src/kOS.Safe/Screen/IScreenBufferLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,19 @@ public interface IScreenBufferLine
void ArrayCopyFrom(IScreenBufferLine source, int sourceStart, int destinationStart, int length = -1);
void ArrayCopyFrom(char[] source, int sourceStart, int destinationStart, int length = -1);
void TouchTime();

/// <summary>
/// Set a single character without changing the timestamp. Should be used carefully in conjunction with SetTimestamp,
/// e.g. by SubBuffer.MergeTo
/// </summary>
/// <param name="i">Target Index</param>
/// <param name="c">Value to set</param>
void SetCharIgnoreTime(int i, char c);

/// <summary>
/// Manually set the change timestamp. Should be used carefully in conjunction with SetCharIgnoreTime, e.g. by SubBuffer.MergeTo
/// </summary>
/// <param name="timestamp">Timestamp to set</param>
void SetTimestamp(ulong timestamp);
}
}
3 changes: 2 additions & 1 deletion src/kOS.Safe/Screen/IScreenSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public interface IScreenSnapShot
int CursorColumn {get;}
int CursorRow {get;}
int RowCount {get;}

bool CursorVisible { get; }

string DiffFrom(IScreenSnapShot older);

IScreenSnapShot DeepCopy();
Expand Down
Loading