From b20e67ecff1be3729286417b09a8db5e338af469 Mon Sep 17 00:00:00 2001 From: Chris Burgess Date: Tue, 22 Aug 2023 22:23:22 -0400 Subject: [PATCH] Switch --fixed indexing to 1 based --- README.md | 8 ++++---- src/main.zig | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8055d5a..5a5323d 100644 --- a/README.md +++ b/README.md @@ -75,13 +75,13 @@ Evaluate changes relative to the first number (default): ↑ 300% 4x [ 1 → 4 ] ``` -Or choose a different reference point (zero-based): +Or choose a different reference point (one-based): ```sh ❯ pc 1 2 3 4 -f 2 -↓ -66.7% 0.33x [ 3 → 1 ] -↓ -33.3% 0.67x [ 3 → 2 ] -↑ 33.3% 1.33x [ 3 → 4 ] +↓ -50% 0.50x [ 2 → 1 ] +↑ 50% 1.50x [ 2 → 3 ] +↑ 100% 2x [ 2 → 4 ] ``` Or index from the end of the series with negative numbers: diff --git a/src/main.zig b/src/main.zig index 909aea8..a44cbd9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -22,9 +22,9 @@ const usage_text: []const u8 = \\ -v, --version : Show version information and exit. \\ -d, --delimiters : Specify extra delimiters (defaults: " \t\n\r|,;"). \\ Example: echo "1,2,3" | pc -d "," - \\ -f, --fixed [N] : Changes are relative to Nth number (default: 0). + \\ -f, --fixed [N] : Changes are relative to Nth number (default: 1). \\ Examples: - \\ echo "1,2,3" | pc -f 1 (second element) + \\ echo "1,2,3" | pc -f 2 (second element) \\ echo "1,2,3" | pc -f -1 (last element) \\ -r, --raw : Show numbers in raw form (e.g. 1000000 instead of 1MiB). \\ --[no-]color : Enable/disable color output (default: auto). @@ -322,7 +322,7 @@ const Maxes = struct { const ComparisonTarget = union(enum) { Moving, - Fixed: i64, + Fixed: i64, // 1 based, 0 and 1 are the same }; pub fn main() !void { @@ -449,7 +449,7 @@ pub fn main() !void { .Fixed => |index| blk: { // account for negative indices and clamp const nums_len: i64 = @intCast(nums.items.len); - const adjusted_index = if (index < 0) nums_len + index else index; + const adjusted_index = if (index < 0) nums_len + index else index - 1; // 1 based const final_idx: usize = @intCast(std.math.clamp(adjusted_index, 0, nums_len - 1)); break :blk final_idx; },