Hi everyone! For learning purpose I'm porting some python examples to nim that 
use the library blt, so far I got this:
    
    
    import
      blt,
      tables
    
    var keyNames = {
      0: "",
      TK_MOUSE_LEFT: "LMB",
      TK_MOUSE_RIGHT: "RMB",
      TK_MOUSE_MIDDLE: "MMB",
      TK_CLOSE: "Close",
      TK_BACKSPACE: "Backspace",
      TK_TAB: "Tab",
      TK_RETURN: "Enter",
      TK_SHIFT: "Shift",
      TK_CONTROL: "Ctrl",
      TK_PAUSE: "Pause",
      TK_ESCAPE: "Escape",
      TK_PAGEUP: "Page Up",
      TK_PAGEDOWN: "Page Down",
      TK_END: "End",
      TK_HOME: "Home",
      TK_LEFT: "Left",
      TK_UP: "Up",
      TK_RIGHT: "Right",
      TK_DOWN: "Down",
      TK_INSERT: "Insert",
      TK_DELETE: "Delete",
      TK_F1: "F1",
      TK_F2: "F2",
      TK_F3: "F3",
      TK_F4: "F4",
      TK_F5: "F5",
      TK_F6: "F6",
      TK_F7: "F7",
      TK_F8: "F8",
      TK_F9: "F9",
      TK_F10: "F10",
      TK_F11: "F11",
      TK_F12: "F12",
      TK_KP_DIVIDE: "Keypad /",
      TK_KP_MULTIPLY: "Keypad *",
      TK_KP_MINUS: "Keypad -",
      TK_KP_PLUS: "Keypad +",
      TK_KP_ENTER: "Keypad Enter",
      TK_KP_0: "Keypad 0",
      TK_KP_1: "Keypad 1",
      TK_KP_2: "Keypad 2",
      TK_KP_3: "Keypad 3",
      TK_KP_4: "Keypad 4",
      TK_KP_5: "Keypad 5",
      TK_KP_6: "Keypad 6",
      TK_KP_7: "Keypad 7",
      TK_KP_8: "Keypad 8",
      TK_KP_9: "Keypad 9",
      TK_KP_PERIOD: "Keypad .",
    }.toTable()
    
    proc drawFrame(x,y,w,h:int) =
      terminal_clear_area(x.cint,y.cint,w.cint,h.cint)
      
      for i in x..(x+w)-1:
        discard terminal_print(i.cint, y.cint, "─")
        discard terminal_print(i.cint, (y+h-1).cint,"─")
      
      for j in y..y+h-1:
        discard terminal_print(x.cint, j.cint, "│")
        discard terminal_print((x+w-1).cint, j.cint, "│")
      
      discard terminal_print(x.cint, y.cint, "┌")
      discard terminal_print((x+w-1).cint, y.cint ,"┐")
      discard terminal_print(x.cint, (y+h-1).cint,"└")
      discard terminal_print((x+w-1).cint, (y+h-1).cint,"┘")
    
    discard terminal_open()
    discard terminal_set("window: size=80x25, cellsize=auto, title='Omni: 
menu;font: default'")
    terminal_color(color_from_name("white"))
    terminal_composition(TK_OFF)
    terminal_refresh()
    
    var
      key : int
      max_chars = 32
      text = ""
      character = ' '
      result : int
      char_result = 0
    
    while true:
      terminal_clear()
      terminal_color(color_from_name("white"))
      
      discard terminal_print(2, 1, "Select different input tests by pressing 
corresponding number:")
      
      discard terminal_print(2, 3, "[color=orange]1.[/color] read_str")
      drawFrame(5, 4, max_chars + 2, 3)
      discard terminal_printf(6,5, "$1", text)
      discard terminal_printf((5 + max_chars + 2 + 1).cint, 5.cint, 
"[color=gray] $1", (if result >= 0: "OK" else: "Cancelled"))
      
      discard terminal_print(2, 8, "[color=orange]2.[/color] read_char")
      drawFrame(5, 9, 5, 3)
      terminal_put(7, 10, character.cint)
      discard terminal_printf(11, 10, "[color=gray]$1", (if 
keyNames.hasKey(char_result): keyNames[char_result] else: $chr(char_result)))
      
      terminal_refresh()
      
      key = terminal_read()
      
      if key in [TK_CLOSE, TK_ESCAPE]:
        break
      elif key == TK_1:
        terminal_color(color_from_name("orange"))
        drawFrame(5, 4, max_chars + 2, 3)
        result = terminal_read_str(6, 5, text, max_chars.cint)
      elif key == TK_2:
        terminal_color(color_from_name("orange"))
        drawFrame(5, 9, 5, 3)
        while true:
          terminal_put(7, 10, ord(character))
          terminal_clear_area(11, 10, 16, 1)
          discard terminal_printf(11, 10, "[color=gray]$1", (if 
keyNames.hasKey(char_result): keyNames[char_result] else: $(char_result)))
          terminal_refresh()
          
          character = ' '
          key = terminal_read()
          if key in [TK_ESCAPE, TK_CLOSE, TK_RETURN]:
            break
          elif bool(terminal_check(TK_WCHAR)):
            character = chr(terminal_state(TK_WCHAR))
            char_result = key
          elif key < TK_KEY_RELEASED:
            char_result = key
    
    terminal_close()
    
    

converted from here 
[https://github.com/ibatugow/blt_samples/blob/master/Python/text_input.py](https://github.com/ibatugow/blt_samples/blob/master/Python/text_input.py)

The example is working except for the read_str function: when I finished 
inserting the string, it prints always just the first character of the string. 
I suppose the error is in this line:
    
    
    discard terminal_printf(6,5, "$1", text)
    
    

but it seems legit to me. Anyone can help me? What's wrong?

Thanks! 

Reply via email to