|
16 | 16 | from click.exceptions import FileError |
17 | 17 |
|
18 | 18 | from libtmux.common import has_gte_version, has_minimum_version, which |
19 | | -from libtmux.exc import LibTmuxException, TmuxCommandNotFound |
| 19 | +from libtmux.exc import TmuxCommandNotFound |
20 | 20 | from libtmux.server import Server |
21 | 21 |
|
22 | | -from . import config, exc, log, util |
| 22 | +from . import config, exc, log, shell, util |
23 | 23 | from .__about__ import __version__ |
24 | 24 | from ._compat import string_types |
25 | 25 | from .workspacebuilder import WorkspaceBuilder, freeze |
@@ -682,64 +682,19 @@ def command_shell(session_name, window_name, socket_name, socket_path, command): |
682 | 682 | """ |
683 | 683 | server = Server(socket_name=socket_name, socket_path=socket_path) |
684 | 684 |
|
685 | | - try: |
686 | | - server.sessions |
687 | | - except LibTmuxException as e: |
688 | | - if 'No such file or directory' in str(e): |
689 | | - raise LibTmuxException( |
690 | | - 'no tmux session found. Start a tmux session and try again. \n' |
691 | | - 'Original error: ' + str(e) |
692 | | - ) |
693 | | - else: |
694 | | - raise e |
| 685 | + shell.raise_if_tmux_not_running(server=server) |
695 | 686 |
|
696 | | - current_pane = None |
697 | | - if os.getenv('TMUX_PANE') is not None: |
698 | | - try: |
699 | | - current_pane = [ |
700 | | - p |
701 | | - for p in server._list_panes() |
702 | | - if p.get('pane_id') == os.getenv('TMUX_PANE') |
703 | | - ][0] |
704 | | - except IndexError: |
705 | | - pass |
706 | | - |
707 | | - try: |
708 | | - if session_name: |
709 | | - session = server.find_where({'session_name': session_name}) |
710 | | - elif current_pane is not None: |
711 | | - session = server.find_where({'session_id': current_pane['session_id']}) |
712 | | - else: |
713 | | - session = server.list_sessions()[0] |
714 | | - |
715 | | - if not session: |
716 | | - raise exc.TmuxpException('Session not found: %s' % session_name) |
717 | | - except exc.TmuxpException as e: |
718 | | - print(e) |
719 | | - return |
| 687 | + current_pane = shell.get_current_pane(server=server) |
720 | 688 |
|
721 | | - try: |
722 | | - if window_name: |
723 | | - window = session.find_where({'window_name': window_name}) |
724 | | - if not window: |
725 | | - raise exc.TmuxpException('Window not found: %s' % window_name) |
726 | | - elif current_pane is not None: |
727 | | - window = session.find_where({'window_id': current_pane['window_id']}) |
728 | | - else: |
729 | | - window = session.list_windows()[0] |
| 689 | + session = shell.get_session( |
| 690 | + server=server, session_name=session_name, current_pane=current_pane |
| 691 | + ) |
730 | 692 |
|
731 | | - except exc.TmuxpException as e: |
732 | | - print(e) |
733 | | - return |
| 693 | + window = shell.get_window( |
| 694 | + session=session, window_name=window_name, current_pane=current_pane |
| 695 | + ) |
734 | 696 |
|
735 | | - try: |
736 | | - if current_pane is not None: |
737 | | - pane = window.find_where({'pane_id': current_pane['pane_id']}) # NOQA: F841 |
738 | | - else: |
739 | | - pane = window.attached_pane # NOQA: F841 |
740 | | - except exc.TmuxpException as e: |
741 | | - print(e) |
742 | | - return |
| 697 | + pane = shell.get_pane(window=window, current_pane=current_pane) # NOQA: F841 |
743 | 698 |
|
744 | 699 | if command is not None: |
745 | 700 | exec(command) |
@@ -779,64 +734,19 @@ def command_shell_plus( |
779 | 734 | """ |
780 | 735 | server = Server(socket_name=socket_name, socket_path=socket_path) |
781 | 736 |
|
782 | | - try: |
783 | | - server.sessions |
784 | | - except LibTmuxException as e: |
785 | | - if 'No such file or directory' in str(e): |
786 | | - raise LibTmuxException( |
787 | | - 'no tmux session found. Start a tmux session and try again. \n' |
788 | | - 'Original error: ' + str(e) |
789 | | - ) |
790 | | - else: |
791 | | - raise e |
| 737 | + shell.raise_if_tmux_not_running(server=server) |
792 | 738 |
|
793 | | - current_pane = None |
794 | | - if os.getenv('TMUX_PANE') is not None: |
795 | | - try: |
796 | | - current_pane = [ |
797 | | - p |
798 | | - for p in server._list_panes() |
799 | | - if p.get('pane_id') == os.getenv('TMUX_PANE') |
800 | | - ][0] |
801 | | - except IndexError: |
802 | | - pass |
803 | | - |
804 | | - try: |
805 | | - if session_name: |
806 | | - session = server.find_where({'session_name': session_name}) |
807 | | - elif current_pane is not None: |
808 | | - session = server.find_where({'session_id': current_pane['session_id']}) |
809 | | - else: |
810 | | - session = server.list_sessions()[0] |
811 | | - |
812 | | - if not session: |
813 | | - raise exc.TmuxpException('Session not found: %s' % session_name) |
814 | | - except exc.TmuxpException as e: |
815 | | - print(e) |
816 | | - return |
| 739 | + current_pane = shell.get_current_pane(server=server) |
817 | 740 |
|
818 | | - try: |
819 | | - if window_name: |
820 | | - window = session.find_where({'window_name': window_name}) |
821 | | - if not window: |
822 | | - raise exc.TmuxpException('Window not found: %s' % window_name) |
823 | | - elif current_pane is not None: |
824 | | - window = session.find_where({'window_id': current_pane['window_id']}) |
825 | | - else: |
826 | | - window = session.list_windows()[0] |
| 741 | + session = shell.get_session( |
| 742 | + server=server, session_name=session_name, current_pane=current_pane |
| 743 | + ) |
827 | 744 |
|
828 | | - except exc.TmuxpException as e: |
829 | | - print(e) |
830 | | - return |
| 745 | + window = shell.get_window( |
| 746 | + session=session, window_name=window_name, current_pane=current_pane |
| 747 | + ) |
831 | 748 |
|
832 | | - try: |
833 | | - if current_pane is not None: |
834 | | - pane = window.find_where({'pane_id': current_pane['pane_id']}) # NOQA: F841 |
835 | | - else: |
836 | | - pane = window.attached_pane # NOQA: F841 |
837 | | - except exc.TmuxpException as e: |
838 | | - print(e) |
839 | | - return |
| 749 | + pane = shell.get_pane(window=window, current_pane=current_pane) # NOQA: F841 |
840 | 750 |
|
841 | 751 | if command is not None: |
842 | 752 | exec(command) |
|
0 commit comments