json) print_json "$lights_json" ;;
simple) print_json "$simple_json" ;;
flat) print_json "$flat_json" ;;
- table) print_json "$simple_json" ;;
- csv) print_csv "$simple_json" ;;
- pair) print_pair "$simple_json" ;;
- html) print_html "$simple_json" ;;
+ table) print_structured '@tsv' ;;
+ csv) print_structured '@csv' ;;
+ pair) print_structured 'pairs' ;;
+ html) print_html ;;
-?*) die "Unknown output format (-f/--format): $format" ;;
esac
}
print_json() {
- # TODO: Evaluate adding jq filtering as filter argument
- query=""
-
- # Deconstruct json and assemble in flattened with .(dot) notation
- if [[ $format == "flat" ]]; then
- #query='reduce ( tostream | select(length==2) | .[0] |= [join(".")] ) as [$p,$v] ({}; setpath($p; $v))'
- query='.'
- else
- query='.'
- fi
# Manage pretty printing
if [[ $pretty -eq 1 ]]; then
- echo "${1-}" | jq "$query"
+ echo "${1-}" | jq '.'
else
- echo "${1-}" | jq -c -M "$query"
+ echo "${1-}" | jq -c -M '.'
fi
exit 0
}
-print_table() {
- bold=$(tput bold)
- normal=$(tput sgr0)
- message='
-
-'
- die "To be implemented"
+print_structured() {
+ pp=${2-$pretty}
+ # Handle csv and table printing
+ query="(.[0] | keys_unsorted | map(ascii_upcase)), (.[] | [.[]])|${1-@csv}"
+
+ # Handle printing as key value pairs
+ if [[ ${1} == 'pairs' ]]; then
+ query='.[] | "--------------",(to_entries[] | [.key, "=", .value] | @tsv)'
+ fi
+
+ # Manage pretty printing
+ if [[ $pp -eq 1 ]]; then
+ echo "${simple_json}" | jq --raw-output "$query" | column -t -s$'\t' | sed -e 's/"//g'
+ else
+ echo "${simple_json}" | jq -r "$query"
+ fi
+}
+
+print_html() {
+ data=$(print_structured '@csv' 1)
+
+ html="
+ <table>
+ $(
+ print_header=true
+ while read d; do
+ if $print_header; then
+ echo "<tr><th>${d//,/<\/th><th>}</th></tr>"
+ print_header=false
+ continue
+ fi
+ echo "<tr><td>${d//,/</td><td>}</td></tr>"
+ done <<<"${data}"
+ )
+ </table>"
+ echo "$html"
}
set_state() {
find_lights
# Fail if we cannot find lights
-[[ ${#lights[@]} -eq 0 ]] && die "No lights found" 2
+[[ ${#lights[@]} -eq 0 ]] && die "No lights found"
produce_json