From 8ddd20203d2051453ac25a521aab9e6f22c9e436 Mon Sep 17 00:00:00 2001 From: filip Date: Tue, 9 Mar 2021 23:26:32 +0100 Subject: [PATCH] added cursor support, examples and bugfixes --- ansi_escape.py | 81 +++++++++++++++++++++++++++++ examples/a.txt | 2 + examples/choose_line_to_erase.py | 8 +++ examples/erase.py | 2 + examples/move_cursor.py | 7 +++ examples/palette.py | 9 ++++ examples/random_cursor_positions.py | 4 ++ examples/test.py | 4 ++ readme.md | 4 +- 9 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 examples/a.txt create mode 100644 examples/choose_line_to_erase.py create mode 100644 examples/erase.py create mode 100644 examples/move_cursor.py create mode 100644 examples/palette.py create mode 100644 examples/random_cursor_positions.py diff --git a/ansi_escape.py b/ansi_escape.py index 1baa017..e4beebb 100644 --- a/ansi_escape.py +++ b/ansi_escape.py @@ -1,10 +1,79 @@ def bell(): print("\u0007") +def cursor_position(**coordinates): + return_string = "\u001b[" + try: + return_string += coordinates["row"] + except: + pass + return_string += ";" + + try: + return_string += coordinates["column"] + except: + pass + return_string += "H" + return return_string + +set_cursor = cursor_position +cup = cursor_position + +def move_cursor(direction, *cells): + return_string = "\u001b[" + try: + return_string += cells[0] + except: + pass + a = { + "up": "A", + "down": "B", + "forward": "C", + "backward": "D" + } + return_string += a[direction] + return return_string + +def erase_in_display(direction): + return_string = "\u001b[" + a = { + "forward": "0", + "backward": "1", + "both": "2", + "history": "3" + } + try: + return_string += a[direction] + except: + return_string += str(direction) + return_string += "J" + return return_string + +erase_in_display_from_cursor = erase_in_display +ed = erase_in_display + +def erase_in_line(direction): + return_string = "\u001b[" + a = { + "forward": "0", + "backward": "1", + "both": "2" + } + try: + return_string += a[direction] + except: + return_string += direction + return_string += "K" + return return_string + +erase_in_line_from_cursor = erase_in_line +el = erase_in_line + def color(mode, ground, *args): return_string = "\u001b[" if mode == "bright": return_string += "1;" + mode = "normal" if ground == "fg": return_string += "3" @@ -41,15 +110,27 @@ def color(mode, ground, *args): bell = "\u0007" backspace = "\u0008" + escape = "\u001b" +esc = escape + csi = escape + "[" + reset = csi + "0m" normal = reset +reset_formatting = reset + bold = csi + "1m" +faint = csi +"2m" underline = csi + "4m" blink = csi + "5m" + invert = csi + "7m" +reverse_video = invert + strike = csi + "9m" +crossed_out = strike + normal_intensity = csi + "22m" #no_italic = csi + "23m" #not vidley supported no_blink = csi + "25m" diff --git a/examples/a.txt b/examples/a.txt new file mode 100644 index 0000000..1913cf0 --- /dev/null +++ b/examples/a.txt @@ -0,0 +1,2 @@ +]0;256 Color Terminal Palette + 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255 \ No newline at end of file diff --git a/examples/choose_line_to_erase.py b/examples/choose_line_to_erase.py new file mode 100644 index 0000000..e51b742 --- /dev/null +++ b/examples/choose_line_to_erase.py @@ -0,0 +1,8 @@ +import ansi_escape +a = 0 +while a < 10: + a += 1 + print("Hello World!!") + +a = input() +print(ansi_escape.move_cursor("up", str(a)) + ansi_escape.el("both")) \ No newline at end of file diff --git a/examples/erase.py b/examples/erase.py new file mode 100644 index 0000000..be99cb7 --- /dev/null +++ b/examples/erase.py @@ -0,0 +1,2 @@ +import ansi_escape, os +print(ansi_escape.erase_from_cursor("2")) \ No newline at end of file diff --git a/examples/move_cursor.py b/examples/move_cursor.py new file mode 100644 index 0000000..c7dc9a5 --- /dev/null +++ b/examples/move_cursor.py @@ -0,0 +1,7 @@ +import keyboard, ansi_escape +print(ansi_escape.erase_from_cursor("both")) +keyboard.add_hotkey('up', ansi_escape.cursor, args=('up')) +keyboard.add_hotkey('down', ansi_escape.cursor, args=('down')) +keyboard.add_hotkey('left', ansi_escape.cursor, args=('backward')) +keyboard.add_hotkey('right', ansi_escape.cursor, args=('forward')) +keyboard.wait() \ No newline at end of file diff --git a/examples/palette.py b/examples/palette.py new file mode 100644 index 0000000..d7d90bc --- /dev/null +++ b/examples/palette.py @@ -0,0 +1,9 @@ +import ansi_escape +color = 0 +print(ansi_escape.escape + "]0;256 Color Terminal Palette" + ansi_escape.bell) +while color <= 255: + color = str(color) + print(ansi_escape.color("256", "bg", color) + " " + " " * ( len(color) - 2 ) + color + " ", end="") + color = int(color) + color += 1 +input() \ No newline at end of file diff --git a/examples/random_cursor_positions.py b/examples/random_cursor_positions.py new file mode 100644 index 0000000..c61b615 --- /dev/null +++ b/examples/random_cursor_positions.py @@ -0,0 +1,4 @@ +import ansi_escape, random +while 1: + print(ansi_escape.set_cursor(row=str(random.randint(1,20)), column=str(random.randint(1,20))) + ansi_escape.color("normal", "bg", "red") + " " + ansi_escape.reset) + input() \ No newline at end of file diff --git a/examples/test.py b/examples/test.py index 70033dc..5ebee2d 100644 --- a/examples/test.py +++ b/examples/test.py @@ -6,4 +6,8 @@ while a < 256: a += 1 print(ansi_escape.escape + "]0;Demo" + ansi_escape.escape + "\\") +input() +print("hello" + ansi_escape.move_cursor("up", "2")) +input() +print(ansi_escape.erase_from_cursor("forward")) input() \ No newline at end of file diff --git a/readme.md b/readme.md index 881d180..3ef13e9 100644 --- a/readme.md +++ b/readme.md @@ -4,4 +4,6 @@ Features: SGR (Select Graphic Rendition) SGR foreground and background color in 4-bit color, 8-bit color and 24-bit color SGR text formating like; underline, overline and bold text -Easy way to use the bell character \ No newline at end of file +Easy way to use the bell character +Move cursor +Clear parts of screen or line \ No newline at end of file