def format_pymux_string(pymux, string, window=None, pane=None):
arrangement = pymux.arrangement<EOL>if window is None:<EOL><INDENT>window = arrangement.get_active_window()<EOL><DEDENT>if pane is None:<EOL><INDENT>pane = window.active_pane<EOL><DEDENT>def id_of_pane():<EOL><INDENT>return '<STR_LIT:%s>' % (pane.pane_id, )<EOL><DEDENT>def index_of_pane():<EOL><INDENT>try:<EOL><INDENT>return '<STR_LIT:%s>' % (window.get_pane_index(pane), )<EOL><DEDENT>except ValueError:<EOL><INDENT>return '<STR_LIT:/>'<EOL><DEDENT><DEDENT>def index_of_window():<EOL><INDENT>return '<STR_LIT:%s>' % (window.index, )<EOL><DEDENT>def name_of_window():<EOL><INDENT>return window.name or '<STR_LIT>'<EOL><DEDENT>def window_flags():<EOL><INDENT>z = '<STR_LIT>' if window.zoom else '<STR_LIT>'<EOL>if window == arrangement.get_active_window():<EOL><INDENT>return '<STR_LIT:*>' + z<EOL><DEDENT>elif window == arrangement.get_previous_active_window():<EOL><INDENT>return '<STR_LIT:->' + z<EOL><DEDENT>else:<EOL><INDENT>return z + '<STR_LIT:U+0020>'<EOL><DEDENT><DEDENT>def name_of_session():<EOL><INDENT>return pymux.session_name<EOL><DEDENT>def title_of_pane():<EOL><INDENT>return pane.process.screen.title<EOL><DEDENT>def hostname():<EOL><INDENT>return socket.gethostname()<EOL><DEDENT>def literal():<EOL><INDENT>return '<STR_LIT:#>'<EOL><DEDENT>format_table = {<EOL>'<STR_LIT>': id_of_pane,<EOL>'<STR_LIT>': window_flags,<EOL>'<STR_LIT>': index_of_window,<EOL>'<STR_LIT>': index_of_pane,<EOL>'<STR_LIT>': name_of_session,<EOL>'<STR_LIT>': title_of_pane,<EOL>'<STR_LIT>': name_of_window,<EOL>'<STR_LIT>': hostname,<EOL>'<STR_LIT>': literal,<EOL>}<EOL>if '<STR_LIT:%>' in string:<EOL><INDENT>try:<EOL><INDENT>if six.PY2:<EOL><INDENT>string = datetime.datetime.now().strftime(<EOL>string.encode('<STR_LIT:utf-8>')).decode('<STR_LIT:utf-8>')<EOL><DEDENT>else:<EOL><INDENT>string = datetime.datetime.now().strftime(string)<EOL><DEDENT><DEDENT>except ValueError:  <EOL><INDENT>string = '<STR_LIT>'<EOL><DEDENT><DEDENT>for symbol, f in format_table.items():<EOL><INDENT>if symbol in string:<EOL><INDENT>string = string.replace(symbol, f())<EOL><DEDENT><DEDENT>return string<EOL>
Apply pymux sting formatting. (Similar to tmux.)
E.g.  #P is replaced by the index of the active pane.

We try to stay compatible with tmux, if possible.
One thing that we won't support (for now) is colors, because our styling
works different. (With a Style class.) On the other hand, in the future, we
could allow things like `#[token=Token.Title.PID]`. This gives a clean
separation of semantics and colors, making it easy to write different color
schemes.