Browse Source

Working embeded ANSI.

There seems to be an issue where it won't let me init the ANSI
storage as a global, it wants it in a function.

The CP437 to Unicode wants bytes to convert.
Steve Thielemann 3 years ago
parent
commit
7c8b384b18
4 changed files with 116 additions and 1 deletions
  1. 80 0
      ansi-to-go.py
  2. 1 1
      door/convert.go
  3. 10 0
      space.ans
  4. 25 0
      testdoor/testdoor.go

+ 80 - 0
ansi-to-go.py

@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+
+import sys
+
+##
+## Convert ANSI art files to C++ header file.
+##
+
+filenames = sys.argv
+filenames.pop(0)
+
+if not filenames:
+    print("I need filename(s) to convert.")
+    sys.exit(0)
+
+
+
+def my_repr(line):
+    """ Given a latin1 line, output valid C++ string.
+
+    if the character is < 0x20 or > 0x7e, output in hex format.
+    if the character is ', " or ?, output \', \", or \?.
+
+    """
+    r = ""
+    for c in line:
+        o = ord(c)
+        if ((o< 0x20) or (o > 0x7e)):
+            r += "\\x{0:02x}".format(o)
+        else:
+            if c == "'":
+                r += "\\'"
+            elif c == '"':
+                r += '\\"'
+            # elif c == '?':
+            #    r += '\\?'
+            elif c == '\\':
+                r += '\\\\'
+            else:
+                r += c
+    return r    
+
+def readfile(filename):
+    """ Reads the given ANSI file and outputs valid C++ code. """
+
+    # basename will be the name of the array defined in the headerfile.
+    basename = filename
+    pos = basename.find('.')
+    if pos != -1:
+        basename = basename[:pos]
+    pos = basename.rfind('/')
+    if pos != -1:
+        basename = basename[pos+1:]
+    
+    # Read the file into lines
+    lines = []
+    with open(filename, "r", encoding="latin1") as fp:
+        for line in fp:
+            line = line.rstrip("\r\n")
+            lines.append(line)
+
+    print("{0} := [...]string {{".format(basename.upper()))
+    # print("std::array<const char *,{1}> {0} = {{".format(basename, len(lines)))
+    # first = True
+    for line in lines:
+        # if not first:
+        #     print(",")
+        # else:
+        #    first = False
+        print("    \"{0}\",".format(my_repr(line)))
+    print(" }\n")
+
+# Begin the output process
+# print("#include <array>\n")
+
+# Process each file given
+for filename in filenames:
+    readfile(filename)
+
+

+ 1 - 1
door/convert.go

