Skip to content

Commit

Permalink
gojq: Remove div operator
Browse files Browse the repository at this point in the history
Add intdiv function intead
  • Loading branch information
wader committed Sep 12, 2021
1 parent a34784d commit 1888bb2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dev/snippets.jq
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def duration:
def _number:
if . == 0 then 0
else
[ ( [ (recurse(if . > 0 then . div 60 else empty end) | . % 60)]
[ ( [ (recurse(if . > 0 then intdiv(.; 60) else empty end) | . % 60)]
| reverse
| .[1:]
| map(tostring | lpad("0"; 2))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/pmezard/go-difflib v1.0.0

// fork of github.com/itchyny/gojq
github.com/wader/gojq v0.12.1-0.20210822175929-fb8fcf3f6bd5
github.com/wader/gojq v0.12.1-0.20210824111828-c86c76d14b82
// fork of github.com/chzyer/readline
github.com/wader/readline v0.0.0-20210817095433-c868eb04b8b2
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354/go.mod h1:KSVJ
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/wader/gojq v0.12.1-0.20210822175929-fb8fcf3f6bd5 h1:1FQ0kEHeEfcn5inEE7LZQB04eVeahoKR+XKDS7ApXOQ=
github.com/wader/gojq v0.12.1-0.20210822175929-fb8fcf3f6bd5/go.mod h1:AmH/OqHVd1Ce1hurSMpGpL79EXPXzEJG3zvYspUmCA8=
github.com/wader/gojq v0.12.1-0.20210824111828-c86c76d14b82 h1:92eS+qJ29p9hzZmxxGUxQPOcJgctefer3XyP0rgIUpA=
github.com/wader/gojq v0.12.1-0.20210824111828-c86c76d14b82/go.mod h1:AmH/OqHVd1Ce1hurSMpGpL79EXPXzEJG3zvYspUmCA8=
github.com/wader/readline v0.0.0-20210817095433-c868eb04b8b2 h1:MGg7fsdEsoi7rattHGyU21wpOPeL3FonbUbJibpPBxc=
github.com/wader/readline v0.0.0-20210817095433-c868eb04b8b2/go.mod h1:jYXyt9wQg3DifxQ8FM5M/ZoskO23GIwmo05QLHtO9CQ=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
8 changes: 6 additions & 2 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def help:

def format: ._format? // null;

# integer division
# inspried by https://github.com/itchyny/gojq/issues/63#issuecomment-765066351
def intdiv($a; $b): ($a - ($a % $b)) / $b;

# valid jq identifer, start with alpha or underscore then zero or more alpha, num or underscore
def _is_ident: type == "string" and test("^[a-zA-Z_][a-zA-Z_0-9]*$");
# escape " and \
Expand Down Expand Up @@ -164,7 +168,7 @@ def table(colmap; render):
def number_to_bytes($bits):
def _number_to_bytes($d):
if . > 0 then
. % $d, (. div $d | _number_to_bytes($d))
. % $d, (intdiv(.; $d) | _number_to_bytes($d))
else
empty
end;
Expand All @@ -191,7 +195,7 @@ def from_radix($base; $table):
def to_radix($base; $table):
if . == 0 then "0"
else
( [ recurse(if . > 0 then . div $base else empty end) | . % $base]
( [ recurse(if . > 0 then intdiv(.; $base) else empty end) | . % $base]
| reverse
| .[1:]
| if $base <= ($table | length) then
Expand Down
4 changes: 2 additions & 2 deletions pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def _build_default_options:
unicode: (tty.is_terminal and env.CLIUNICODE != null),
raw: (tty.is_terminal | not),
# TODO: div 2 * 2 to get even number, nice or maybe not needed?
linebytes: (if tty.is_terminal then [((tty.width div 8) div 2) * 2, 4] | max else 16 end),
displaybytes: (if tty.is_terminal then [((tty.width div 8) div 2) * 2, 4] | max else 16 end),
linebytes: (if tty.is_terminal then [intdiv(intdiv(tty.width; 8); 2) * 2, 4] | max else 16 end),
displaybytes: (if tty.is_terminal then [intdiv(intdiv(tty.width; 8); 2) * 2, 4] | max else 16 end),
addrbase: 16,
sizebase: 10,
colors: (
Expand Down

0 comments on commit 1888bb2

Please sign in to comment.