2023-06-17 22:35:01 -04:00

95 lines
3.2 KiB
Python

# find yourself in list of ssb users
class Screen2(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, bg='#bcfef9')
# Add label for instructions
tk.Label(self, text="Start typing your public key to find yourself in the list", bg='#bcfef9').pack(anchor='nw')
# Add entry box for user search
self.search_var = tk.StringVar()
self.search_var.trace('w', self.update_users_list)
self.search_entry = tk.Entry(self, textvariable=self.search_var, width=50)
self.search_entry.pack(anchor='nw')
# Refresh button
refresh_button = tk.Button(self, text="Refresh List", command=self.refresh_users, height=3, width=30, bg='peach puff')
refresh_button.place(relx=0.25, rely=0, anchor='nw')
# The 'Done' button to navigate to next screen
done_button = tk.Button(self, text="Done", command=lambda: master.switch_frame(Screen3), height=3, width=30, bg='peach puff')
done_button.pack(pady=10)
# Container for user list
self.container = ttk.Frame(self)
self.container.pack(fill='both', expand=True)
self.update_users_list()
def update_users_list(self, *args):
search_text = self.search_var.get().lower()
# Remove current users list
for widget in self.container.winfo_children():
widget.destroy()
# Filtered list of users
users = [user for user in self.users if search_text in user['id'].lower()]
self.display_users(users)
def refresh_users(self):
# Update users list from Scuttlebutt, save it to users.json and display it
users = self.get_scuttlebutt_users()
self.save_users_to_file(users)
self.display_users(users)
def get_scuttlebutt_users(self):
result = subprocess.run(['node', 'scuttlebot.js'], capture_output=True, text=True)
if result.returncode == 0:
users = json.loads(result.stdout)
return users
else:
raise Exception("Command failed: " + result.stderr)
def get_users_from_file(self):
if os.path.exists('users.json') and os.path.getsize('users.json') > 0:
try:
with open('users.json', 'r') as f:
users = json.load(f)
except json.JSONDecodeError:
users = []
else:
users = []
return users
def save_users_to_file(self, users):
with open('users.json', 'w') as f:
json.dump(users, f)
def display_users(self, users):
# Scrollable list of users
canvas = tk.Canvas(self.container)
scrollbar = ttk.Scrollbar(self.container, orient='vertical', command=canvas.yview, width=60)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
for user in users:
tk.Label(scrollable_frame, text=user['id']).pack()
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")