kble/src/scaling.zig

48 lines
1.5 KiB
Zig

// SPDX-License-Identifier: MIT
// Copyright (c) 2021 KikooDX
// This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is
// MIT licensed. The MIT license requires this copyright notice to be
// included in all copies and substantial portions of the software.
//! Operates on a scale value used by drawing code.
const std = @import("std");
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
const conf = @import("conf.zig");
const Parameter = @import("parameter.zig");
pub const scale_type = u8;
const scale_min = 3;
const scale_max = maxInt(scale_type);
pub fn scale_reset(current_scale: scale_type, arg: Parameter.buffer_type) scale_type {
return conf.default_scaling_level;
}
pub fn scale_up(current_scale: scale_type, arg: Parameter.buffer_type) scale_type {
if (@intCast(u32, current_scale) + @intCast(u32, arg) < @intCast(u32, scale_max)) {
return current_scale + @intCast(u8, arg);
} else return scale_max;
}
pub fn scale_down(current_scale: scale_type, arg: Parameter.buffer_type) scale_type {
if (@intCast(i32, current_scale) - @intCast(i32, arg) > @intCast(i32, scale_min)) {
return current_scale - @intCast(u8, arg);
} else return scale_min;
}
test "scale reset" {
expect(scale_reset(42, 1) == conf.default_scaling_level);
}
test "scale up" {
expect(scale_up(42, 1) == 43);
expect(scale_up(scale_max - 4, 42) == scale_max);
}
test "scale down" {
expect(scale_down(42, 1) == 41);
expect(scale_down(scale_min + 2, 42) == scale_min);
}