Skip to content

enhancement: Introduces application-wide time format setting. #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ def initial_data(logged_on_user, users_fixture, streams_fixture):
}
}
},
'twenty_four_hour_time': True,
'last_event_id': -1,
'muted_topics': [],
'realm_user_groups': [],
Expand Down
30 changes: 30 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
'subscription',
'typing',
'update_message_flags',
'update_display_settings',
]
fetch_event_types = [
'realm',
Expand All @@ -189,6 +190,7 @@ def test_register_initial_desired_events(self, mocker, initial_data):
'muted_topics',
'realm_user',
'realm_user_groups',
'update_display_settings',
'zulip_version',
]
model.client.register.assert_called_once_with(
Expand Down Expand Up @@ -1940,6 +1942,34 @@ def test__handle_subscription_event_subscribers_one_user_multiple_streams(
new_subscribers = model.stream_dict[stream_id]['subscribers']
assert new_subscribers == expected_subscribers

@pytest.mark.parametrize(['setting', 'twenty_four_hr_format'], [
(True, False), (False, True)
])
def test_update_twenty_four_hour_format(self, mocker, model, setting,
twenty_four_hr_format):
event = {
'type': 'update_display_settings',
'setting_name': 'twenty_four_hour_time',
}
event['setting'] = setting

first_msg_w = mocker.Mock()
second_msg_w = mocker.Mock()
first_msg_w.original_widget.message = {'id': 1}
second_msg_w.original_widget.message = {'id': 2}
self.controller.view.message_view = mocker.Mock(
log=[first_msg_w, second_msg_w])
create_msg_box_list = mocker.patch('zulipterminal.model.'
'create_msg_box_list')
# Test for change in time format
model.twenty_four_hr_format = twenty_four_hr_format
assert model.twenty_four_hr_format != event['setting']
model._handle_update_display_settings(event)
assert model.twenty_four_hr_format == event['setting']
assert create_msg_box_list.call_count == len(
self.controller.view.message_view.log)
assert model.controller.update_screen.called

@pytest.mark.parametrize('muted_streams, stream_id, is_muted', [
({1}, 1, True),
({1}, 2, False),
Expand Down
7 changes: 7 additions & 0 deletions zulipterminal/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ class UpdateMessageFlagsEvent(TypedDict):
all: bool


class UpdateDisplaySettings(TypedDict):
type: Literal['update_display_settings']
setting_name: str
setting: bool


Event = Union[
MessageEvent,
UpdateMessageEvent,
ReactionEvent,
SubscriptionEvent,
TypingEvent,
UpdateMessageFlagsEvent,
UpdateDisplaySettings,
]
26 changes: 25 additions & 1 deletion zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def __init__(self, controller: Any) -> None:
('typing', self._handle_typing_event),
('update_message_flags',
self._handle_update_message_flags_event),
('update_display_settings',
self._handle_update_display_settings),
])
)

Expand Down Expand Up @@ -149,6 +151,7 @@ def __init__(self, controller: Any) -> None:
self.active_emoji_data = OrderedDict(sorted(all_emoji_data,
key=lambda e: e[0]))

self.twenty_four_hr_format = self.initial_data['twenty_four_hour_time']
self.new_user_input = True
self._start_presence_updates()

Expand Down Expand Up @@ -1170,8 +1173,10 @@ def formatted_local_time(
format_codes = (
"%a %b %d "
f"{'%Y ' if show_year else ''}"
"%H:%M"
f"{'%H:' if self.twenty_four_hr_format else '%I:'}"
"%M"
f"{':%S' if show_seconds else ''}"
f"{'' if self.twenty_four_hr_format else ' %p'}"
)
return local_time.strftime(format_codes)

Expand Down Expand Up @@ -1222,6 +1227,24 @@ def _update_rendered_view(self, msg_id: int) -> None:
self.controller.update_screen()
return

def _handle_update_display_settings(self, event: Event) -> None:
"""
Handle change to user display setting (Eg: Time format)
"""
assert event['type'] == "update_display_settings"
view = self.controller.view
if event['setting_name'] == 'twenty_four_hour_time':
self.twenty_four_hr_format = event['setting']
for msg_w in view.message_view.log:
msg_box = msg_w.original_widget
msg_id = msg_box.message['id']
last_msg = msg_box.last_message
msg_pos = view.message_view.log.index(msg_w)
msg_w_list = create_msg_box_list(self, [msg_id],
last_message=last_msg)
view.message_view.log[msg_pos] = msg_w_list[0]
self.controller.update_screen()

def _register_desired_events(self, *, fetch_data: bool=False) -> str:
fetch_types = None if not fetch_data else [
'realm',
Expand All @@ -1232,6 +1255,7 @@ def _register_desired_events(self, *, fetch_data: bool=False) -> str:
'muted_topics',
'realm_user', # Enables cross_realm_bots
'realm_user_groups',
'update_display_settings',
# zulip_version and zulip_feature_level are always returned in
# POST /register from Feature level 3.
'zulip_version',
Expand Down
2 changes: 1 addition & 1 deletion zulipterminal/ui_tools/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ def main_view(self) -> List[Any]:

content_header = urwid.Columns([
('weight', 10, urwid.Text(text['author'])),
(23, urwid.Text(text['time'], align='right')),
(26, urwid.Text(text['time'], align='right')),
(1, urwid.Text(text['star'], align='right')),
], dividechars=1)
else:
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,8 +1392,8 @@ def _make_edit_block(self, snapshot: Dict[str, Any],
]
subheader = [
urwid.Text(('edit_author', author)),
# 19 = len(timestamp).
(19, urwid.Text(('edit_time', date_and_time), align='right')),
# 22 = len(timestamp).
(22, urwid.Text(('edit_time', date_and_time), align='right')),
]

edit_block = [
Expand Down