Consolidate format time methods
This commit is contained in:
@@ -15,6 +15,18 @@ import urllib.parse
|
||||
|
||||
from vlc_player import VLCSyncPlayer
|
||||
|
||||
def format_time_hms(ms: int) -> str:
|
||||
"""Format milliseconds as HH:MM:SS."""
|
||||
s = max(0, ms) // 1000
|
||||
return f"{s // 3600:02d}:{(s % 3600) // 60:02d}:{s % 60:02d}"
|
||||
|
||||
def format_time_short(ms: int) -> str:
|
||||
"""Format milliseconds as MM:SS or HH:MM:SS if >= 1 hour."""
|
||||
s = int(max(0, ms) // 1000)
|
||||
if s >= 3600:
|
||||
return f"{s // 3600:02d}:{(s % 3600) // 60:02d}:{s % 60:02d}"
|
||||
return f"{(s % 3600) // 60:02d}:{s % 60:02d}"
|
||||
|
||||
class ChatBubble(QWidget):
|
||||
def __init__(self, author, text, time_str, is_self, on_link_clicked):
|
||||
super().__init__()
|
||||
@@ -427,8 +439,7 @@ class RoomWidget(QWidget):
|
||||
length_ms = self.vlc_player.get_length()
|
||||
if length_ms > 0:
|
||||
target_ms = int((value / 1000.0) * length_ms)
|
||||
s = max(0, target_ms) // 1000
|
||||
return f"{s//3600:02d}:{(s%3600)//60:02d}:{s%60:02d}"
|
||||
return format_time_hms(target_ms)
|
||||
return ""
|
||||
|
||||
def setup_room(self, room_code: str, username: str, file_name: str, file_path: str, start_time_s: float = 0.0):
|
||||
@@ -705,12 +716,8 @@ class RoomWidget(QWidget):
|
||||
def on_vlc_time(self, time_ms: int):
|
||||
length_ms = self.vlc_player.get_length()
|
||||
if length_ms > 0:
|
||||
def fmt(ms):
|
||||
s = max(0, ms) // 1000
|
||||
return f"{s//3600:02d}:{(s%3600)//60:02d}:{s%60:02d}"
|
||||
|
||||
if not self.seekbar.isSliderDown():
|
||||
self.time_lbl.setText(f"{fmt(time_ms)} / {fmt(length_ms)}")
|
||||
self.time_lbl.setText(f"{format_time_hms(time_ms)} / {format_time_hms(length_ms)}")
|
||||
progress = int((time_ms / length_ms) * 1000)
|
||||
self.seekbar.blockSignals(True)
|
||||
self.seekbar.setValue(progress)
|
||||
@@ -754,10 +761,7 @@ class RoomWidget(QWidget):
|
||||
length_ms = self.vlc_player.get_length()
|
||||
if length_ms > 0:
|
||||
target_ms = int((value / 1000.0) * length_ms)
|
||||
def fmt(ms):
|
||||
s = max(0, ms) // 1000
|
||||
return f"{s//3600:02d}:{(s%3600)//60:02d}:{s%60:02d}"
|
||||
self.time_lbl.setText(f"{fmt(target_ms)} / {fmt(length_ms)}")
|
||||
self.time_lbl.setText(f"{format_time_hms(target_ms)} / {format_time_hms(length_ms)}")
|
||||
# Scrub the video locally in real-time
|
||||
self._tell_vlc_and_expect("seek", target_ms / 1000.0)
|
||||
|
||||
@@ -799,8 +803,7 @@ class RoomWidget(QWidget):
|
||||
if action == "play": self.add_system_message(f"{username} pressed play")
|
||||
elif action == "pause": self.add_system_message(f"{username} paused")
|
||||
elif action == "seek":
|
||||
def fmt(s): return f"{int(s)//3600:02d}:{(int(s)%3600)//60:02d}:{int(s)%60:02d}"
|
||||
self.add_system_message(f"{username} seeked to {fmt(position_s)}")
|
||||
self.add_system_message(f"{username} seeked to {format_time_hms(int(position_s * 1000))}")
|
||||
|
||||
def update_users(self, users: list):
|
||||
if self._is_first_user_update:
|
||||
@@ -950,9 +953,7 @@ class RoomWidget(QWidget):
|
||||
target_ms = self._parse_time_arg(time_arg)
|
||||
|
||||
if target_ms is not None:
|
||||
s = int(max(0, target_ms) // 1000)
|
||||
if s >= 3600: time_str = f"{s//3600:02d}:{(s%3600)//60:02d}:{s%60:02d}"
|
||||
else: time_str = f"{(s%3600)//60:02d}:{s%60:02d}"
|
||||
time_str = format_time_short(target_ms)
|
||||
|
||||
msg = f"{time_str} {tag_name}".strip()
|
||||
self.chat_message_ready.emit(msg)
|
||||
@@ -964,9 +965,7 @@ class RoomWidget(QWidget):
|
||||
elif cmd == "/time":
|
||||
self.chat_input.setText("")
|
||||
current_ms = self.vlc_player.current_time_ms
|
||||
s = max(0, current_ms) // 1000
|
||||
if s >= 3600: time_str = f"{s//3600:02d}:{(s%3600)//60:02d}:{s%60:02d}"
|
||||
else: time_str = f"{(s%3600)//60:02d}:{s%60:02d}"
|
||||
time_str = format_time_short(current_ms)
|
||||
self.add_system_message(f"Current time: <a href='seek:{time_str}' style='color: #66b3ff; text-decoration: none;'>{time_str}</a>")
|
||||
return
|
||||
elif cmd == "/help":
|
||||
|
||||
Reference in New Issue
Block a user