Skip to content

Commit

Permalink
Merge pull request #6 from cgbur/one-based-index
Browse files Browse the repository at this point in the history
Switch --fixed indexing to 1 based
  • Loading branch information
cgbur authored Aug 23, 2023
2 parents 4b077d0 + b20e67e commit dbb706e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [ 32 ]
33.3% 1.33x [ 3 → 4 ]
↓ -50% 0.50x [ 2 → 1 ]
↑ 50% 1.50x [ 23 ]
100% 2x [ 2 → 4 ]
```

Or index from the end of the series with negative numbers:
Expand Down
8 changes: 4 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
},
Expand Down

0 comments on commit dbb706e

Please sign in to comment.