예제 #1
0
import ttyio4 as ttyio

terminalwidth = ttyio.getterminalwidth()
ttyio.echo("{home}{clear}", end="")
ttyio.echo("{curpos:0,0}", end="")
ttyio.echo("foo bar baz bing foo bar baz bing foo bar baz bing ".ljust(
    terminalwidth - 2, "*"),
           end="")
ttyio.echo("{home}alpha bravo charlie delta{el}{f6}")
예제 #2
0
def handlemci(buf:str, stripcommands:bool=False) -> str:
  width = ttyio.getterminalwidth()
  # https://www.c64-wiki.com/wiki/Color
  # https://en.wikipedia.org/wiki/ANSI_escape_code
  mcicommands = (
  { "command": "{clear}",      "ansi": "2J" },
  { "command": "{home}",       "ansi": "0;0H" },
  { "command": "{clreol}",     "ansi": "0K" },
  { "command": "{/all}",       "ansi": "0m" },

  { "command": "{bold}",       "ansi": "1m" },
  { "command": "{/bold}",      "ansi": "22m" },
  { "command": "{faint}",      "ansi": "2m" },
  { "command": "{italic}",     "ansi": "3m" },
  { "command": "{/italic}",    "ansi": "23m" },
  { "command": "{underline}",  "ansi": "4m" },
  { "command": "{/underline}", "ansi": "24m" },
  { "command": "{blink}",      "ansi": "5m" },
  { "command": "{/blink}",     "ansi": "25m" },

  { "command": "{strike}",     "ansi": "9m" },
  { "command": "{/strike}",    "ansi": "29m" },

  { "command": "{magenta}",    "ansi": "35m" },

  { "command": "{reverse}",    "ansi": "7m" },
  { "command": "{/reverse}",   "ansi": "27m" },

  { "command": "{white}",      "ansi": "38;2;255;255;255m", "rgb": (255,255,255) }, # 37m
  { "command": "{red}",        "ansi": "38;2;136;0;0m",     "rgb": (136,0,0) }, # 31m
  { "command": "{cyan}",       "ansi": "38;2;170;255;238m", "rgb": (170,255,238) }, # 36m
  { "command": "{purple}",     "ansi": "38;2;204;68;204m",  "rgb": (204, 68, 204) }, # 35m
  { "command": "{green}",      "ansi": "38;2;0;204;85m",    "rgb": (0,204,85) }, # 32m
  { "command": "{blue}",       "ansi": "38;2;0;0;170m",     "rgb": (0,0,170) }, # 34m
  { "command": "{yellow}",     "ansi": "38;2;238;238;119m", "rgb": (238,238,119) }, # 33m
  { "command": "{orange}",     "ansi": "38;2;221;136;85m",  "rgb": (221,136,85) },
  { "command": "{brown}",      "ansi": "38;2;102;68;0m",    "rgb": (102,68,0) },
  { "command": "{lightred}",   "ansi": "38;2;255;119;119m", "rgb": (255, 119, 119) },
  { "command": "{darkgray}",   "ansi": "38;2;51;51;51m",    "rgb": (51, 51, 51) },
  { "command": "{gray}",       "ansi": "38;2;119;119;119m", "rgb": (119, 119, 119) },
  { "command": "{lightgreen}", "ansi": "38;2;170;255;102m", "rgb": (170, 255, 102) },
  { "command": "{lightblue}",  "ansi": "38;2;0;136;255m",   "rgb": (0, 136, 255) },
  { "command": "{lightgray}",  "ansi": "38;2;187;187;187m", "rgb": (187, 187, 187) },
  { "command": "{black}",      "ansi": "38;2;0;0;0m",       "rgb": (0,0,0) }, # 30m

  { "command": "{bgwhite}",      "ansi": "48;2;255;255;255m", "rgb": (255,255,255) }, # 37m
  { "command": "{bgred}",        "ansi": "48;2;136;0;0m",     "rgb": (136,0,0) }, # 31m
  { "command": "{bgcyan}",       "ansi": "48;2;170;255;238m", "rgb": (170,255,238) }, # 36m
  { "command": "{bgpurple}",     "ansi": "48;2;204;68;204m",  "rgb": (204, 68, 204) }, # 35m
  { "command": "{bggreen}",      "ansi": "48;2;0;204;85m",    "rgb": (0,204,85) }, # 32m
  { "command": "{bgblue}",       "ansi": "48;2;0;0;170m",     "rgb": (0,0,170) }, # 34m
  { "command": "{bgyellow}",     "ansi": "48;2;238;238;119m", "rgb": (238,238,119) }, # 33m
  { "command": "{bgorange}",     "ansi": "48;2;221;136;85m",  "rgb": (221,136,85) },
  { "command": "{bgbrown}",      "ansi": "48;2;102;68;0m",    "rgb": (102,68,0) },
  { "command": "{bglightred}",   "ansi": "48;2;255;119;119m", "rgb": (255, 119, 119) },
  { "command": "{bgdarkgray}",   "ansi": "48;2;51;51;51m",    "rgb": (51, 51, 51) },
  { "command": "{bggray}",       "ansi": "48;2;119;119;119m", "rgb": (119, 119, 119) },
  { "command": "{bglightgreen}", "ansi": "48;2;170;255;102m", "rgb": (170, 255, 102) },
  { "command": "{bglightblue}",  "ansi": "48;2;0;136;255m",   "rgb": (0, 136, 255) },
  { "command": "{bglightgray}",  "ansi": "48;2;187;187;187m", "rgb": (187, 187, 187) },
  { "command": "{bgblack}",      "ansi": "48;2;0;0;0m",       "rgb": (0,0,0) } # 30m
  )

  value = ""
  token = None
  nodes = []

  print(buf)
  print
  # 3 possible tokens: WORD, COMMAND, and WHITESPACE
  patterns = (commandre, wordre)
  index = 0
  for p in patterns:
    m = p.findall(buf)
    print("p=%r, m=%r" % (p, m))
  
  sys.exit(0)

  for ch in buf:
    # print("ch=%r value=%r" % (ch, value))
    if ch == "{":
      if len(value) > 0:
        nodes.append({"token":"WORD", "value":value})
        token = "COMMAND"
        value = ""
    elif ch == "}":
      nodes.append({"token":"COMMAND", "value":"{%s}" % (value)})
      value = ""
      token = None
    elif ch.isspace() is True:
      nodes.append({"token":"WORD", "value":value})
      nodes.append({"token":"WHITESPACE", "value":ch})
      value = ""
      token = None
    else:
      value += ch

  if len(value) > 0:
    nodes.append({"token":"WORD", "value":value})

  # nodes.append({"token": "COMMAND", "value":"{/all}"})

  buf = ""
  pos = 0
  for n in nodes:
    token = n["token"]
    value = n["value"]
    if token == "WORD":
      if len(value)+pos > width:
        pos = 0
        buf += "\n"
      else:
        pos += len(value)
        buf += value
    elif token == "WHITESPACE":
      buf += " "
      pos += 1
    elif token == "COMMAND":
      if stripcommands is False:
        value = value.lower()
        # print("value=%r" % (value))
        for item in mcicommands:
          command = item["command"]
          ansi = item["ansi"]
          if value == command:
            # print("value == k")
            buf += "\033[%s" % (ansi)
            break
    else:
      buf += value
  return buf