Skip to content

Commit

Permalink
Fixed ANSI control codes
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Jul 23, 2023
1 parent 21f7eff commit 33e6a08
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2023 Marco Maccaferri and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package com.maccasoft.propeller;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import com.maccasoft.propeller.SerialTerminal.Cell;

@TestInstance(Lifecycle.PER_CLASS)
class SerialTerminalTest {

Display display;
SerialTerminal instance;

@BeforeAll
void initialize() {
display = Display.getDefault();
}

@BeforeEach
void setUp() {
instance = new SerialTerminal();
instance.display = display;
instance.shell = new Shell(display);
instance.screenWidth = 80;
instance.screenHeight = 25;
instance.screen = new Cell[instance.screenHeight][instance.screenWidth];
instance.canvas = new Canvas(instance.shell, SWT.NONE);
for (int y = 0; y < 25; y++) {
for (int x = 0; x < 80; x++) {
instance.screen[y][x] = instance.new Cell(null, null);
}
}
}

@AfterEach
void tearDown() {
while (display.readAndDispatch()) {

}
instance.shell.dispose();
while (display.readAndDispatch()) {

}
}

@AfterAll
void terminate() {
display.dispose();
}

@Test
void testANSINoArguments() {
SerialTerminal.ANSI subject = instance.new ANSI();
subject.write((char) 0x1B);
subject.write('[');
subject.write('X');
Assertions.assertEquals(0, subject.argc);
}

@Test
void testANSISingleArgument() {
SerialTerminal.ANSI subject = instance.new ANSI();
subject.write((char) 0x1B);
subject.write('[');
subject.write('1');
subject.write('2');
subject.write('X');
Assertions.assertEquals(1, subject.argc);
Assertions.assertEquals(12, subject.args[0]);
}

@Test
void testANSIMultipleArguments() {
SerialTerminal.ANSI subject = instance.new ANSI();
subject.write((char) 0x1B);
subject.write('[');
subject.write('1');
subject.write('2');
subject.write(';');
subject.write('3');
subject.write('4');
subject.write('X');
Assertions.assertEquals(2, subject.argc);
Assertions.assertEquals(12, subject.args[0]);
Assertions.assertEquals(34, subject.args[1]);
}

@Test
void testANSICursorPosition() {
SerialTerminal.ANSI subject = instance.new ANSI();
subject.write((char) 0x1B);
subject.write('[');
subject.write('1');
subject.write('2');
subject.write(';');
subject.write('2');
subject.write('H');
Assertions.assertEquals(12 - 1, instance.cy);
Assertions.assertEquals(2 - 1, instance.cx);
}

@Test
void testANSIColor() {
SerialTerminal.ANSI subject = instance.new ANSI();
subject.write((char) 0x1B);
subject.write('[');
subject.write('3');
subject.write('1');
subject.write(';');
subject.write('4');
subject.write('2');
subject.write('m');
subject.write('A');
Assertions.assertEquals('A', instance.screen[0][0].character);
Assertions.assertEquals(subject.colors[1], instance.screen[0][0].foreground.getRGB());
Assertions.assertEquals(subject.colors[2], instance.screen[0][0].background.getRGB());
}

}
21 changes: 15 additions & 6 deletions modules/spin-tools/src/com/maccasoft/propeller/SerialTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ public void run() {
redraw(Math.min(cx, screenWidth - 1) * characterWidth, cy * characterHeight, characterWidth, characterHeight);
}
}
screen[cy][cx].foreground = foreground;
screen[cy][cx].background = background;
screen[cy][cx].character = c;
cx++;
break;
Expand All @@ -361,7 +363,7 @@ public void run() {
class ANSI extends TTY {

int state = 0;
int argc, fg = 7, bg = 0;
int idx, argc, fg = 7, bg = 0;
int[] args = new int[16];
char prefix;
int savedCx, savedCy;
Expand Down Expand Up @@ -390,7 +392,7 @@ public void write(char c) {
switch (state) {
case 0:
if (c == 0x1B) {
argc = -1;
argc = idx = 0;
args[0] = 0;
prefix = 0;
state = 1;
Expand Down Expand Up @@ -446,15 +448,16 @@ public void write(char c) {
case '7':
case '8':
case '9':
if (argc == -1) {
if (idx == argc) {
argc++;
args[argc] = 0;
args[idx] = 0;
}
args[argc] = args[argc] * 10 + (c - '0');
args[idx] = args[idx] * 10 + (c - '0');
break;
case ';':
argc++;
args[argc] = 0;
idx++;
args[idx] = 0;
break;
case '?':
prefix = c;
Expand Down Expand Up @@ -580,6 +583,12 @@ else if (args[i] >= 30 && args[i] <= 37) {
else if (args[i] >= 40 && args[i] <= 47) {
bg = args[i] - 40;
}
else if (args[i] >= 90 && args[i] <= 97) {
fg = (args[i] - 90) | 8;
}
else if (args[i] >= 100 && args[i] <= 107) {
bg = (args[i] - 100) | 8;
}
}
if (argc == 0) {
bg = 0;
Expand Down

0 comments on commit 33e6a08

Please sign in to comment.