-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab_manager.py
More file actions
46 lines (40 loc) · 1.44 KB
/
Copy pathtab_manager.py
File metadata and controls
46 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import os
class TabManager:
def __init__(self):
self.session_file = self._get_session_path()
def _get_session_path(self):
"""Get the path to store tab session file"""
user_profile = os.path.expanduser('~')
app_dir = os.path.join(user_profile, 'AppData', 'Local', 'NotepadAI')
os.makedirs(app_dir, exist_ok=True)
return os.path.join(app_dir, 'tab_session.json')
def save_session(self, tabs):
"""Save current tab session
tabs: list of dicts with 'path' (or None for untitled) and 'content'
"""
try:
session_data = {
'tabs': tabs,
'active_tab': 0
}
with open(self.session_file, 'w', encoding='utf-8') as f:
json.dump(session_data, f, indent=2, ensure_ascii=False)
except IOError:
pass
def load_session(self):
"""Load previous tab session"""
if not os.path.exists(self.session_file):
return None
try:
with open(self.session_file, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
return None
def clear_session(self):
"""Clear saved session"""
if os.path.exists(self.session_file):
try:
os.remove(self.session_file)
except OSError:
pass