JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
when clearing, careful of cursor being after right-most column
[watch-my-terminal.git] / terminal.coffee
index 751a53a..913d874 100644 (file)
@@ -83,18 +83,20 @@ class Terminal
                                when '\x0d' # cr
                                        @x = 0
                                when '\x08' # backspace
-                                       if @x > 0
+                                       if @x is 0
+                                               @x = @width - 1
+                                               if @y > 0
+                                                       @y -= 1
+                                       else
                                                @x -= 1
-                                               @text[@y][@x] = ' '
-                                               # should this set the attribute too?
                                when '\x0a', '\x0b' # lf, vertical tab (same thing)
                                        @wrap_to_next_line()
                                else
+                                       if @x >= @width
+                                               @wrap_to_next_line()
                                        @text[@y][@x] = c
                                        @attributes[@y][@x] = @a
                                        @x += 1
-                                       if @x is @width
-                                               @wrap_to_next_line()
                return
 
        set_attribute_bits: (mask, value) ->
@@ -167,7 +169,12 @@ class Terminal
                                        @text[@y][i] = ' '
                                        @attributes[@y][i] = @a
                        when '1' # erase to left
-                               for i in [0..@x]
+                               # @x can equal @width (after printing to right-most column)
+                               if @x < @width
+                                       max = @x
+                               else
+                                       max = @width - 1
+                               for i in [0..max]
                                        @text[@y][i] = ' '
                                        @attributes[@y][i] = @a
                        when '2' # erase whole line