mirror of
https://github.com/fairyglade/ly.git
synced 2025-12-20 19:24:53 +00:00
Refactor active_input field-jumping logic (#873)
Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/873 Reviewed-by: AnErrupTion <anerruption@disroot.org> Co-authored-by: radsammyt <radsammyt@noreply.codeberg.org> Co-committed-by: radsammyt <radsammyt@noreply.codeberg.org>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
const std = @import("std");
|
||||||
pub const Animation = enum {
|
pub const Animation = enum {
|
||||||
none,
|
none,
|
||||||
doom,
|
doom,
|
||||||
@@ -19,6 +20,26 @@ pub const Input = enum {
|
|||||||
session,
|
session,
|
||||||
login,
|
login,
|
||||||
password,
|
password,
|
||||||
|
|
||||||
|
/// Moves the current Input forwards by one entry. If `reverse`, then the Input
|
||||||
|
/// moves backwards. If `wrap` is true, then the entry will wrap back around
|
||||||
|
pub fn move(self: *Input, reverse: bool, wrap: bool) void {
|
||||||
|
const maxNum = @typeInfo(Input).@"enum".fields.len - 1;
|
||||||
|
const selfNum = @intFromEnum(self.*);
|
||||||
|
if (reverse) {
|
||||||
|
if (wrap) {
|
||||||
|
self.* = @enumFromInt(selfNum -% 1);
|
||||||
|
} else if (selfNum != 0) {
|
||||||
|
self.* = @enumFromInt(selfNum - 1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (wrap) {
|
||||||
|
self.* = @enumFromInt(selfNum +% 1);
|
||||||
|
} else if (selfNum != maxNum) {
|
||||||
|
self.* = @enumFromInt(selfNum + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ViMode = enum {
|
pub const ViMode = enum {
|
||||||
|
|||||||
38
src/main.zig
38
src/main.zig
@@ -938,37 +938,19 @@ pub fn main() !void {
|
|||||||
update = true;
|
update = true;
|
||||||
},
|
},
|
||||||
termbox.TB_KEY_CTRL_K, termbox.TB_KEY_ARROW_UP => {
|
termbox.TB_KEY_CTRL_K, termbox.TB_KEY_ARROW_UP => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(true, false);
|
||||||
.session, .info_line => .info_line,
|
|
||||||
.login => .session,
|
|
||||||
.password => .login,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
},
|
},
|
||||||
termbox.TB_KEY_CTRL_J, termbox.TB_KEY_ARROW_DOWN => {
|
termbox.TB_KEY_CTRL_J, termbox.TB_KEY_ARROW_DOWN => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(false, false);
|
||||||
.info_line => .session,
|
|
||||||
.session => .login,
|
|
||||||
.login, .password => .password,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
},
|
},
|
||||||
termbox.TB_KEY_TAB => {
|
termbox.TB_KEY_TAB => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(false, true);
|
||||||
.info_line => .session,
|
|
||||||
.session => .login,
|
|
||||||
.login => .password,
|
|
||||||
.password => .info_line,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
},
|
},
|
||||||
termbox.TB_KEY_BACK_TAB => {
|
termbox.TB_KEY_BACK_TAB => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(true, true);
|
||||||
.info_line => .password,
|
|
||||||
.session => .info_line,
|
|
||||||
.login => .session,
|
|
||||||
.password => .login,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
},
|
},
|
||||||
termbox.TB_KEY_ENTER => authenticate: {
|
termbox.TB_KEY_ENTER => authenticate: {
|
||||||
@@ -1125,20 +1107,12 @@ pub fn main() !void {
|
|||||||
if (!insert_mode) {
|
if (!insert_mode) {
|
||||||
switch (event.ch) {
|
switch (event.ch) {
|
||||||
'k' => {
|
'k' => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(true, false);
|
||||||
.session, .info_line => .info_line,
|
|
||||||
.login => .session,
|
|
||||||
.password => .login,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
'j' => {
|
'j' => {
|
||||||
active_input = switch (active_input) {
|
active_input.move(false, false);
|
||||||
.info_line => .session,
|
|
||||||
.session => .login,
|
|
||||||
.login, .password => .password,
|
|
||||||
};
|
|
||||||
update = true;
|
update = true;
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user