- Extract _load_data() method from load_path() for reuse in reload() - Add is_extters externally_modified() to track file state changes (mtime/size) - Replace load_bytes() with load_contents() for better error handling - Add reload() method to re-read file from disk - Fix file_state_watcher by properly detecting external changes
29 lines
733 B
Python
29 lines
733 B
Python
# Python imports
|
|
import json
|
|
|
|
# Lib imports
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
def add_prettify_json(buffer, menu):
|
|
def on_prettify_json(menuitem, buffer):
|
|
start_itr, \
|
|
end_itr = buffer.get_start_iter(), buffer.get_end_iter()
|
|
data = buffer.get_text(start_itr, end_itr, False)
|
|
text = json.dumps(json.loads(data), separators = (',', ':'), indent = 4)
|
|
|
|
buffer.begin_user_action()
|
|
buffer.delete(start_itr, end_itr)
|
|
buffer.insert(start_itr, text)
|
|
buffer.end_user_action()
|
|
|
|
item = Gtk.MenuItem(label = "Prettify JSON")
|
|
item.connect("activate", on_prettify_json, buffer)
|
|
menu.append(item) |