X-Git-Url: http://git.vanrenterghem.biz/elgato-keylight-script.git/blobdiff_plain/febb52dde59e5e3e9854427f6ec815a68045b88c..fd2d087d6958d2475de4b8c18fda0cab93092bbd:/keylights.sh?ds=sidebyside diff --git a/keylights.sh b/keylights.sh index 352e336..1dab511 100755 --- a/keylights.sh +++ b/keylights.sh @@ -15,6 +15,7 @@ declare target='.' declare format="json" declare -A lights declare lights_json +declare simple_json declare flat_json declare call='curl --silent --show-error --location --header "Accept: application/json" --request' declare devices="/elgato/lights" @@ -51,14 +52,15 @@ Available actions: status Get state of lights on Turn all lights on off Turn all lights off - temperature Set temperature level (260-470) + temperature Set temperature level (260-470) brightness Set brightness level (0-100) increase Increases brightness by 10 decrease Decreases brightness by 10 Available formats: json Renders output as JSON (default) - flat Renders output as flattened JSON with .(dot) notation JSON (default) + simple Renders output as JSON array of single level objects with subarrays as .(dot) notation JSON + flat Renders output as fully flattened single level JSON with .(dot) notation JSON html Renders output as basic html table csv Renders output as csv table Renders output as a printed table @@ -142,51 +144,75 @@ produce_json() { t=$(eval echo "'[.[] | select($target)]'") lights_json=$(echo "${lights[@]}" | jq -c -s "$t") + simple_json=$(echo "${lights_json}" | jq -c '.[] | reduce ( tostream | select(length==2) | .[0] |= [join(".")] ) as [$p,$v] ({}; setpath($p; $v)) ') + simple_json=$(echo "${simple_json}" | jq -c -s '.') # slurp it to make it an array + flat_json=$(echo "${lights_json}" | jq -c -s '.[] | reduce ( tostream | select(length==2) | .[0] |= [join(".")] ) as [$p,$v] ({}; setpath($p; $v)) ') + } output() { - data=${1-} - type=${2-"$format"} # Mange user requested output format case $format in - json | flat) print_json "$data" ;; - table) print_json "$data" ;; - csv) print_csv "$data" ;; - pair) print_pair "$data" ;; - html) print_html "$data" ;; - -?*) die "Unknown output format (-f/--format): $type" ;; + json) print_json "$lights_json" ;; + simple) print_json "$simple_json" ;; + flat) print_json "$flat_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))' - 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=" +
${d//,/<\/th> | } |
---|---|
${d//,/ | } |