You can write a command + keybinding for it too (so it doesn’t rely on external executables).
{ "keys": ["alt+q"], "command": "show_gzipped_size" },
import gzip
import io
import sublime
import sublime_plugin
def gzip_str(string: str, compresslevel: int = 9) -> bytes:
out = io.BytesIO()
with gzip.GzipFile(fileobj=out, mode="w", compresslevel=compresslevel) as f:
f.write(string.encode())
return out.getvalue()
class ShowGzippedSizeCommand(sublime_plugin.TextCommand):
def run(self, edit: sublime.Edit) -> None:
sel = self.view.sel()
region_whole_file = sublime.Region(0, self.view.size())
is_partial_file = len(sel) > 0 and len(sel[0]) > 0 and not sel[0].contains(region_whole_file)
region = sel[0] if is_partial_file else region_whole_file
string = self.view.substr(region)
string_gzipped = gzip_str(string)
sublime.message_dialog(
"Gzipped size: {:,} bytes {}".format(
len(string_gzipped),
"(selected)" if is_partial_file else "(whole file)",
),
)