replace vi_mode with a bool

This commit is contained in:
Kinzie 2024-04-16 06:17:32 +01:00
parent 7cc6fbaee3
commit 56496e2bdb
4 changed files with 20 additions and 25 deletions

View File

@ -16,8 +16,3 @@ pub const Input = enum {
login, login,
password, password,
}; };
pub const ViMode = enum {
normal,
insert,
};

View File

@ -158,7 +158,7 @@ pub fn main() !void {
defer password.deinit(); defer password.deinit();
var active_input = config.default_input; var active_input = config.default_input;
var vi_mode: ViMode = if (config.vi_mode) .normal else .insert; var insert_mode: bool = if (config.vi_mode) false else true;
// Load last saved username and desktop selection, if any // Load last saved username and desktop selection, if any
if (config.load) { if (config.load) {
@ -187,11 +187,11 @@ pub fn main() !void {
password.position(coordinates.x, coordinates.y + 6, coordinates.visible_length); password.position(coordinates.x, coordinates.y + 6, coordinates.visible_length);
switch (active_input) { switch (active_input) {
.session => desktop.handle(null, vi_mode), .session => desktop.handle(null, insert_mode),
.login => login.handle(null, vi_mode) catch { .login => login.handle(null, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
.password => password.handle(null, vi_mode) catch { .password => password.handle(null, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
} }
@ -291,11 +291,11 @@ pub fn main() !void {
// If the user entered a wrong password 10 times in a row, play a cascade animation, else update normally // If the user entered a wrong password 10 times in a row, play a cascade animation, else update normally
if (auth_fails < 10) { if (auth_fails < 10) {
switch (active_input) { switch (active_input) {
.session => desktop.handle(null, vi_mode), .session => desktop.handle(null, insert_mode),
.login => login.handle(null, vi_mode) catch { .login => login.handle(null, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
.password => password.handle(null, vi_mode) catch { .password => password.handle(null, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
} }
@ -387,7 +387,7 @@ pub fn main() !void {
} }
if (config.vi_mode) { if (config.vi_mode) {
const label_txt = if (vi_mode == .normal) lang.normal else lang.insert; const label_txt = if (!insert_mode) lang.normal else lang.insert;
buffer.drawLabel(label_txt, buffer.box_x, buffer.box_y - 1); buffer.drawLabel(label_txt, buffer.box_x, buffer.box_y - 1);
} }
@ -459,8 +459,8 @@ pub fn main() !void {
switch (event.key) { switch (event.key) {
termbox.TB_KEY_ESC => { termbox.TB_KEY_ESC => {
if (config.vi_mode and vi_mode != .normal) { if (config.vi_mode and insert_mode) {
vi_mode = .normal; insert_mode = false;
update = true; update = true;
} }
}, },
@ -564,7 +564,7 @@ pub fn main() !void {
} }
}, },
else => { else => {
if (vi_mode == .normal) { if (!insert_mode) {
switch (event.ch) { switch (event.ch) {
'k' => { 'k' => {
active_input = switch (active_input) { active_input = switch (active_input) {
@ -583,7 +583,7 @@ pub fn main() !void {
continue; continue;
}, },
'i' => { 'i' => {
vi_mode = .insert; insert_mode = true;
update = true; update = true;
continue; continue;
}, },
@ -592,11 +592,11 @@ pub fn main() !void {
} }
switch (active_input) { switch (active_input) {
.session => desktop.handle(&event, vi_mode), .session => desktop.handle(&event, insert_mode),
.login => login.handle(&event, vi_mode) catch { .login => login.handle(&event, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
.password => password.handle(&event, vi_mode) catch { .password => password.handle(&event, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.setText(lang.err_alloc);
}, },
} }

View File

@ -118,7 +118,7 @@ pub fn crawl(self: *Desktop, path: []const u8, display_server: DisplayServer) !v
} }
} }
pub fn handle(self: *Desktop, maybe_event: ?*termbox.tb_event, vi_mode: ViMode) void { pub fn handle(self: *Desktop, maybe_event: ?*termbox.tb_event, insert_mode: bool) void {
if (maybe_event) |event| blk: { if (maybe_event) |event| blk: {
if (event.type != termbox.TB_EVENT_KEY) break :blk; if (event.type != termbox.TB_EVENT_KEY) break :blk;
@ -126,7 +126,7 @@ pub fn handle(self: *Desktop, maybe_event: ?*termbox.tb_event, vi_mode: ViMode)
termbox.TB_KEY_ARROW_LEFT, termbox.TB_KEY_CTRL_H => self.goLeft(), termbox.TB_KEY_ARROW_LEFT, termbox.TB_KEY_CTRL_H => self.goLeft(),
termbox.TB_KEY_ARROW_RIGHT, termbox.TB_KEY_CTRL_L => self.goRight(), termbox.TB_KEY_ARROW_RIGHT, termbox.TB_KEY_CTRL_L => self.goRight(),
else => { else => {
if (vi_mode == .normal) { if (!insert_mode) {
switch (event.ch) { switch (event.ch) {
'h' => self.goLeft(), 'h' => self.goLeft(),
'l' => self.goRight(), 'l' => self.goRight(),

View File

@ -47,7 +47,7 @@ pub fn position(self: *Text, x: u64, y: u64, visible_length: u64) void {
self.visible_length = visible_length; self.visible_length = visible_length;
} }
pub fn handle(self: *Text, maybe_event: ?*termbox.tb_event, vi_mode: ViMode) !void { pub fn handle(self: *Text, maybe_event: ?*termbox.tb_event, insert_mode: bool) !void {
if (maybe_event) |event| blk: { if (maybe_event) |event| blk: {
if (event.type != termbox.TB_EVENT_KEY) break :blk; if (event.type != termbox.TB_EVENT_KEY) break :blk;
@ -56,7 +56,7 @@ pub fn handle(self: *Text, maybe_event: ?*termbox.tb_event, vi_mode: ViMode) !vo
termbox.TB_KEY_ARROW_RIGHT => self.goRight(), termbox.TB_KEY_ARROW_RIGHT => self.goRight(),
termbox.TB_KEY_DELETE => self.delete(), termbox.TB_KEY_DELETE => self.delete(),
termbox.TB_KEY_BACKSPACE, termbox.TB_KEY_BACKSPACE2 => { termbox.TB_KEY_BACKSPACE, termbox.TB_KEY_BACKSPACE2 => {
if (vi_mode == .insert) { if (insert_mode) {
self.backspace(); self.backspace();
} else { } else {
self.goLeft(); self.goLeft();
@ -65,7 +65,7 @@ pub fn handle(self: *Text, maybe_event: ?*termbox.tb_event, vi_mode: ViMode) !vo
termbox.TB_KEY_SPACE => try self.write(' '), termbox.TB_KEY_SPACE => try self.write(' '),
else => { else => {
if (event.ch > 31 and event.ch < 127) { if (event.ch > 31 and event.ch < 127) {
if (vi_mode == .insert) { if (insert_mode) {
try self.write(@intCast(event.ch)); try self.write(@intCast(event.ch));
} else { } else {
switch (event.ch) { switch (event.ch) {