Skip to content

Instantly share code, notes, and snippets.

@jjsullivan5196
Created September 7, 2020 06:36
Show Gist options
  • Save jjsullivan5196/a8f923473f923e7f4d31a6d03c05b14a to your computer and use it in GitHub Desktop.
Save jjsullivan5196/a8f923473f923e7f4d31a6d03c05b14a to your computer and use it in GitHub Desktop.
const ROWS: u8 = 4;
const COLS: u8 = 15;
const KEYS = ROWS * COLS;
pub const Key = struct {
label: [:0]const u8,
width: u8,
col: u8 = 0,
row: u8 = 0,
};
pub const keys = calc_key_positions(.{
Key{ .label = "a", .width = 1 },
Key{ .label = "a", .width = 3 },
Key{ .label = "a", .width = 1 },
Key{ .label = "a", .width = 2 },
Key{ .label = "a", .width = 1 },
Key{ .label = "a", .width = 4 },
Key{ .label = "a", .width = 1 },
});
// Take a list of keys, determine column/row for each key based on width of preceding keys.
pub fn calc_key_positions(...) []Key {
var col: u8 = 0;
var row: u8 = 0;
for (...) |*key| {
key.*.col = col;
key.*.row = row;
col += key.*.width;
if(col >= COLS) {
col = 0;
row += 1;
}
}
return ...;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment