A text editor
Now we want to turn our text viewer in a proper editor. I guess it's the natural progression for this kind of things. Not to mention that our guide is called "Build a text editor", not "Build a text viewer". Let's not forget that.
Let's start by handling more keypresses in the processKeypress()
function.
We add new switch prongs for Backspace, Del and Enter:
Editor.zig: processKeypress()
.backspace, .ctrl_h, .del => {
if (k == .del) {
e.moveCursorWithKey(.right);
}
try e.deleteChar();
e.doCwant(.set);
},
.enter => try e.insertNewLine(),
We also change our else
branch to handle characters to be inserted. We only
handle Tab and printable characters, for now.
Editor.zig: processKeypress()
else => {
const c = @intFromEnum(k);
if (k == .tab or asc.isPrint(c)) {
try e.insertChar(c);
e.doCwant(.set);
}
},
There is a new constant to set:
Editor.zig
const asc = std.ascii;
And new functions to implement:
insertChar
will insert a character at cursor positindeleteChar
will delete the character on the left of the cursorinsertNewLine
will start editing a new line after the current one