@@ -8,7 +8,7 @@ package door
 func CP437_to_Unicode(cp437 string) string {
 	var result string
 
-	for _, char := range cp437 {
+	for _, char := range []byte(cp437) {
 		switch int(char) {
 		case 0x01:
 			result += "\u263A"

+ 10 - 0
space.ans

@@ -0,0 +1,10 @@
+[?7hワイイイイロワ アアイイロロアイイロアイイロロアイイロロアイイロアイイロロアイイロロ
+アアロアアロローアイローアロローアロローアイローアロローアロロ
+ーアワワワワ   ーアイロ ーイローーーーワワ ーイローーーーワワ
+ ゚゚゚゚イイーーアアイ  ーーアアイイ ー ー゚゚  ーーアアイイ ー ー゚゚
+ロ    アイ ー  アイ  アイ  アイ  アイ  アイ  アイ
+゚ ローーア゚     ーア  ーーア  ーーア  ーア  ーーア  ーーア
+
+
+Bugz in space, with a deck of cards
+

+ 25 - 0
testdoor/testdoor.go

@@ -131,6 +131,31 @@ func main() {
 
 	d.Write(door.Reset + door.CRNL + door.CRNL)
 
+	SPACE := [...]string{
+		"\x1b[?7h\x1b[255D\x1b[0;1;32m\xdc\x1b[42m\xb2\xb2\xb2\xb2\xdb\x1b[40m\xdc \x1b[42m\xb1\xb1\xb2\xb2\xdb\xdb\x1b[4C\xb1\xb2\xb2\xdb\x1b[4C\xb1\xb2\xb2\xdb\xdb\x1b[3C\xb1\xb2\xb2\xdb\xdb\x1b[11C\xb1\xb2\xb2\xdb\x1b[4C\xb1\xb2\xb2\xdb\xdb\x1b[3C\xb1\xb2\xb2\xdb\xdb\x1b[40m",
+		"\x1b[42m\xb1\xb1\x1b[4C\xdb\x1b[2C\xb1\xb1\x1b[2C\xdb\xdb\x1b[2C\xb0\xb1\x1b[2C\xb2\xdb\x1b[2C\xb0\xb1\x1b[3C\xdb\xdb\x1b[1C\xb0\xb1\x1b[3C\xdb\xdb\x1b[9C\xb0\xb1\x1b[2C\xb2\xdb\x1b[2C\xb0\xb1\x1b[3C\xdb\xdb\x1b[1C\xb0\xb1\x1b[3C\xdb\xdb\x1b[40m",
+		"\x1b[42m\xb0\xb1\x1b[40m\xdc\xdc\xdc\xdc   \x1b[42m\xb0\xb1\x1b[2C\xb2\xdb\x1b[1C \xb0\x1b[4C\xb2\xdb\x1b[1C\xb0\xb0\x1b[6C\xb0\xb0\x1b[0;32m\xdc\xdc\x1b[11C\x1b[42m \x1b[1m\xb0\x1b[4C\xb2\xdb\x1b[1C\xb0\xb0\x1b[6C\xb0\xb0\x1b[0;32m\xdc\xdc",
+		" \x1b[1m\xdf\xdf\xdf\xdf\x1b[42m\xb2\xb2\x1b[2C\xb0\xb0\xb1\xb1\xb2\x1b[2C  \xb0\xb0\xb1\xb1\xb2\xb2\x1b[1C \xb0\x1b[6C \xb0\x1b[0;32m\xdf\xdf\x1b[11C\x1b[42m  \x1b[1m\xb0\xb0\xb1\xb1\xb2\xb2\x1b[1C \xb0\x1b[6C \xb0\x1b[0;32m\xdf\xdf",
+		"\xdb    \x1b[1;42m\xb1\xb2\x1b[2C \xb0\x1b[5C  \x1b[4C\xb1\xb2\x1b[1C  \x1b[3C\xb1\xb2\x1b[1C  \x1b[3C\xb1\xb2\x1b[8C  \x1b[4C\xb1\xb2\x1b[1C  \x1b[3C\xb1\xb2\x1b[1C  \x1b[3C\xb1\xb2\x1b[40m",
+		"\x1b[0;32m\xdf\x1b[42m \x1b[40m\xdb\x1b[1;42m\xb0\xb0\xb1\x1b[0;32m\xdf \x1b[42m  \x1b[7C  \x1b[2C\x1b[1m\xb0\xb1\x1b[3C  \xb0\xb0\xb1\x1b[3C  \xb0\xb0\xb1\x1b[10C  \x1b[2C\xb0\xb1\x1b[3C  \xb0\xb0\xb1\x1b[3C  \xb0\xb0\xb1\x1b[40m",
+		"",
+		"",
+		"\x1b[37mBugz in space, with a deck of cards",
+		"\x1b[0m",
+	}
+
+	d.Write(door.Clrscr)
+	for _, line := range SPACE {
+		if door.Unicode {
+			d.Write(door.CP437_to_Unicode(line) + door.CRNL)
+		} else {
+			d.Write(line + door.CRNL)
+		}
+	}
+
+	d.Write(door.Reset + door.CRNL + "Press a key to continue...")
+	d.WaitKey(120, 0)
+
 	message = fmt.Sprintf("Returning %s to the BBS..."+door.CRNL, name)
 	d.Write(message)