Jq: Color encoded json via jq -C "." does not work on Windows 10 console

Created on 5 Aug 2016  路  5Comments  路  Source: stedolan/jq

Windows 10 Console does support ANSI escape sequences However the Color coded json appears to have this for the output.

C:\Users\swami>curl "https://api.github.com/repos/stedolan/jq/commits?per_page=5" | jq -C ".[0]"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 17549 100 17549 0 0 99k 0 --:--:-- --:--:-- --:--:-- 99k
{
"sha": "0b8218515eabf1a967eba0dbcc7a0e5ae031a509",
"commit": {
"author": {
"name": "Nicolas Williams",
"email": "[email protected]",
"date": "2016-03-21T22:43:01Z"
},
"committer": {
"name": "Nicolas Williams",
"email": "[email protected]",
"date": "2016-03-21T23:59:52Z"
},
"message": "Disable appveyor build for now",
"tree": {
"sha": "4920056437c4f768f24b2771309eddac55f55645",
"url": "https://api.github.com/repos/stedolan/jq/git/trees/4920056437c4f768f24b2771309eddac55f55645"
},
"url": "https://api.github.com/repos/stedolan/jq/git/commits/0b8218515eabf1a967eba0dbcc7a0e5ae031a509",
"comment_count": 1
},
"url": "https://api.github.com/repos/stedolan/jq/commits/0b8218515eabf1a967eba0dbcc7a0e5ae031a509",
"html_url": "https://github.com/stedolan/jq/commit/0b8218515eabf1a967eba0dbcc7a0e5ae031a509",
"comments_url": "https://api.github.com/repos/stedolan/jq/commits/0b8218515eabf1a967eba0dbcc7a0e5ae031a509/comments",
"author": {
"login": "nicowilliams",
"id": 604851,
"avatar_url": "https://avatars.githubusercontent.com/u/604851?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/nicowilliams",
"html_url": "https://github.com/nicowilliams",
"followers_url": "https://api.github.com/users/nicowilliams/followers",
"following_url": "https://api.github.com/users/nicowilliams/following{/other_user}",
"gists_url": "https://api.github.com/users/nicowilliams/gists{/gist_id}",
"starred_url": "https://api.github.com/users/nicowilliams/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/nicowilliams/subscriptions",
"organizations_url": "https://api.github.com/users/nicowilliams/orgs",
"repos_url": "https://api.github.com/users/nicowilliams/repos",
"events_url": "https://api.github.com/users/nicowilliams/events{/privacy}",
"received_events_url": "https://api.github.com/users/nicowilliams/received_events",
"type": "User",
"site_admin": false
},
"committer": {
"login": "nicowilliams",
"id": 604851,
"avatar_url": "https://avatars.githubusercontent.com/u/604851?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/nicowilliams",
"html_url": "https://github.com/nicowilliams",
"followers_url": "https://api.github.com/users/nicowilliams/followers",
"following_url": "https://api.github.com/users/nicowilliams/following{/other_user}",
"gists_url": "https://api.github.com/users/nicowilliams/gists{/gist_id}",
"starred_url": "https://api.github.com/users/nicowilliams/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/nicowilliams/subscriptions",
"organizations_url": "https://api.github.com/users/nicowilliams/orgs",
"repos_url": "https://api.github.com/users/nicowilliams/repos",
"events_url": "https://api.github.com/users/nicowilliams/events{/privacy}",
"received_events_url": "https://api.github.com/users/nicowilliams/received_events",
"type": "User",
"site_admin": false
},
"parents": [
{
"sha": "1a8a5ee00078aa5bd977f575e3e5ecad4e467487",
"url": "https://api.github.com/repos/stedolan/jq/commits/1a8a5ee00078aa5bd977f575e3e5ecad4e467487",
"html_url": "https://github.com/stedolan/jq/commit/1a8a5ee00078aa5bd977f575e3e5ecad4e467487"
}
]
}

All 5 comments

this could be a quick fix: https://github.com/mattn/ansicolor-w32.c

The fix for this is actually simpler than pulling in a library to parse sequences -- using SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING will let ConHost handle sequences.

https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

@LPGhatguy how new is that?

Pretty sure it was the Anniversary Update in August of 2016. It's possible to do error checking on the GetConsoleMode/SetConsoleMode calls so that older versions of Windows won't be affected.

Thanks @LPGhatguy and @flying-sheep. I think we might take both approaches.

Was this page helpful?
0 / 5 - 0 ratings