Skip to content

Commit

Permalink
Merge pull request ccxt#12315 from samgermain/test-sh
Browse files Browse the repository at this point in the history
add multilang.sh
  • Loading branch information
frosty00 authored Mar 12, 2022
2 parents 6158107 + e6c146b commit 801e78a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 28 deletions.
54 changes: 26 additions & 28 deletions examples/js/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ function printUsage () {
//-----------------------------------------------------------------------------

const printHumanReadable = (exchange, result) => {

if (Array.isArray (result) || table) {
if (!no_table && Array.isArray (result) || table) {
result = Object.values (result)
let arrayOfObjects = (typeof result[0] === 'object')

Expand All @@ -158,33 +157,32 @@ const printHumanReadable = (exchange, result) => {
log (object)
})

if (!no_table)
if (arrayOfObjects || table && Array.isArray (result)) {
log (result.length > 0 ? asTable (result.map (element => {
let keys = Object.keys (element)
delete element['info']
keys.forEach (key => {
if (!iso8601)
return element[key]
try {
const iso8601 = exchange.iso8601 (element[key])
if (iso8601.match (/^20[0-9]{2}[-]?/))
element[key] = iso8601
else
throw new Error ('wrong date')
} catch (e) {
return element[key]
}
})
return element
})) : result)
log (result.length, 'objects');
} else {
console.dir (result, { depth: null })
log (result.length, 'objects');
}
if (arrayOfObjects || table && Array.isArray (result)) {
log (result.length > 0 ? asTable (result.map (element => {
let keys = Object.keys (element)
delete element['info']
keys.forEach (key => {
if (!iso8601)
return element[key]
try {
const iso8601 = exchange.iso8601 (element[key])
if (iso8601.match (/^20[0-9]{2}[-]?/))
element[key] = iso8601
else
throw new Error ('wrong date')
} catch (e) {
return element[key]
}
})
return element
})) : result)
log (result.length, 'objects');
} else {
console.dir (result, { depth: null })
log (result.length, 'objects');
}
} else {
console.dir (result, { depth: null})
console.dir (result, { depth: null, maxArrayLength: null })
}
}

Expand Down
1 change: 1 addition & 0 deletions examples/py/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def print_usage():
method = getattr(exchange, argv.method)
# if it is a method, call it
if callable(method):
print(f"{argv.exchange_id}.{argv.method}({','.join(args)})")
result = method(*args)
else: # otherwise it's a property, print it
result = method
Expand Down
97 changes: 97 additions & 0 deletions multilang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash

function usage() {
echo "usage: $0 [-clsh] exchange method [...args]"
echo " -c Number of lines to trim off the top and bottom of output"
echo " -l View in less editor"
echo " -s Remove special characters"
echo " -h Display help"
exit 1
}

if [[ $# < 1 ]]; then
usage
fi


cliFolder='./examples'

jsCli="${cliFolder}/js/cli.js"
pythonCli="${cliFolder}/py/cli.py"
phpCli="${cliFolder}/php/cli.php"

useLess=false
removeSpecial=false
numLines=0

function display () {
# Displays output in a less window or just to stdout
if ${useLess}; then
less -S -R
else
tee
fi
}

function removeSpecial {
# Removes special characters
if ${removeSpecial}; then
sed -e 's/\[[0-9]\{1,2\}m//g'
else
tee
fi
}

function condense {
# Trims output down to a set number of lines on the top and the bottom
if [ ${numLines} -gt 0 ]; then
awk 'NF' | awk -v head=${numLines} -v tail=${numLines} 'FNR<=head
{lines[FNR]=$0}
END{
for (i=FNR-tail+1; i<=FNR; i++) print lines[i]
}'
else
tee
fi
}

function removeAndColorLines {
sed -E -e '/.*(iteration|Array|^202.*|^$)/d' -e 's/ / /g' -e "s/(.*)/$(tput setaf $color)\1$(tput sgr0)/"
}

function writeOutput() {
local interpretter="$1"
local path="$2"
local args="$3"
$interpretter "$path" $args | removeSpecial | removeAndColorLines $color
}

# Loop through command line arguments
while getopts 'hc:sl' flag; do
case "${flag}" in
h) usage ;;
c) numLines="${OPTARG}" ;;
s) removeSpecial=true ;;
l) useLess=true ;;
*) usage ;;
esac
done

shift $((OPTIND-1))

args="$@"

color=3
jsOutput=$(writeOutput node $jsCli "--no-table $args")
((color++))

pythonOutput=$(writeOutput python3 $pythonCli "$args")
((color++))

# python has the shortest output
length=$(wc -l <<< "$pythonOutput")

phpOutput=$(writeOutput php $phpCli "$args")
((color++))

paste <(echo "$jsOutput") <(echo "$phpOutput") <(echo "$pythonOutput") | column -s $'\t' -t | head -n $length | condense | display

0 comments on commit 801e78a

Please sign in to comment.