Skip to content

Commit

Permalink
clear, color
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaffySwitcher committed Jul 27, 2024
1 parent ec09ec4 commit ea8f6ac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/parser/parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,17 @@ def findBlockEnd(s: Seq[String], startk: String, endk: String, i: Int, count: In
else if startsWith_strict(s(i), endk) then
findBlockEnd(s, startk, endk, i+1, count-1)
else findBlockEnd(s, startk, endk, i+1, count)

def parseLocate(line: String): (Int, Int) =
val start = findLineStart(line, 6)
val parts = line.substring(start).split(",").map(_.trim)
if parts.length != 2 then
closeTofu(s"Syntax error in locate: $line\n\nExpected format: locate x, y")
(parts(0).toInt, parts(1).toInt)

def parseColor(line: String): (String, String) =
val start = findLineStart(line, 5)
val parts = line.substring(start).split(",").map(_.trim)
if parts.length != 2 then
closeTofu(s"Syntax error in color: $line\n\nExpected format: color text/background, color")
(parts(0), parts(1))
21 changes: 19 additions & 2 deletions src/runner/runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tofu.{debugMessage, debug_printSeq}
import tofu.variables.*
import tofu.parser.*
import tofu.reader.findLineStart
import tofu.terminal.TerminalOps

import tofu.reader.readScript
import tofu.closeTofu
Expand Down Expand Up @@ -101,6 +102,22 @@ private def loopScript(s: Seq[String], ifunc: Seq[Int], nfunc: Seq[String], i: I
case "print" =>
printArg(s(i))
loopScript(s, ifunc, nfunc, i+1)
case "clear" =>
print(TerminalOps.clearScreen())
loopScript(s, ifunc, nfunc, i+1)
case "locate" =>
val (x, y) = parseLocate(s(i))
print(TerminalOps.locate(x, y))
loopScript(s, ifunc, nfunc, i+1)
case "color" =>
val (colorType, color) = parseColor(s(i))
debugMessage(s"Changing color of '$colorType' to '$color'")
colorType match {
case "text" => print(TerminalOps.getColor(color))
case "background" => print(TerminalOps.getBackgroundColor(color))
case _ => closeTofu(s"Syntax error! Unknown color type '$colorType'")
}
loopScript(s, ifunc, nfunc, i+1)
case "sleep" =>
runSleep(s(i))
loopScript(s, ifunc, nfunc, i+1)
Expand All @@ -127,8 +144,8 @@ private def loopScript(s: Seq[String], ifunc: Seq[Int], nfunc: Seq[String], i: I
private def lineType(line: String, types: Vector[String] =
Vector(
"string", "readstr", "while", "sleep", "calcint", "int",
"print", "if", "function", "exec", "call", "break", "stop",
"array", "arradd", "arrget"),
"print", "clear", "locate", "color", "if", "function", "exec", "call",
"break", "stop", "array", "arradd", "arrget"),
i: Int = 0): String =
if i >= types.length then "none"
else if startsWith_strict(line, types(i)) then types(i)
Expand Down
45 changes: 45 additions & 0 deletions src/runner/terminal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tofu.terminal

import scala.io.AnsiColor

object TerminalOps {
def locate(x: Int, y: Int): String = s"\u001b[${y};${x}H"

def clearScreen(): String = s"\u001b[2J\u001b[H"

def getColor(color: String): String = color match {
case "red" => AnsiColor.RED
case "green" => AnsiColor.GREEN
case "blue" => AnsiColor.BLUE
case "yellow" => AnsiColor.YELLOW
case "magenta" => AnsiColor.MAGENTA
case "cyan" => AnsiColor.CYAN
case "white" => AnsiColor.WHITE
case "black" => AnsiColor.BLACK
case "blink" => AnsiColor.BLINK
case "bold" => AnsiColor.BOLD
case "invisible" => AnsiColor.INVISIBLE
case "reset" => AnsiColor.RESET
case "reversed" => AnsiColor.REVERSED
case "underlined" => AnsiColor.UNDERLINED
case _ => throw new IllegalArgumentException(s"Unknown color '$color'")
}

def getBackgroundColor(color: String): String = color match {
case "red" => AnsiColor.RED_B
case "green" => AnsiColor.GREEN_B
case "blue" => AnsiColor.BLUE_B
case "yellow" => AnsiColor.YELLOW_B
case "magenta" => AnsiColor.MAGENTA_B
case "cyan" => AnsiColor.CYAN_B
case "white" => AnsiColor.WHITE_B
case "black" => AnsiColor.BLACK_B
case "blink" => AnsiColor.BLINK
case "bold" => AnsiColor.BOLD
case "invisible" => AnsiColor.INVISIBLE
case "reset" => AnsiColor.RESET
case "reversed" => AnsiColor.REVERSED
case "underlined" => AnsiColor.UNDERLINED
case _ => throw new IllegalArgumentException(s"Unknown background color '$color'")
}
}

0 comments on commit ea8f6ac

Please sign in to comment.