From 2ecb7d5ea0f9ddba96125a562197cb09786e164e Mon Sep 17 00:00:00 2001 From: TKE Date: Thu, 4 May 2023 08:09:27 +0200 Subject: [PATCH] Add visidata plugin and json-function --- config/visidataplugins/hidecol.py | 37 +++++++++++++++++++++++++++++++ config/visidatarc | 21 ++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 config/visidataplugins/hidecol.py diff --git a/config/visidataplugins/hidecol.py b/config/visidataplugins/hidecol.py new file mode 100644 index 0000000..83d9fd0 --- /dev/null +++ b/config/visidataplugins/hidecol.py @@ -0,0 +1,37 @@ +__version__ = "0.1.0" +__author__ = "Your Name " + +from visidata import Sheet, BaseSheet, vd + + +@Sheet.api +def hide_empty_and_superfluous_cols(sheet): + """ + Given a sheet, hides columns that are empty or superfluous (contain the same + value in every row). + """ + # Create a dictionary to keep track of which columns to hide + cols_to_hide = {} + + # Check each column for being empty or superfluous + for col in sheet.visibleCols: + # Check if the column is empty (all cells are empty or None) + if all([col.getValue(row) in (None, "") for row in sheet.rows]): + cols_to_hide[col] = True + continue + + # Check if the column is superfluous (contains the same value in every row) + values = set([col.getValue(row) for row in sheet.rows if col.getValue(row) not in (None, "", "null")]) + if len(values) == 1: + cols_to_hide[col] = True + + # Hide the columns that were flagged + if cols_to_hide: + for col in cols_to_hide: + col.width=0 + vd.status(f"Hid {len(cols_to_hide)} empty/superfluous columns.") + else: + vd.status("No empty/superfluous columns to hide.") + +Sheet.addCommand(None, "tke-hidecol", "sheet.hide_empty_and_superfluous_cols()") + diff --git a/config/visidatarc b/config/visidatarc index 04612d0..52db97c 100644 --- a/config/visidatarc +++ b/config/visidatarc @@ -1,11 +1,20 @@ # copy or link this file to ~/.visidatarc options.disp_date_fmt="%Y-%m-%dT%H:%M:%S" +import plugins.hidecol from datetime import datetime import functools import json +from urllib.parse import unquote_plus + +def decode_url_safe(url_safe_string): + utf8_string = unquote_plus(url_safe_string) + return utf8_string + + + def what(item): return f"{type(item)}:{str(item)}" @@ -267,3 +276,15 @@ def int2ip(zahl): # convert IP-String to Integer def ip2int(ip): return int.from_bytes(b"".join([int(c).to_bytes(1,'big') for c in b.split('.')]),'big') + +# parse KeyValue +def dirty_kv(data): + return {y[0] : y[1] for y in [x.strip().split("=") for x in data.strip().strip('"{}').split(',')]} + + + + +# parse json with missing quotes around attribute names +import yaml +def dirty_json(data): + return yaml.load(data, yaml.SafeLoader) \ No newline at end of file