meld icon indicating copy to clipboard operation
meld copied to clipboard

Offer formatting option

Open iwalucas opened this issue 2 years ago • 1 comments

I use meld a lot to compare JSON files, and its always an extra step for me to format that on some external tool, before pasting it into meld. Any chance of adding some basic formatting options?

iwalucas avatar Apr 27 '22 10:04 iwalucas

Multiple people ran into this issue. What I did to resolve it is to make a temp copy of each file which i "prettified" first, and then run meld. Just place this in "/usr/local/bin/meldjson" and make it executable ($ chmod u+x /usr/local/bin/meldjson). Note sure how safe this is, but it does the job! Of course you could also create this script as a bash script...

#!/bin/bash
#Pretty meld: prettify JSON files and then execute meld.

script="
import json
import os

def clean(filename):
    filename_out = '/tmp/' + os.path.basename(filename)
    with open(filename) as f:
        data = json.load(f)
    with open(filename_out, 'w') as f:
        json.dump(data, f, indent=4, sort_keys=True)
    return filename_out
    
filename_tmp_1 = clean('$1')
filename_tmp_2 = clean('$2')
os.system('/usr/bin/meld ' + filename_tmp_1 + ' ' + filename_tmp_2)
os.system('rm ' + filename_tmp_1 + ' ' + filename_tmp_2)
"

python -c "$script"

robwijnhoven avatar Dec 11 '23 11:12 robwijnhoven