1- # Copyright (c) 2020-2022 The MathWorks, Inc.
1+ # Copyright (c) 2020-2025 The MathWorks, Inc.
22# Script to print information about all running matlab-proxy servers for current user on current machine.
33
44import glob
77import matlab_proxy .settings as mwi_settings
88import matlab_proxy .util as mwi_util
99
10+ from datetime import datetime
11+ from rich .console import Console
12+ from rich .table import Table
13+
14+ __NO_SERVERS_MSG = "No MATLAB-PROXY Servers are currently running."
15+
16+
17+ def _extract_version_and_session (title ):
18+ """Extracts session name and MATLAB version from the title."""
19+ parts = title .split ("-" )
20+ if len (parts ) < 2 :
21+ return title .replace ("MATLAB " , "" ), ""
22+ session_name = parts [0 ].strip ()
23+ matlab_version = parts [1 ].strip ().replace ("MATLAB " , "" )
24+ return matlab_version , session_name
25+
26+
27+ def _get_server_info (server ):
28+ """Helper function to parse info from server file."""
29+ with open (server ) as f :
30+ # Assumes that the server file contains the address on the first line,
31+ # the browser_title on the second line, and the timestamp is derived from the file's last modified time.
32+ address = f .readline ().strip ()
33+ browser_title = f .readline ().strip ()
34+ matlab_version , session_name = _extract_version_and_session (browser_title )
35+ timestamp = _get_timestamp (server )
36+ return timestamp , matlab_version , session_name , address
37+
38+
39+ def _print_server_info_as_table (servers ):
40+ console = Console ()
41+ table = Table (
42+ title = "MATLAB Proxy Servers" ,
43+ title_style = "cyan" ,
44+ title_justify = "center" ,
45+ caption = "No servers found." if not servers else "" ,
46+ caption_style = "bold red" ,
47+ show_header = True ,
48+ header_style = "yellow" ,
49+ show_lines = True ,
50+ show_edge = True ,
51+ )
52+ table .add_column ("Created On" )
53+ table .add_column ("MATLAB\n Version" )
54+ table .add_column ("Session Name" )
55+ table .add_column ("Server URL" , overflow = "fold" )
56+
57+ # Build server information
58+ for server in servers :
59+ table .add_row (* _get_server_info (server ))
60+
61+ console .print (table )
62+
63+
64+ def _get_timestamp (filename ):
65+ """Get the last modified timestamp of the file in a human-readable format."""
66+ timestamp = os .path .getmtime (filename )
67+ readable_time = datetime .fromtimestamp (timestamp ).strftime ("%d/%m/%y %H:%M:%S" )
68+ return readable_time
69+
1070
1171def print_server_info ():
1272 """Print information about all matlab-proxy servers (with version > 0.4.0) running on this machine"""
@@ -15,30 +75,15 @@ def print_server_info():
1575 # Look for files in port folders
1676 ports_folder = home_folder / "ports"
1777 search_string = str (ports_folder ) + "/**/mwi_server.info"
78+ servers = sorted (glob .glob (search_string ), key = os .path .getmtime )
1879
19- print_output = str (
20- mwi_util .prettify (
21- boundary_filler = "-" ,
22- text_arr = ["Your running servers are:" ],
23- )
24- )
25- print_output += "\n "
26- search_results = sorted (glob .glob (search_string ), key = os .path .getmtime )
27- if len (search_results ) == 0 :
28- print_output += "No MATLAB-PROXY Servers are currently running."
29- else :
30- server_number = 0
31- for server in search_results :
32- server_number += 1
80+ args = mwi_util .parse_list_cli_args ()
81+
82+ if args ["quiet" ]:
83+ for server in servers :
3384 with open (server ) as f :
3485 server_info = f .readline ().strip ()
35- print_output += str (server_number ) + ". " + str (server_info ) + "\n "
36-
37- print_output += str (
38- mwi_util .prettify (
39- boundary_filler = "-" ,
40- text_arr = ["Thank you." ],
41- )
42- )
43-
44- return print_output
86+ print (f"{ server_info } " , end = "\n " )
87+ else :
88+ _print_server_info_as_table (servers )
89+ return
0 commit comments