mirror of https://github.com/fairyglade/ly.git
positionWidgets: Less integer overflows possible
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
parent
d00780d6ce
commit
84950136c9
|
|
@ -269,7 +269,8 @@ pub fn runEventLoop(
|
|||
}
|
||||
}
|
||||
|
||||
try TerminalBuffer.presentBuffer();
|
||||
// We don't care about present errors here
|
||||
TerminalBuffer.presentBuffer() catch {};
|
||||
}
|
||||
|
||||
if (inactivity_event_fn) |inactivity_fn| {
|
||||
|
|
|
|||
|
|
@ -129,8 +129,10 @@ fn draw(self: *Lua) void {
|
|||
cell.put(x, y) catch {};
|
||||
if (self.lua_str) |str|
|
||||
for (str, 0..) |c, i| {
|
||||
const dwidth = @divFloor(self.width, 2);
|
||||
const dlen = @divFloor(str.len, 2);
|
||||
Cell.init(c, 0x00FFFFFF, 0).put(
|
||||
@divFloor(self.width, 2) - @divFloor(str.len, 2) + i,
|
||||
(if (dlen > dwidth) 0 else dwidth - dlen) + i,
|
||||
self.margin + 5,
|
||||
) catch {};
|
||||
};
|
||||
|
|
|
|||
22
src/main.zig
22
src/main.zig
|
|
@ -2244,26 +2244,34 @@ fn positionWidgets(ptr: *anyopaque) !void {
|
|||
bb_width = @max(bb_width, clock_text_len);
|
||||
}
|
||||
|
||||
const max_v_position: f32 = @floatFromInt(state.buffer.height - bb_height - 1);
|
||||
const max_h_position: f32 = @floatFromInt(state.buffer.width - bb_width - 1);
|
||||
const dheight = if (bb_height + 1 > state.buffer.height) 1 else state.buffer.height - bb_height - 1;
|
||||
const dwidth = if (bb_width + 1 > state.buffer.width) 1 else state.buffer.width - bb_width - 1;
|
||||
|
||||
const max_v_position: f32 = @floatFromInt(dheight);
|
||||
const max_h_position: f32 = @floatFromInt(dwidth);
|
||||
|
||||
bb_height = @min(bb_height, state.buffer.height - 2);
|
||||
bb_width = @min(bb_width, state.buffer.width - 2);
|
||||
|
||||
const v_space: f32 = @floatFromInt(state.buffer.height - bb_height);
|
||||
const v_position: usize = @intFromFloat(std.math.clamp(v_space * state.config.box_position_v, 1.0, max_v_position));
|
||||
const v_position: usize = @intFromFloat(if (max_v_position < 1.0) 1.0 else std.math.clamp(v_space * state.config.box_position_v, 1.0, max_v_position));
|
||||
const h_space: f32 = @floatFromInt(state.buffer.width - bb_width);
|
||||
const h_position: usize = @intFromFloat(std.math.clamp(h_space * state.config.box_position_h, 1.0, max_h_position));
|
||||
const h_position: usize = @intFromFloat(if (max_h_position < 1.0) 1.0 else std.math.clamp(h_space * state.config.box_position_h, 1.0, max_h_position));
|
||||
|
||||
if (state.config.bigclock != .none) {
|
||||
const cwidth = if (clock_text_len > bb_width) 0 else bb_width - clock_text_len;
|
||||
|
||||
state.bigclock_label.positionXY(TerminalBuffer.START_POSITION
|
||||
.addX(h_position + (bb_width - clock_text_len) / 2)
|
||||
.addX(h_position + cwidth / 2)
|
||||
.addY(v_position));
|
||||
}
|
||||
|
||||
const bwidth = if (state.box.width > bb_width) 0 else bb_width - state.box.width;
|
||||
const bheight = if (state.box.height > bb_height) 0 else bb_height - state.box.height;
|
||||
|
||||
state.box.positionXY(TerminalBuffer.START_POSITION
|
||||
.addX(h_position + (bb_width - state.box.width) / 2)
|
||||
.addY(v_position + (bb_height - state.box.height)));
|
||||
.addX(h_position + bwidth / 2)
|
||||
.addY(v_position + bheight));
|
||||
|
||||
state.info_line.label.positionY(state.box
|
||||
.childrenPosition());
|
||||
|
|
|
|||
Loading…
Reference in New Issue