Skip to content
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

Here is a simple example to use this library. #170

Open
thinkycx opened this issue Jan 11, 2019 · 0 comments
Open

Here is a simple example to use this library. #170

thinkycx opened this issue Jan 11, 2019 · 0 comments

Comments

@thinkycx
Copy link

@thinkycx thinkycx commented Jan 11, 2019

When I use the library to split window, I use win.cmd('split-window', '-h') which let me spend a lot of time.

At last, I suggest to use bash script instead of this library.

#!/usr/bin/env python3
# coding=utf-8
# author: thinkycx
# date: 2018-01-11
# Usage:
#    run several commands in tmux panel, every window have 4 panes
#    $ pip3 install libtmux
#    $ python tmux.py


import libtmux
import math
import os
import logging

def create_session(session_name, commands):
    '''
        create session with 2*2 panes and run commands
    :param session_name: tmux session name
    :param commands: bash commands
    :return:
    '''
    logging.info(commands)
    # pane_NUM = 3
    # WINDOW_NUM = int(math.ceil(len(commands)/4.0))  # in python3, we can use 4 also

    server = libtmux.Server()
    session = server.new_session(session_name)

    # create windows
    windows = []
    panes = []

    for i in range(len(commands)):
        # create window to store 4 panes
        if i % 4 == 0:
            win = session.new_window(attach=False, window_name="win"+str(int(i/4)))
            windows.append(win)

            # tmux_args = ('-h',)
            win.cmd('split-window', '-h')
            win.cmd('split-window', '-f')
            win.cmd('split-window', '-h')

            panes.extend(win.list_panes())

        panes[i].send_keys(commands[i])

    logging.info(panes)
    logging.info(windows)


if __name__ == '__main__':
    commands = []
    for i in range(10):
        commands.append("echo " + str(i))

    os.system("tmux kill-session -t session")
    create_session("session", commands)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.