def run(self, bits=None, byte_array=None, starting_address=0): """Run the command.""" self.starting_address = starting_address if active_thread is not None and active_thread.is_alive(): error( "HexViewer is already converting a file!\n" "Please run the abort command to stop the current conversion." ) return # If thread is active cancel thread if self.thread is not None and self.thread.is_alive(): self.abort_hex_load() return # Init Buffer file_name = self.buffer_init(bits, byte_array) # Identify view if self.handshake != -1 and self.handshake == self.view.id(): self.reset() self.handshake = self.view.id() if file_name is not None and exists(file_name): # Decide whether to read in as a binary file or a traditional file if self.view.settings().has("hex_viewer_file_name"): self.view_type = "hex" if common.is_hex_dirty(self.view): self.file_name = file_name if bits is None and byte_array is None: self.switch_type = "file" else: self.switch_type = "hex" self.discard_panel() else: if bits is None and byte_array is None: # Switch back to traditional output self.read_file(file_name) else: # Reload hex with new settings self.read_bin(file_name) else: # We are going to swap out the current file for hex output # So as not to clutter the screen. Changes need to be saved # Or they will be lost if self.view.is_dirty(): self.file_name = file_name self.switch_type = "hex" self.discard_panel() else: # Switch to hex output self.read_bin(file_name) else: if file_name is None: error("View does not exist on disk!") else: error("%s does not exist on disk!" % basename(file_name))
def on_close(self, view): """Handle the close event.""" if view.settings().has("hex_viewer_file_name") and common.is_hex_dirty(view): window = sublime.active_window() file_name = file_name = view.settings().get("hex_viewer_file_name") if window is not None and file_name is not None: # Save hex view settings self.fail_safe_view = { "buffer": view.substr(sublime.Region(0, view.size())), "bits": view.settings().get("hex_viewer_bits"), "bytes": view.settings().get("hex_viewer_bytes"), "actual": view.settings().get("hex_viewer_actual_bytes"), "name": file_name, "font_face": view.settings().get("font_face"), "font_size": view.settings().get("font_size"), "edits": view.get_regions("hex_edit") } # Keep window from closing by creating a view # If the last is getting closed # Use this buffer as the restore view if restore occurs count = 0 for v in window.views(): if not v.settings().get("is_widget"): count += 1 if count == 1: view = sublime.active_window().new_file() if view is not None: self.handshake = view.id() # Alert user that they can restore window.show_input_panel( ("Restore %s? (yes | no):" % basename(file_name)), "yes", self.restore, None, lambda: self.restore(value="yes") )