From a4f694f5d6b1d1f8a8ffe2aca567c1301b056f4b Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 7 Mar 2018 18:32:21 +0900 Subject: [PATCH] Add auto adjust for columns based on terminal width With flags -a to override any auto adjust and just keep default column width. And flag -c for automatic compact view (40% column size reduction) The auto adjust on terminal size larger than needed is only for path column at the moment. For smaller it will auto adjust each column slowly in 10% steps until the width is smaller than the terminal width. Side note: CJK characters can still be reason for a skewed layout. --- README.md | 4 ++- bin/reverse_geolocate.py | 78 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 005752f..a61c47f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ reverse_geolocate.py [-h] -i [-l LIGHTROOM FOLDER] [-s] [-f ] [-g GOOGLE API KEY] [-o] [-e EMIL ADDRESS] [-w] - [-r] [-n] [-v] [--debug] [--test] + [-r] [-a] [-c] [-n] [-v] [--debug] [--test] ### Arguments @@ -43,6 +43,8 @@ Argument | Argument Value | Description -e, --email | email address | For OpenStreetMap with a large number of access -g, --google | Google Maps API Key | If available, to avoid the access limitations to the reverse location lookup -r, --read-only | | Read only data from the XMP files and print them out. No LR DB connection is done or any map lookups +-a, --no-autoadjust | | Do not try to auto adjust column width to terminal column width +-c, --compact | | Do extra compact view -w, --write-settings | | Write the Google API key or the OpenStreetMap email into the settings file -v, --verbose | | More verbose output. Currently not used --debug | | Full detailed debug output. Will print out alot of data diff --git a/bin/reverse_geolocate.py b/bin/reverse_geolocate.py index 9654191..3cf1e69 100755 --- a/bin/reverse_geolocate.py +++ b/bin/reverse_geolocate.py @@ -346,6 +346,17 @@ def shortenPath(path, length = 30, file_only = False, path_only = False): path = "{} {}".format("..", path[len(path) - length:]) return path; +# METHOD: shortenString +# PARAMS: string, shorten width, override shorten placeholder +# RETURN: shortened string +# DESC : shortens a string to width and attached placeholder +def shortenString(string, width, placeholder = '..'): + if len(str(string)) > width: + width -= len(placeholder) + return "{}{}".format(str(string)[:width], placeholder) + else: + return str(string) + # METHOD: printHeader # PARAMS: header string, line counter, print header counter trigger # RETURN: line counter +1 @@ -463,6 +474,20 @@ parser.add_argument('-r', '--read-only', help = 'Read current values from the XMP file only, do not read from LR or lookup any data and write back' ) +# don't try to do auto adjust in list view +parser.add_argument('-a', '--no-autoadjust', + dest = 'no_autoadjust', + action = 'store_true', + help = 'Don\'t try to auto adjust columns' +) + +# compact view, compresses columns down to a minimum +parser.add_argument('-c', '--compact', + dest = 'compact_view', + action = 'store_true', + help = 'Very compact list view' +) + # Do not create backup files parser.add_argument('-n', '--nobackup', dest = 'no_xmp_backup', @@ -493,7 +518,7 @@ if not args.verbose: args.verbose = 0 if args.debug: - print("### ARGUMENT VARS: I: {incl}, X: {excl}, L: {lr}, F: {fc}, M: {osm}, G: {gp}, E: {em}, N: {nbk}, W: {wrc}, V: {v}, D: {d}, T: {t}".format( + print("### ARGUMENT VARS: I: {incl}, X: {excl}, L: {lr}, F: {fc}, M: {osm}, G: {gp}, E: {em}, R: {read}, A: {adj}, C: {cmp}, N: {nbk}, W: {wrc}, V: {v}, D: {d}, T: {t}".format( incl = args.xmp_sources, excl = args.exclude_sources, lr = args.lightroom_folder, @@ -501,6 +526,9 @@ if args.debug: osm = args.use_openstreetmap, gp = args.google_api_key, em = args.email, + read = args.read_only, + adj = args.no_autoadjust, + cmp = args.compact_view, nbk = args.no_xmp_backup, wrc = args.config_write, v = args.verbose, @@ -714,6 +742,46 @@ if args.read_only: 'location': 25, 'path': 40, } + if args.compact_view: + reduce_percent = 40 + # all formats are reduced to a mininum, we cut % off + for format_key in ['filename', 'latitude', 'longitude', 'country', 'state', 'city', 'location', 'path']: + format_length[format_key] = ceil(format_length[format_key] - ((format_length[format_key] / 100) * reduce_percent)) + else: + # change sizes for print based on terminal size + # NOTE: in screen or term this data might NOT be correct + # Current size needs the in between and left/right space data + current_columns = sum(format_length.values()) + ((len(format_length) - 1) * 3) + 2 + if current_columns < get_terminal_size().columns: + # only do if not disabled + if not args.no_autoadjust: + # keep one empty + width_up = get_terminal_size().columns - current_columns - 1 + if width_up > 0: + format_length['path'] += width_up + else: + # NOTE: perhaps we can have some min size numbers to counter resize + # lat/long -> max 6, country -> 10, path -> 20 + if not args.no_autoadjust: + print("Terminal width too small, will adjust layout automatically") + # abort flag so we can break out of the second loop too + abort = False + # start with 10, then increase until we reach max + reduce_percent_min = 10 + reduce_percent_max = 50 + # loop for each percent grouping and through each reduce size + for reduce_percent in range(reduce_percent_min, reduce_percent_max, 10): + for format_key in ['latitude', 'longitude', 'path', 'country', 'state', 'city', 'location', 'filename']: + format_length[format_key] = ceil(format_length[format_key] - ((format_length[format_key] / 100) * reduce_percent)) + # abort if the size of the new format is smaller than the terminal columns + if sum(format_length.values()) + ((len(format_length) - 1) * 3) + 2 <= get_terminal_size().columns: + abort = True + break + # break out of the outer loop too + if abort: + break + else: + print("Screen layout might be skewed. Increase Terminal width") # after how many lines do we reprint the header header_repeat = 50; # how many pages will we have @@ -823,10 +891,10 @@ for xmp_file in work_files: latitude = str(convertDMStoLat(data_set['GPSLatitude']))[:format_length['latitude']], # cut off from the right longitude = str(convertDMStoLong(data_set['GPSLongitude']))[:format_length['longitude']], code = data_set['CountryCode'][:2].center(4), # is only 2 chars - country = textwrap.shorten(data_set['Country'], width = format_length['country'], placeholder = '..'), # shorten from the right - state = textwrap.shorten(data_set['State'], width = format_length['state'], placeholder = '..'), - city = textwrap.shorten(data_set['City'], width = format_length['city'], placeholder = '..'), - location = textwrap.shorten(data_set['Location'], width = format_length['location'], placeholder = '..'), + country = shortenString(data_set['Country'], width = format_length['country']), # shorten from the right + state = shortenString(data_set['State'], width = format_length['state']), + city = shortenString(data_set['City'], width = format_length['city']), + location = shortenString(data_set['Location'], width = format_length['location']), path = shortenPath(xmp_file, format_length['path'], path_only = True) )) else: