Sublime Forum

4512 characters selected, 450 bytes zipped?

#1

I often use ST3 to quickly check the # of chars in a file. I thought it might be useful (and interesting) to also see how many bytes the selected characters would be if zipped.

It may be useful for web work for making snap decisions when you don’t care too much if the file size is big but rather whether the gzip size is too big.

It also just might be intellectually fun to see how much information is in the current selection.

Has anyone seen a plugin that does this?

0 Likes

#2

Always showing the “gzipped” characters sounds like a recipe for awful performance and CPU usage, considering the only way to find out that number is to actually gzip it. A better idea might be to use a build system that runs something like gzip $filename --stdout | wc -c.

2 Likes

#3

Yes I agree “always” showing it would accelerate global warming.

Maybe there are a few heuristics to employ as to when to show it. I will dig up my catch all extension and play around with a few things (your snippet is a great starting point/implementation :slight_smile: ).

0 Likes

#4

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)",
            ),
        )

image

2 Likes

#5

This package shows the file size of the current file in the status bar and has a flag to show the gzipped size as well.

1 Like