45
45
from zulipterminal .server_url import near_message_url
46
46
from zulipterminal .ui_tools .boxes import PanelSearchBox
47
47
from zulipterminal .ui_tools .buttons import (
48
+ CodeSnippetButton ,
48
49
EmojiButton ,
49
50
HomeButton ,
50
51
MentionedButton ,
@@ -1548,11 +1549,13 @@ def __init__(
1548
1549
title : str ,
1549
1550
topic_links : Dict [str , Tuple [str , int , bool ]],
1550
1551
message_links : Dict [str , Tuple [str , int , bool ]],
1552
+ code_snippets : List [Tuple [str , List [Tuple [str , str ]]]],
1551
1553
time_mentions : List [Tuple [str , str ]],
1552
1554
) -> None :
1553
1555
self .msg = msg
1554
1556
self .topic_links = topic_links
1555
1557
self .message_links = message_links
1558
+ self .code_snippets = code_snippets
1556
1559
self .time_mentions = time_mentions
1557
1560
self .server_url = controller .model .server_url
1558
1561
date_and_time = controller .model .formatted_local_time (
@@ -1568,6 +1571,9 @@ def __init__(
1568
1571
full_raw_message_keys = "[{}]" .format (
1569
1572
", " .join (map (str , keys_for_command ("FULL_RAW_MESSAGE" )))
1570
1573
)
1574
+ copy_code_keys = "[{}]" .format (
1575
+ ", " .join (map (str , keys_for_command ("COPY_CODE_SNIPPET" )))
1576
+ )
1571
1577
msg_info = [
1572
1578
(
1573
1579
"" ,
@@ -1586,6 +1592,7 @@ def __init__(
1586
1592
("Open in web browser" , view_in_browser_keys ),
1587
1593
("Full rendered message" , full_rendered_message_keys ),
1588
1594
("Full raw message" , full_raw_message_keys ),
1595
+ ("Copy code to clipboard" , copy_code_keys ),
1589
1596
],
1590
1597
)
1591
1598
msg_info .append (viewing_actions )
@@ -1607,6 +1614,8 @@ def __init__(
1607
1614
msg_info .append (("Topic Links" , []))
1608
1615
if time_mentions :
1609
1616
msg_info .append (("Time mentions" , time_mentions ))
1617
+ if code_snippets :
1618
+ msg_info .append (("Code Snippets" , []))
1610
1619
if msg ["reactions" ]:
1611
1620
reactions = sorted (
1612
1621
(reaction ["emoji_name" ], reaction ["user" ]["full_name" ])
@@ -1644,7 +1653,6 @@ def __init__(
1644
1653
widgets [:slice_index ] + message_link_widgets + widgets [slice_index :]
1645
1654
)
1646
1655
popup_width = max (popup_width , message_link_width )
1647
-
1648
1656
if topic_links :
1649
1657
topic_link_widgets , topic_link_width = self .create_link_buttons (
1650
1658
controller , topic_links
@@ -1660,6 +1668,21 @@ def __init__(
1660
1668
widgets = widgets [:slice_index ] + topic_link_widgets + widgets [slice_index :]
1661
1669
popup_width = max (popup_width , topic_link_width )
1662
1670
1671
+ if code_snippets :
1672
+ (
1673
+ code_snippets_widgets ,
1674
+ code_snippets_width ,
1675
+ ) = self .create_code_snippet_buttons (controller , code_snippets )
1676
+ # slice_index = Number of labels before code snippets + 1 newline
1677
+ # + 1 'Code Snippets' category label.
1678
+ slice_index = len (msg_info [0 ][1 ]) + len (msg_info [1 ][1 ]) + 2 + 2
1679
+ slice_index += sum ([len (w ) + 2 for w in self .button_widgets ])
1680
+ self .button_widgets .append (code_snippets )
1681
+ widgets = (
1682
+ widgets [:slice_index ] + code_snippets_widgets + widgets [slice_index :]
1683
+ )
1684
+ popup_width = max (popup_width , code_snippets_width )
1685
+
1663
1686
super ().__init__ (controller , widgets , "MSG_INFO" , popup_width , title )
1664
1687
1665
1688
@staticmethod
@@ -1689,12 +1712,52 @@ def create_link_buttons(
1689
1712
1690
1713
return link_widgets , link_width
1691
1714
1715
+ def create_code_snippet_buttons (
1716
+ self , controller : Any , code_snippets : List [Tuple [str , List [Tuple [str , str ]]]]
1717
+ ) -> Tuple [List [Any ], int ]:
1718
+ code_snippet_widgets = []
1719
+ code_snippet_width = 0
1720
+
1721
+ for index , snippet in enumerate (code_snippets ):
1722
+ language , snippet_list = snippet
1723
+ language = "" if language is None else language
1724
+ display_code , copy_code = CodeSnippetButton .get_code_from_snippet (
1725
+ self , snippet_list
1726
+ )
1727
+ if display_code :
1728
+ code_snippet_width = max (
1729
+ code_snippet_width , len (max (display_code , key = len ))
1730
+ )
1731
+ display_code [- 1 ] = (
1732
+ display_code [- 1 ][0 ],
1733
+ display_code [- 1 ][1 ].rstrip ("\n " ),
1734
+ )
1735
+ caption = f"{ str (index + 1 )} : { language } \n "
1736
+
1737
+ display_attr = None if index % 2 else "popup_contrast"
1738
+ display_code = [("pygments:w" , caption )] + display_code
1739
+ code_snippet_widgets .append (
1740
+ CodeSnippetButton (
1741
+ controller = controller ,
1742
+ caption = caption ,
1743
+ display_code = display_code ,
1744
+ copy_code = copy_code ,
1745
+ display_attr = display_attr ,
1746
+ )
1747
+ )
1748
+ code = caption + str (snip [1 ] for snip in snippet_list )
1749
+ code_snippet_width = max (
1750
+ code_snippet_width , len (max (code .split ("\n " ), key = len ))
1751
+ )
1752
+ return code_snippet_widgets , code_snippet_width
1753
+
1692
1754
def keypress (self , size : urwid_Size , key : str ) -> str :
1693
1755
if is_command_key ("EDIT_HISTORY" , key ) and self .show_edit_history_label :
1694
1756
self .controller .show_edit_history (
1695
1757
message = self .msg ,
1696
1758
topic_links = self .topic_links ,
1697
1759
message_links = self .message_links ,
1760
+ code_snippets = self .code_snippets ,
1698
1761
time_mentions = self .time_mentions ,
1699
1762
)
1700
1763
elif is_command_key ("VIEW_IN_BROWSER" , key ):
@@ -1705,6 +1768,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
1705
1768
message = self .msg ,
1706
1769
topic_links = self .topic_links ,
1707
1770
message_links = self .message_links ,
1771
+ code_snippets = self .code_snippets ,
1708
1772
time_mentions = self .time_mentions ,
1709
1773
)
1710
1774
return key
@@ -1713,6 +1777,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
1713
1777
message = self .msg ,
1714
1778
topic_links = self .topic_links ,
1715
1779
message_links = self .message_links ,
1780
+ code_snippets = self .code_snippets ,
1716
1781
time_mentions = self .time_mentions ,
1717
1782
)
1718
1783
return key
@@ -1762,13 +1827,15 @@ def __init__(
1762
1827
message : Message ,
1763
1828
topic_links : Dict [str , Tuple [str , int , bool ]],
1764
1829
message_links : Dict [str , Tuple [str , int , bool ]],
1830
+ code_snippets : List [Tuple [str , List [Tuple [str , str ]]]],
1765
1831
time_mentions : List [Tuple [str , str ]],
1766
1832
title : str ,
1767
1833
) -> None :
1768
1834
self .controller = controller
1769
1835
self .message = message
1770
1836
self .topic_links = topic_links
1771
1837
self .message_links = message_links
1838
+ self .code_snippets = code_snippets
1772
1839
self .time_mentions = time_mentions
1773
1840
width = 64
1774
1841
widgets : List [Any ] = []
@@ -1867,6 +1934,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
1867
1934
msg = self .message ,
1868
1935
topic_links = self .topic_links ,
1869
1936
message_links = self .message_links ,
1937
+ code_snippets = self .code_snippets ,
1870
1938
time_mentions = self .time_mentions ,
1871
1939
)
1872
1940
return key
@@ -1880,13 +1948,15 @@ def __init__(
1880
1948
message : Message ,
1881
1949
topic_links : Dict [str , Tuple [str , int , bool ]],
1882
1950
message_links : Dict [str , Tuple [str , int , bool ]],
1951
+ code_snippets : List [Tuple [str , List [Tuple [str , str ]]]],
1883
1952
time_mentions : List [Tuple [str , str ]],
1884
1953
title : str ,
1885
1954
) -> None :
1886
1955
self .controller = controller
1887
1956
self .message = message
1888
1957
self .topic_links = topic_links
1889
1958
self .message_links = message_links
1959
+ self .code_snippets = code_snippets
1890
1960
self .time_mentions = time_mentions
1891
1961
max_cols , max_rows = controller .maximum_popup_dimensions ()
1892
1962
@@ -1911,6 +1981,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
1911
1981
msg = self .message ,
1912
1982
topic_links = self .topic_links ,
1913
1983
message_links = self .message_links ,
1984
+ code_snippets = self .code_snippets ,
1914
1985
time_mentions = self .time_mentions ,
1915
1986
)
1916
1987
return key
@@ -1924,13 +1995,15 @@ def __init__(
1924
1995
message : Message ,
1925
1996
topic_links : Dict [str , Tuple [str , int , bool ]],
1926
1997
message_links : Dict [str , Tuple [str , int , bool ]],
1998
+ code_snippets : List [Tuple [str , List [Tuple [str , str ]]]],
1927
1999
time_mentions : List [Tuple [str , str ]],
1928
2000
title : str ,
1929
2001
) -> None :
1930
2002
self .controller = controller
1931
2003
self .message = message
1932
2004
self .topic_links = topic_links
1933
2005
self .message_links = message_links
2006
+ self .code_snippets = code_snippets
1934
2007
self .time_mentions = time_mentions
1935
2008
max_cols , max_rows = controller .maximum_popup_dimensions ()
1936
2009
@@ -1961,6 +2034,7 @@ def keypress(self, size: urwid_Size, key: str) -> str:
1961
2034
msg = self .message ,
1962
2035
topic_links = self .topic_links ,
1963
2036
message_links = self .message_links ,
2037
+ code_snippets = self .code_snippets ,
1964
2038
time_mentions = self .time_mentions ,
1965
2039
)
1966
2040
return key
0 commit comments