Created
January 3, 2025 06:18
-
-
Save Syakhisk/346ffa5d6f43677a35d3b678be12e2c3 to your computer and use it in GitHub Desktop.
jq interactive result using fx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./split-input.sh <json_file> | |
# <json_file>: Path to the JSON file. | |
output_script="./split-output.sh" | |
main() { | |
# Check dependencies | |
if ! command -v jq &>/dev/null; then | |
echo "Dependency 'jq' not found. Install it to continue." | |
exit 1 | |
fi | |
JSON_FILE=$1 | |
OUTPUT_FILE="/tmp/jq_output.json" | |
if [[ ! -f $JSON_FILE ]]; then | |
echo "File $JSON_FILE not found." | |
exit 1 | |
fi | |
# Ensure the output file exists | |
>"$OUTPUT_FILE" | |
# Start the input prompt loop | |
echo "Enter jq query below (press Ctrl+C to exit):" | |
while true; do | |
read -p "jq> " QUERY | |
if jq "$QUERY" "$JSON_FILE" >"$OUTPUT_FILE" 2>/dev/null; then | |
echo "Query executed successfully. Check the output in the other pane." | |
else | |
echo "Invalid jq query. Please try again." | |
fi | |
manage_output | |
done | |
} | |
manage_output() { | |
output_pid=$(pgrep -f "^$(which bash) ${output_script}$") | |
output_shell_pid=$(ps -o ppid= $output_pid) | |
if [[ -z $output_pid ]]; then | |
echo "Output script not running. Start it before running this script." | |
return | |
fi | |
pane_info=$(get_tmux_pane_info "$output_shell_pid") | |
pane_address=$(get_tmux_address_from_pane_info "$pane_info") | |
tmux send-keys -t $pane_address C-c | |
tmux send-keys -t $pane_address "$output_script" \Enter | |
} | |
get_tmux_pane_info() { | |
local pid="$1" | |
tmux_pane_infos=$(tmux list-panes -a -F "#{session_name}:#{window_index}.#{pane_index} #{pane_pid} #{pane_current_command}") | |
tmux_pane_info=$(echo "$tmux_pane_infos" | grep "$pid") | |
echo "$tmux_pane_info" | |
} | |
get_pid_from_pane_info() { | |
local pane_info="$1" | |
echo "$pane_info" | awk '{print $2}' | |
} | |
get_tmux_address_from_pane_info() { | |
local pane_info="$1" | |
echo "$pane_info" | awk '{print $1}' | |
} | |
main "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: ./split-output.sh | |
OUTPUT_FILE="/tmp/jq_output.json" | |
FX_PID_FILE="/tmp/fx_pid" | |
# Check dependencies | |
if ! command -v fx &>/dev/null; then | |
echo "Dependencies 'fx' are required. Install them to continue." | |
exit 1 | |
fi | |
# Ensure the output file exists | |
if [[ ! -f $OUTPUT_FILE ]]; then | |
echo "Waiting for queries... Ensure the input script is running." | |
>"$OUTPUT_FILE" | |
fi | |
fx "$OUTPUT_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Tmux is required.