-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogViewer.py
More file actions
1386 lines (1019 loc) · 51.9 KB
/
LogViewer.py
File metadata and controls
1386 lines (1019 loc) · 51.9 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" Realtime log viewer for Star Citizen """
from tkinter import messagebox, filedialog, simpledialog, colorchooser, Toplevel, Menu
import re
import threading
import json
import os
import tkinter as tk
import time
import pyautogui
import webbrowser
import sys
import datetime
import pygame.mixer
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Button
from play_time_calculator import PlayTimeCalculator
from tkinter import messagebox
from tkinter import ttk
from tkinter import filedialog
import time
from pynput import keyboard, mouse
import tkinter as tk
import os
print("########################################################################################################")
print("LogViewer")
print("Build: 0.2.9.3")
print("30 June 2024")
print("################################################ START ##################################################")
root = tk.Tk()
# Webbrowser Urls
URLS = ["https://issue-council.robertsspaceindustries.com/projects/STAR-CITIZEN",
"https://robertsspaceindustries.com/spectrum/community/SC/lobby/38230",
"https://robertsspaceindustries.com/spectrum/community/AVOCADO/lobby/1355241",
"https://robertsspaceindustries.com/spectrum/community/SC/forum/190048?page=1&sort=newest",
"https://robertsspaceindustries.com/account/settings",
"https://robertsspaceindustries.com/galactapedia",
"https://robertsspaceindustries.com/spectrum/community/SC/forum/190048?page=1&sort=newest",
"https://robertsspaceindustries.com/spectrum/community/AVOCADO/search?member&page=1&q=&range=year&role=67971&scopes=op%2Creply%2Cchat&sort=latest&visibility=nonerased",
"https://robertsspaceindustries.com/spectrum/community/SC/search?member&page=1&q=&range=year&role=2&scopes=op%2Creply%2Cchat&sort=latest&visibility=nonerased"]
KEY = ["Issue Council","SC Testing", "Avocado", "Spectrum", "Account", "Galactapedia", "Patchnotes","Evo Latest","SC Testing Latest"]
class LogViewer(threading.Thread):
"""LogViewer class that monitors the log file and displays the log in a tkinter window."""
def __init__(self,logviewer):
"""Initializes the LogViewer class."""
threading.Thread.__init__(self,None,daemon=True)
print("SC Log Monitor Starting...")
if not os.path.exists("mp3"):
os.makedirs("mp3")
self.mp3_folder = os.path.abspath("mp3")
else:
self.mp3_folder = os.path.abspath("mp3")
pygame.mixer.init()
mp3_files = [f for f in os.listdir(self.mp3_folder) if f.endswith(".mp3")]
if mp3_files:
mp3_file = mp3_files[0]
mp3_file_path = os.path.join(self.mp3_folder, mp3_file)
print("Playing:", mp3_file_path)
pygame.mixer.music.load(mp3_file_path)
pygame.mixer.music.play()
else:
print("No mp3 files found in:", self.mp3_folder)
self.is_minimized = False
self.logviewer = logviewer
self.topmost_var = tk.BooleanVar(value=True)
SliderFrame = tk.Frame(self.logviewer,bg="#1E1E1E")
SliderFrame.pack(side="top",fill=tk.BOTH, expand=False)
self.frame = tk.Frame(self.logviewer,bg="black")
self.frame.pack(fill=tk.BOTH, expand=True)
logviewer.attributes('-topmost', 1)
logviewer.overrideredirect(True)
logviewer.geometry("705x950")
logviewer.title("SC LogViewer")
screen_width = logviewer.winfo_screenwidth()
screen_height = logviewer.winfo_screenheight()
x = (screen_width / 2) - (705 / 2)
y = (screen_height / 2) - (950 / 2)
logviewer.geometry("705x950+%d+%d" % (x, y))
logviewer.bind("<ButtonPress-3>", self.start_move)
logviewer.bind("<ButtonRelease-3>", self.stop_move)
logviewer.bind("<B3-Motion>", self.on_move)
self.font_scale_var = tk.DoubleVar(value=1.0)
self.btn_win_mode = tk.Button(self.frame, text="", command=self.toggle_window, fg="white", bg="#1E1E1E")
self.btn_win_mode.pack(side="top", fill="x", padx=5, pady=1)
DefaultSliderValueOpac = 1
self.slider_opacity = tk.Scale(
SliderFrame, from_=0.2, to=1, resolution=0.1, width=10, length=250,
orient="horizontal", label="", showvalue=False, command=self.set_transparency,
bg="#1E1E1E", fg="#FFFFFF", troughcolor="#565656", highlightbackground="#1E1E1E",
bd=0, font=("Arial", 10, "bold")
)
self.slider_opacity.set(DefaultSliderValueOpac)
self.slider_opacity.pack(side="right", fill="x", padx=5, pady=1)
DefaultSliderValueFont = 1
self.slider_font_size = tk.Scale(
SliderFrame, from_=0.2, to=2, resolution=0.01, width=10, length=250,
orient="horizontal", label="", showvalue=False, command=self.Set_FontSize,
bg="#1E1E1E", fg="#FFFFFF", troughcolor="#565656", highlightbackground="#1E1E1E",
bd=0, font=("Arial", 10, "bold")
)
self.slider_font_size.set(DefaultSliderValueFont)
self.slider_font_size.pack(side="left", fill="x", padx=5, pady=1)
self.scrollbar = tk.Scrollbar(self.frame)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.scrollbar.pack_forget()
self.log_text = tk.Text(self.frame, wrap=tk.WORD, yscrollcommand=self.scrollbar.set, fg="white", bg="black")
self.log_text.pack(fill=tk.BOTH, expand=True)
self.log_text.config(insertbackground='white', insertwidth=2)
self.log_text.focus_set()
self.configure_text_tags()
height = 1
self.start_monitor_text = "Start"
self.pause_monitor_text = "Pause"
self.stop_monitor_text = "Stop"
self.resume_monitor_text = "Resume"
quit_text = "Quit"
to_top_text = "⬆"
to_bottom_text = "⬇"
clear_log_text = "Clear"
window_mode_text = "Window"
toggle_file_menu_text = "File"
toggle_search_menu_text = "Words"
self.btn_monitor = tk.Button(self.frame, text=self.start_monitor_text, command=self.start_stop, fg="white", bg="green", height=height)
self.btn_monitor.pack(side="left", padx=5)
self.btn_pause_monitor = tk.Button(self.frame, text=self.pause_monitor_text, command=self.pause_monitor, fg="white", bg="green", height=height)
self.btn_pause_monitor.pack(side="left", padx=5)
self.btn_hide_log_view = tk.Button(self.frame, text=quit_text, command=self.Quit_LV, fg="white", bg="#b30404", height=height)
self.btn_hide_log_view.pack(side="right", padx=5)
self.btn_to_top = tk.Button(self.frame, text=to_top_text, command=self.ToTop, fg="white", bg="#333333", height=height,font=("Helvetica", 8))
self.btn_to_top.pack(side="left", padx=5)
self.btn_to_bottom = tk.Button(self.frame, text=to_bottom_text, command=self.ToBottom, fg="white", bg="#333333", height=height,font=("Helvetica", 8))
self.btn_to_bottom.pack(side="left", padx=5)
self.btn_clear_log_mon = tk.Button(self.frame, text=clear_log_text, command=self.ClearLog, fg="white", bg="#ce723c", height=height)
self.btn_clear_log_mon.pack(side="left", padx=5)
self.btn_win_mode = tk.Button(self.frame, text=window_mode_text, command=self.toggle_mode, fg="white", bg="#333333", height=height)
self.btn_win_mode.pack(side="right", padx=5)
self.btn_toggle_file_menu= tk.Button(self.frame, text=toggle_file_menu_text, command=self.show_file_menu, fg="white", bg="#333333", height=height)
self.btn_toggle_file_menu.pack(side="right", padx=5)
self.btn_toggle_search_menu = tk.Button(self.frame, text=toggle_search_menu_text, command=self.toggle_word_menu, fg="white", bg="#333333", height=height)
self.btn_toggle_search_menu.pack(side="left", padx=5)
self.btn_print_stats = tk.Button(self.frame, text="Stats", command=self.print_stats, fg="white", bg="#333333", height=height)
self.btn_print_stats.pack(side="left", padx=5)
self.btn_start_stop_chart = tk.Button(self.frame, text="Chart", command=self.start_stop_chart, fg="white", bg="#333333", height=height)
self.btn_start_stop_chart.pack(side="left", padx=5)
self.log_text.bind("<FocusOut>", self.on_focus_out)
self.log_text.bind("<FocusIn>", self.on_focus_in)
self.logviewer.attributes("-alpha", 0.7)
self.logviewer.configure(bg="black")
self.monitor_log_process = False
self.is_borderless = True
self.start_stop_chart_bool = False
self.timer = None
self.inside_physics_instance = False
self.search_win = None
self.pause_monitor = False
self.is_search_mode = True
self.menu_bar = tk.Menu(logviewer)
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="Select LIVE", command=self.save_live_json)
self.file_menu.add_command(label="Open txt", command=self.open_file)
self.file_menu.add_command(label="Save txt", command=self.save_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.Quit_LV)
self.add_word_menu = tk.Menu(self.file_menu, tearoff=0)
self.add_word_menu.add_command(label="Word Menu", command=self.toggle_word_menu)
self.notifications_menu = tk.Menu(self.file_menu, tearoff=0)
self.notifications_menu.add_command(label="Enable Notifications", command=self.enable_notifications)
self.notifications_menu.add_command(label="Disable Notifications", command=self.disable_notifications)
self.monitor_menu = tk.Menu(self.menu_bar, tearoff=0)
self.monitor_menu.add_command(label=self.start_monitor_text, command=self.start_monitor)
self.monitor_menu.add_command(label=self.pause_monitor_text, command=self.pause_monitor)
self.monitor_menu.add_command(label="Stop Monitor", command=self.stop_monitor)
self.monitor_menu.add_command(label="Clear Log", command=self.ClearLog)
self.monitor_menu.add_command(label="Top", command=self.ToTop)
self.monitor_menu.add_command(label="Bottom", command=self.ToBottom)
self.view_menu = tk.Menu(self.menu_bar, tearoff=0)
self.view_menu.add_command(label="Toggle Window Mode", command=self.toggle_mode)
self.view_menu.add_command(label="Toggle Topmost", command=self.toggle_force_front)
self.view_menu.add_command(label="Toggle Buttons", command=self.toggle_buttons)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
self.menu_bar.add_cascade(label="Words", menu=self.add_word_menu)
self.menu_bar.add_cascade(label="Notifications", menu=self.notifications_menu)
self.menu_bar.add_cascade(label="Monitor", menu=self.monitor_menu)
self.menu_bar.add_cascade(label="View", menu=self.view_menu)
self.links_menu = tk.Menu(self.menu_bar, tearoff=0)
for key, url in zip(KEY, URLS):
self.links_menu.add_command(label=key, command=lambda url=url: webbrowser.open(url))
self.menu_bar.add_cascade(label="Links", menu=self.links_menu)
self.extras_menu = tk.Menu(self.menu_bar, tearoff=0)
self.extras_menu.add_command(label="Idle Monitor", command=self.run_play_time_app)
self.menu_bar.add_cascade(label="Extras", menu=self.extras_menu)
logviewer.config(menu=self.menu_bar)
self.search_list = []
self.mute_notifications = False
"""Opens the search menu for adding, removing, and coloring persistant search words."""
self.search_win = Toplevel(self.logviewer)
self.search_win.title("Search Menu")
self.search_win.attributes("-topmost", 1)
self.search_win.configure(bg="black")
self.search_win.geometry("300x500")
parent_x = self.logviewer.winfo_x()
parent_y = self.logviewer.winfo_y()
parent_width = self.logviewer.winfo_width()
parent_height = self.logviewer.winfo_height()
x = parent_x + (parent_width / 2) - (300 / 2)
y = parent_y + (parent_height / 2) - (500 / 2)
self.search_win.geometry(f'300x500+{int(x)}+{int(y)}')
self.search_win.overrideredirect(True)
self.btn_toggle_search_menu.config(bg="#333333")
self.search_win.withdraw()
def move_window(event):
"""Move the window when the left mouse button is dragged."""
self.search_win.geometry(f'+{event.x_root - dx}+{event.y_root - dy}')
def on_right_click(event):
"""Store the initial x and y coordinates when the right mouse button is clicked."""
nonlocal dx, dy
dx = event.x
dy = event.y
def resize_window(event):
if resizing:
self.search_win.geometry(f'{event.x_root}x{event.y_root}')
def on_right_click_resize(event):
nonlocal resizing
edge_thickness = 10
if (event.x > self.search_win.winfo_width() - edge_thickness) or (event.y > self.search_win.winfo_height() - edge_thickness):
resizing = True
else:
resizing = False
dx, dy = 0, 0
resizing = False
self.search_win.bind('<Button-3>', on_right_click)
self.search_win.bind('<B3-Motion>', move_window)
self.search_win.bind('<Button-1>', on_right_click_resize)
self.search_win.bind('<B1-Motion>', resize_window)
self.log_text.bind("<Button-1>", self.on_left_click)
def add_word():
"""adds word when inside of the search menu/ word manager"""
try:
search_word = self.log_text.selection_get()
except tk.TclError:
search_word = ""
if not search_word:
screen_width = self.search_win.winfo_screenwidth()
screen_height = self.search_win.winfo_screenheight()
x = (screen_width / 2) - (300 / 2)
y = (screen_height / 2) - (500 / 2)
self.search_win.geometry(f"300x500+{int(x)}+{int(y)}")
search_word = simpledialog.askstring("Add Word", "Enter a word to search and highlight:", parent=self.search_win)
if search_word:
color = colorchooser.askcolor(parent=self.search_win)[1]
if color:
notify = messagebox.askyesno("Notify", "Do you want to be notified when the word is found?")
self.search_list.append({"word": search_word, "color": color, "count": 0, "notify": notify})
self.search_and_highlight_words()
update_word_list()
def hotkey_add_word():
"""Adds a word to the search menu/ word manager using a hotkey, default notify = false."""
try:
search_word = self.log_text.selection_get()
except tk.TclError:
search_word = None
print("No text selected.")
if search_word:
color = colorchooser.askcolor(parent=self.search_win)[1]
if color:
notify = messagebox.askyesno("Notify", "Do you want to be notified when the word is found?")
self.search_list.append({"word": search_word, "color": color, "count": 0, "notify": notify})
self.search_and_highlight_words()
update_word_list()
def remove_selected_words():
"""Remove the selected words from the listbox."""
selected_items = listbox.curselection()
for item in reversed(selected_items):
word = self.search_list[item]["word"]
del self.search_list[item]
clear_selected_highlights(word)
update_word_list()
def remove_all_words():
"""Remove all words from the listbox."""
def confirm_remove():
root = tk.Tk()
root.withdraw()
return messagebox.askyesno("Confirm Remove", "Do you want to remove all words?")
if confirm_remove():
clear_all_highlights()
self.search_list = []
update_word_list()
else:
print("The operation was cancelled. The words were not removed.")
def clear_all_highlights():
"""Clear all highlights from the log text widget."""
for item in self.search_list:
word = item["word"]
self.log_text.tag_remove(word, 1.0, tk.END)
self.log_text.tag_configure(word, foreground="white", background="")
def clear_selected_highlights(removed_word):
"""Clear the highlights of the removed word."""
self.log_text.tag_remove(removed_word, 1.0, tk.END)
self.log_text.tag_configure(removed_word, foreground="white", background="")
def update_word_list():
"""Update the word list in the listbox."""
listbox.delete(0, tk.END)
for idx, item in enumerate(self.search_list):
word = item["word"]
color = item["color"]
listbox.insert(tk.END, word)
listbox.itemconfig(idx, fg="black", bg=color)
def color_word():
"""Color the selected words in the listbox."""
selected_items = listbox.curselection()
color = colorchooser.askcolor(parent=self.search_win)[1]
if color:
for item in selected_items:
self.search_list[item]["color"] = color
update_word_list()
self.search_and_highlight_words()
def change_notification_state_for_selected_words():
"""Change the notification state of the selected word in the listbox."""
selected_items = listbox.curselection()
# ask the user once if they want to notify when the selected words are found
notify = messagebox.askyesno("Notify", "Do you want to be notified when the selected words are found?")
for item in selected_items:
self.search_list[item]["notify"] = notify
def default_save_word():
"""Save the current words to the default words file."""
def confirm_overwrite():
"""Ask the user if they want to overwrite the default words."""
root = tk.Tk()
root.withdraw()
return messagebox.askyesno("Confirm Overwrite", "Do you want to overwrite the default with current words?")
file_path = "defaultwords.json"
if file_path:
if confirm_overwrite():
with open(file_path, "w") as file:
json.dump(self.search_list, file)
else:
print("The operation was cancelled. The default words were not overwritten.")
def default_load_word():
"""Load the default words from the default words file."""
file_path = "defaultwords.json"
if os.path.exists(file_path):
with open(file_path, "r") as file:
self.search_list = json.load(file)
self.search_and_highlight_words()
update_word_list()
def on_app_close():
"""Handle the close event of the search menu."""
self.Quit_LV()
def save_word():
"""Save the current words to a file."""
file_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json"), ("All files", "*.*")])
if file_path:
with open(file_path, "w") as file:
json.dump(self.search_list, file)
def load_word():
"""Load the words from a file."""
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json"), ("All files", "*.*")])
if file_path:
with open(file_path, "r") as file:
self.search_list = json.load(file)
self.search_and_highlight_words()
update_word_list()
def toggle_highlight():
"""Toggle the highlight of the words in the listbox."""
self.highlight = not self.highlight
if self.highlight:
highlight_button.config(text="Highlight", bg="green", fg="white")
else:
highlight_button.config(text="Highlight", bg="black", fg="white")
self.search_and_highlight_words()
self.logviewer.protocol("WM_DELETE_WINDOW", on_app_close)
xpadval = 1
button_properties = [
{"frame": "bottom", "text": "Add Word", "command": add_word, "fg": "white", "bg": "#003a96"},
{"frame": "bottom", "text": "Remove", "command": remove_selected_words, "fg": "white", "bg": "#c73018"},
{"frame": "bottom", "text": "Remove All", "command": remove_all_words, "fg": "white", "bg": "#7a1d0e"},
{"frame": "bottom", "text": "Color", "command": color_word, "fg": "white", "bg": "#7809a0"},
]
button_frame_top = tk.Frame(self.search_win, bg="black")
button_frame_top.pack(side="top", pady=xpadval)
button_frame_bottom = tk.Frame(self.search_win, bg="black")
button_frame_bottom.pack(side="bottom")
frames = {"top": button_frame_top, "bottom": button_frame_bottom}
for properties in button_properties:
btn = tk.Button(frames[properties["frame"]], text=properties["text"], command=properties["command"], fg=properties["fg"], bg=properties["bg"])
btn.pack(side="left", padx=xpadval)
listbox = tk.Listbox(self.search_win, selectmode=tk.MULTIPLE, fg="white", bg="black")
listbox.pack(fill=tk.BOTH, expand=True)
update_word_list()
self.highlight = False
highlight_button = tk.Button(
button_frame_bottom,
text="Highlight",
command=toggle_highlight,
fg="white",
bg="black",
activebackground="black",
activeforeground="white",
)
highlight_button.pack(side="left", padx=xpadval)
menu_bar = tk.Menu(self.search_win)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Save Words", command=save_word)
file_menu.add_command(label="Load Words", command=load_word)
file_menu.add_command(label="Save Default", command=default_save_word)
file_menu.add_command(label="Load Default", command=default_load_word)
menu_bar.add_cascade(label="File", menu=file_menu)
words_menu = tk.Menu(menu_bar, tearoff=0)
words_menu.add_command(label="Add Word", command=add_word)
words_menu.add_command(label="Remove", command=remove_selected_words)
words_menu.add_command(label="Remove All", command=remove_all_words)
menu_bar.add_cascade(label="Words", menu=words_menu)
highlight_menu = tk.Menu(menu_bar, tearoff=0)
highlight_menu.add_command(label="Toggle", command=toggle_highlight)
highlight_menu.add_command(label="New Color", command=color_word)
menu_bar.add_cascade(label="Highlight Color", menu=highlight_menu)
# notification menu
notification_menu = tk.Menu(menu_bar, tearoff=0)
notification_menu.add_command(label="Change Selected", command=change_notification_state_for_selected_words)
menu_bar.add_cascade(label="Notify", menu=notification_menu)
menu_bar.add_command(label="Close", command=self.close_search_menu)
self.search_win.config(menu=menu_bar)
#keybinds
# bind ctrl w to open word menu
self.logviewer.bind("<Control-e>", lambda event: self.toggle_word_menu())
# bind ctrl f to call new function named "search_and_count"
self.logviewer.bind("<Control-f>", lambda event: self.search_and_count_handler())
# bind ctrl q to call new function named "start stop chart"
self.logviewer.bind("<Control-q>", lambda event: self.start_stop_chart())
# bind ctrl a to call the keybind word function
self.logviewer.bind("<Control-w>", lambda event: hotkey_add_word())
default_load_word()
def on_focus_out(self, event):
try:
start_index, end_index = self.log_text.tag_ranges(tk.SEL)
if start_index and end_index:
self.log_text.tag_add("persist_highlight", start_index, end_index)
self.log_text.tag_configure("persist_highlight", background="gray")
except ValueError:
pass
def on_focus_in(self, event):
self.log_text.tag_remove("persist_highlight", 1.0, tk.END)
def on_left_click(self, event):
self.log_text.tag_remove("persist_highlight", 1.0, tk.END)
def search_and_count_handler(self):
"""Handle the search and count operation."""
if self.is_search_mode:
self.start_search_and_count()
else:
self.finish_search_and_count()
def start_search_and_count(self):
self.is_search_mode = False
try:
self.search_word = self.log_text.selection_get()
except tk.TclError:
self.search_word = ""
if not self.search_word:
print("no text selected")
if self.search_word:
index = 1.0
count = 0
while index:
index = self.log_text.search(self.search_word, index, stopindex=tk.END, nocase=False)
if index:
count += 1
end_index = f"{index}+{len(self.search_word)}c"
self.log_text.tag_add(self.search_word, index, end_index)
self.log_text.tag_configure(self.search_word, background="yellow", foreground="black")
index = end_index
else:
break
self.notification = Toplevel(self.logviewer)
self.notification.title("Search and Count")
self.notification.attributes("-topmost", 1)
self.notification.configure(bg="black")
min_width = 100
width_multiplier = 10
calculated_width = len(self.search_word) * width_multiplier
final_width = max(calculated_width, min_width)
self.notification.geometry(f"{final_width}x25")
label = tk.Label(self.notification, text=f"{self.search_word} : {count}", fg="yellow", bg="black", font=("Arial", 12))
label.pack()
self.notification.overrideredirect(True)
x, y = pyautogui.position()
self.notification.geometry(f"+{x}+{y}")
def finish_search_and_count(self):
"""Finish the search and count operation by removing the temporary highlights."""
self.is_search_mode = True
try:
self.log_text.tag_remove(self.search_word, 1.0, tk.END)
self.log_text.tag_configure(self.search_word, foreground="white", background="")
self.notification.destroy()
self.search_and_highlight_words()
except AttributeError:
pass
def toggle_buttons(self):
"""Toggles the visibility of the UI buttons in the logviewer window"""
if self.btn_monitor.winfo_ismapped():
self.hide_buttons()
else:
self.show_buttons()
def hide_buttons(self):
"""Hides the UI buttons in the logviewer window"""
self.btn_monitor.pack_forget()
self.btn_pause_monitor.pack_forget()
self.btn_hide_log_view.pack_forget()
self.btn_to_top.pack_forget()
self.btn_to_bottom.pack_forget()
self.btn_clear_log_mon.pack_forget()
self.btn_win_mode.pack_forget()
self.btn_toggle_file_menu.pack_forget()
self.btn_toggle_search_menu.pack_forget()
self.btn_print_stats.pack_forget()
self.btn_start_stop_chart.pack_forget()
def show_buttons(self):
"""Shows the UI buttons in the logviewer window"""
self.btn_monitor.pack(side="left", padx=5)
self.btn_pause_monitor.pack(side="left", padx=5)
self.btn_hide_log_view.pack(side="right", padx=5)
self.btn_to_top.pack(side="left", padx=5)
self.btn_to_bottom.pack(side="left", padx=5)
self.btn_clear_log_mon.pack(side="left", padx=5)
self.btn_win_mode.pack(side="right", padx=5)
self.btn_toggle_file_menu.pack(side="right", padx=5)
self.btn_toggle_search_menu.pack(side="left", padx=5)
self.btn_print_stats.pack(side="left", padx=5)
self.btn_start_stop_chart.pack(side="left", padx=5)
def show_file_menu(self):
"""Shows a dropdown menu with options"""
menu = tk.Menu(self.logviewer, tearoff=0, bg="#3f0554", fg="#FFFFFF")
# Button 1: Select LIVE
menu.add_command(label="Select LIVE", command=self.save_live_json)
# Button 2: Open txt
menu.add_command(label="Open txt", command=self.open_file)
# Button 3: Save txt
menu.add_command(label="Save txt", command=self.save_file)
menu.post(self.btn_toggle_file_menu.winfo_rootx(), self.btn_toggle_file_menu.winfo_rooty())
def open_file(self):
"""opens a file dialog to select a file and reads the content of the file into the log text widget so the user can read and edit."""
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
if file_path:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
self.log_text.delete(1.0, tk.END)
self.log_text.insert(tk.INSERT, content)
def save_file(self):
"""saves the content of the variable self.log_text to a file. allowing user to modify the log, make notes and save it to a file."""
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
if file_path:
content = self.log_text.get(1.0, tk.END)
with open(file_path, "w") as file:
file.write(content)
def toggle_word_menu(self):
"""Toggle the search menu on and off showing the search words and highlighting them in the log text."""
if self.search_win is not None and self.search_win.winfo_viewable():
self.close_search_menu()
print("Search Menu Closed")
else:
self.open_search_menu()
print("Search Menu Opened")
def open_search_menu(self):
"""Opens the search menu"""
print("Open search menu called")
self.search_win.deiconify()
parent_x = self.logviewer.winfo_x()
parent_y = self.logviewer.winfo_y()
parent_width = self.logviewer.winfo_width()
parent_height = self.logviewer.winfo_height()
x = parent_x + (parent_width / 2) - (300 / 2)
y = parent_y + (parent_height / 2) - (500 / 2)
self.search_win.geometry(f'300x500+{int(x)}+{int(y)}')
self.search_win.attributes("-topmost", 1)
self.btn_toggle_search_menu.config(bg="green")
def close_search_menu(self):
"""Closes the search menu"""
print("Close search menu called")
if self.search_win:
self.search_win.withdraw()
self.btn_toggle_search_menu.config(bg="#333333")
def enable_notifications(self):
"""Enable notifications for the search words self.mute_notifications = False"""
print("Enable notifications called")
self.mute_notifications = False
def disable_notifications(self):
"""Disable notifications for the search words self.mute_notifications = True"""
print("Disable notifications called")
self.mute_notifications = True
def toggle_force_front(self):
"""Toggle the topmost attribute of the logviewer window to keep it on top of all other windows."""
if self.topmost_var.get():
self.logviewer.attributes("-topmost", 0)
else:
self.logviewer.attributes("-topmost", 1)
self.topmost_var.set(not self.topmost_var.get())
self.logviewer.lift()
def highlight_new_line(self, line, start_index):
"""Highlight the new line that was added to the log text."""
for item in self.search_list:
word = item["word"]
color = item["color"]
index = start_index
while index:
index = self.log_text.search(word, index, stopindex=tk.END, nocase=True)
if index:
end_index = f"{index}+{len(word)}c"
self.log_text.tag_add(word, index, end_index)
if self.highlight:
self.log_text.tag_configure(word, background=color, foreground="black")
else:
self.log_text.tag_configure(word, foreground=color, background="")
index = end_index
item["count"] += 1
else:
break
def load_default_words(self):
"""Load the default words from the defaultwords.json file."""
file_path = "defaultwords.json"
if os.path.exists(file_path):
with open(file_path, "r") as file:
self.search_list = json.load(file)
self.search_and_highlight_words()
self.update_word_list()
def search_and_highlight_words(self):
"""Search for the words in the search list and highlight them in the log text."""
for item in self.search_list:
word = item["word"]
color = item["color"]
self.log_text.tag_remove(word, 1.0, tk.END)
content = self.log_text.get(1.0, tk.END)
for item in self.search_list:
word = item["word"]
color = item["color"]
index = 1.0
item["count"] = 0
while index:
index = self.log_text.search(word, index, stopindex=tk.END, nocase=False)
if index:
end_index = f"{index}+{len(word)}c"
self.log_text.tag_add(word, index, end_index)
if self.highlight:
self.log_text.tag_configure(word, background=color, foreground="black")
else:
self.log_text.tag_configure(word, foreground=color, background="")
index = end_index
item["count"] += 1
def count_words(self):
"""Count the occurrences of each word in the log text."""
content = self.log_text.get(1.0, tk.END)
words = content.split()
word_counts = {}
for word in words:
if word not in word_counts:
word_counts[word] = 1
else:
word_counts[word] += 1
for item in self.search_list:
word = item["word"]
if word in word_counts:
item["count"] = word_counts[word]
else:
item["count"] = 0
def print_stats(self):
print("Printing stats...")
"""Print the statistics of the search words to the console."""
print("____________________________________Word Count____________________________________________")
self.log_text.insert(tk.END, "____________________________________Word Count____________________________________________\n", "yellow")
for item in self.search_list:
word = item["word"]
count = item["count"]
print(f"{word}: {count}")
self.log_text.insert(tk.END, f"{word}: {count}\n", "yellow")
self.log_text.see(tk.END)
print("__________________________________________________________________________________________")
self.log_text.insert(tk.END, f"__________________________________________________________________________________________\n", "yellow")
def print_to_console_and_text_widget(self, message):
"""Print a message to the console and the log text widget."""
print(message)
self.log_text.insert(tk.END, message + '\n', 'white')
self.log_text.see(tk.END)
def start_stop_chart(self):
"""Start or stop the charting of the search word counts."""
if self.start_stop_chart_bool:
print("Stopping chart...")
self.start_stop_chart_bool = False
self.btn_start_stop_chart.config(bg="#333333")
self.remove_chart()
else:
print("Starting chart...")
self.start_stop_chart_bool = True
self.btn_start_stop_chart.config(bg="green")
self.plot_graph()
def remove_chart(self):
"""Remove the chart from the log text widget."""
self.btn_start_stop_chart.config(bg="#333333")
self.start_stop_chart_bool = False
print("Removing chart...")
plt.close()
def plot_graph(self):
print("Plotting graph...")
words = [item["word"] for item in self.search_list]
counts = [item["count"] for item in self.search_list]
colors = [item["color"] for item in self.search_list]
plt.style.use('dark_background')
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
plt.subplots_adjust(left=0.3)
plt.subplots_adjust(right=0.97)
ax.barh(words, counts, color=colors)
self.set_plot_style(ax)
button_ax = fig.add_axes([0.81, 0.05, 0.1, 0.075])
button = Button(button_ax, 'Refresh', color='darkgrey', hovercolor='lightgrey')
button.on_clicked(lambda event: self.refresh_chart(fig, ax))
fig.canvas.mpl_connect('close_event', lambda event: self.remove_chart())
plt.show()
def refresh_chart(self, fig, ax):
print("Refreshing plot...")
words = [item["word"] for item in self.search_list]
counts = [item["count"] for item in self.search_list]
colors = [item["color"] for item in self.search_list]
ax.clear()
ax.barh(words, counts, color=colors)
self.set_plot_style(ax)
plt.draw()
def set_plot_style(self, ax):
ax.set_title("Search Word Counts", color='white')
ax.set_xlabel("Counts", color='white')
ax.set_ylabel("Words", color='white')
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
def print_rsi_launcher_json(self):
"""Print the value of the RSI Launcher directory from the config file to the console and log text widget."""
script_directory = os.path.abspath('.')
config_file_path = os.path.join(script_directory, "config.json")