Skip to content

Commit

Permalink
Merge pull request #10 from cgbur/reverse
Browse files Browse the repository at this point in the history
Add reverse option
  • Loading branch information
cgbur authored Dec 27, 2023
2 parents 11f5640 + 484b0d9 commit b39ca72
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v2
- run: zig build
- run: PATH=$PATH:zig-out/bin python3 run-tests.py
- run: python3 run-tests.py

build:
needs: [test, lint, cli-test]
Expand Down
7 changes: 7 additions & 0 deletions run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def print_diff(expected_output, actual_output):


def main():
# modify path to use zig-out/bin/pc first
os.environ["PATH"] = os.path.abspath("zig-out/bin") + ":" + os.environ["PATH"]

# print which pc
out = subprocess.check_output(["which", "pc"])
print(f"Using pc: {out.decode('utf-8').strip()}")

test_dir = "tests"
passed = 0
failed = 0
Expand Down
14 changes: 11 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ArrayList = std.ArrayList;
const ColorConfig = std.io.tty.Config;
const Color = std.io.tty.Color;

const version = "1.3.2";
const version = "1.4.0";
const default_delims = " \t\n\r|,;:";
const usage_text: []const u8 =
\\Usage:
Expand All @@ -26,7 +26,8 @@ const usage_text: []const u8 =
\\ Examples:
\\ 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).
\\ -r, --reverse : Reverse the order of the numbers.
\\ --raw : Show numbers in raw form (e.g. 1000000 instead of 1MiB).
\\ --[no-]color : Enable/disable color output (default: auto).
\\ --format <f> : Specify output format (options: json, csv).
\\ -w, --warnings : Show warnings for invalid numbers (default: false).
Expand Down Expand Up @@ -352,6 +353,7 @@ pub fn main() !void {
}
var target: ComparisonTarget = .Moving;
var raw = false;
var reverse = false;
var color: ColorChoice = .Auto;
var format: Format = .Default;
var print_warnings: bool = false;
Expand Down Expand Up @@ -381,8 +383,10 @@ pub fn main() !void {
continue;
}
}
} else if (std.mem.eql(u8, arg, "-r") or std.mem.eql(u8, arg, "--raw")) {
} else if (std.mem.eql(u8, arg, "--raw")) {
raw = true;
} else if (std.mem.eql(u8, arg, "-r") or std.mem.eql(u8, arg, "--reverse")) {
reverse = true;
} else if (std.mem.eql(u8, arg, "-d") or std.mem.eql(u8, arg, "--delimiters")) {
arg_i += 1;
if (arg_i >= args.len) {
Expand Down Expand Up @@ -442,6 +446,10 @@ pub fn main() !void {
return std.process.exit(1);
}

if (reverse) {
std.mem.reverse(f32, nums.items);
}

// construct the base type, collecting all the rows
var rows: ArrayList(Row) = ArrayList(Row).init(allocator);
defer rows.deinit();
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/reverse-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pc 1 2 3 4 5 -r
↓ -20% 0.80x [ 5 → 4 ]
↓ -25% 0.75x [ 4 → 3 ]
↓ -33.3% 0.67x [ 3 → 2 ]
↓ -50% 0.50x [ 2 → 1 ]
5 changes: 5 additions & 0 deletions tests/reverse-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pc 1 2 3 4 5 --reverse
↓ -20% 0.80x [ 5 → 4 ]
↓ -25% 0.75x [ 4 → 3 ]
↓ -33.3% 0.67x [ 3 → 2 ]
↓ -50% 0.50x [ 2 → 1 ]

0 comments on commit b39ca72

Please sign in to comment.