From 15c3497f332920220f0c8b3f1005a4bafb95b4d7 Mon Sep 17 00:00:00 2001 From: David Bekoyan Date: Tue, 7 Feb 2017 12:01:05 +0400 Subject: [PATCH] improvements remove on_close hook git view to run as parameter move erase_status outside of loop --- README.md | 2 +- htoint.py | 34 ++++++++++++++-------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1c46e21..03ded71 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -hex2int +hex2int Preview ================ ## Summary diff --git a/htoint.py b/htoint.py index fe4d432..e2e6bc4 100644 --- a/htoint.py +++ b/htoint.py @@ -2,39 +2,33 @@ class HtointCommand(sublime_plugin.EventListener): def on_new(self, view): - self.view = view - self.run() - - def on_close(self, view): - self.view.erase_status('Hexcode') + self.run(view) def on_selection_modified(self, view): - self.view = view - self.run() + self.run(view) def on_activated(self, view): - self.view = view - self.run() + self.run(view) - def run(self): + def run(self, view): statusline = [] - for region in self.view.sel(): + for region in view.sel(): + if region.begin() == region.end(): - word = self.view.word(region) + word = view.word(region) else: word = region + if not word.empty(): - keyword = self.view.substr(word) + + keyword = view.substr(word) isHex = re.findall(r'0x[0-9a-fA-F]+', keyword) - # print(isHex) + if isHex: statusline.append('{} -> {}'.format(isHex[0], self.str_to_int(isHex[0]))) - else: - self.view.erase_status('Hexcode') - else: - self.view.erase_status('Hexcode') - # print(", ".join(statusline)) - self.view.set_status('Hexcode', '{}'.format(", ".join(statusline))) + + view.erase_status('Hexcode') + view.set_status('Hexcode', '{}'.format(", ".join(statusline))) def str_to_int(self, s): i = int(s, 16)