Skip to content

Instantly share code, notes, and snippets.

@rowend36
Created October 14, 2024 14:43
Show Gist options
  • Save rowend36/cdef895d7de344516957e99675a2800e to your computer and use it in GitHub Desktop.
Save rowend36/cdef895d7de344516957e99675a2800e to your computer and use it in GitHub Desktop.
This restores your layout in Windows Terminal so you can pick up where you started using Windows Terminal Command-Line. It depends on Git Bash but you can launch pretty much any terminal.
#!/usr/bin/env python
"""
Example configuration file:
- config.json
[
"C:\\Users\\USER\\Documents\\Work\\app-backend", # Add an optional root folder - DEFAULTS to current directory
# This panes will be arranged horizontally
{
"title": "Django Server",
"commands": [
". .venv/Scripts/activate",
"python manage.py runserver"
]
},
{}, # An empty interactive terminal pane
[
# This panes will be arranged vertically
{
"profile": "Windows PowerShell",
"title": "Expo",
"folder": "../app-mobile",
"commands": [
# PS: Don't keep commands that exit immediately on a separate pane. It skews width calculations.
"adb reverse tcp:8000 tcp:8000 || echo 'Failed to reverse TCP'",
"npm start"
]
},
{
"title": "SQL Logs",
"commands": [
"tail -f sqllogs.pipe"
]
}
]
]
Result:
_________________________________________
| | | Expo |
|Django Server | Terminal |-----------|
|_______________|_____________|_SQL Logs__|
"""
from random import randint
import os
import json
import sys
VERTICAL = 0
HORIZONTAL = 1
id = randint(1, 1000) * 10
COMMAND = []
def splits(
panes,
as_split,
direction,
first_pane,
root_folder=os.curdir,
size=1,
):
if type(panes) == list:
if type(panes[0]) == str:
folder = panes[0]
if not os.path.isabs(folder):
folder = os.path.join(root_folder, folder)
root_folder = folder
panes = panes[1:]
if first_pane:
splits(panes[0], as_split, direction, True, root_folder, size)
return
for i in range(1, len(panes)):
splits(
panes[i],
True,
1 - direction,
True,
root_folder,
float(len(panes) - i) / (len(panes) - i + 1),
)
for i in range(1, len(panes)):
if direction == VERTICAL:
COMMAND.append(" move-focus up")
elif direction == HORIZONTAL:
COMMAND.append(" move-focus left")
splits(panes[0], as_split, 1 - direction, False, root_folder, size)
for i in range(1, len(panes)):
if direction == VERTICAL:
COMMAND.append(" move-focus down")
elif direction == HORIZONTAL:
COMMAND.append(" move-focus right")
splits(panes[i], True, 1 - direction, False, root_folder, size)
elif first_pane:
command = ""
if as_split:
command += " split-pane "
if direction == VERTICAL:
command += "-V "
if direction == HORIZONTAL:
command += "-H "
if size != 1:
command += "-s {size} ".format(size=size)
else:
command = " new-tab"
if "title" in panes:
command += " --title " + json.dumps(panes["title"])
folder = panes.get("folder", ".")
if not os.path.isabs(folder):
folder = os.path.normpath(os.path.join(root_folder, folder))
command += ' -d "{folder}" '.format(
folder=folder,
)
if not "commands" in panes:
command += " -p '{profile}' ".format(
profile=panes.get("profile", "Git Bash"),
)
else:
command += '"C:\\Program Files\\Git\\bin\\bash.exe" -c "{command}" '.format(
command=" && ".join(
panes.get("commands", [])
+ [
"echo Done. PRESS Enter to exit || echo Failed. PRESS Enter to exit && read"
]
)
)
COMMAND.append(command)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="A simple program to drop you right where you left off every time you start. Don't hibernate - use Lazarus.",
epilog="Written by Rowend Duke",
)
parser.add_argument(
"terminal",
help="A json file containing your workspace.",
nargs="+",
type=argparse.FileType("r", encoding="UTF-8"),
)
parser.add_argument(
"-d", "--debug", help="Don't run just show commands", action="store_true"
)
parser.add_argument(
"-n", "--no-fullscreen", help="Don't take full screen", action="store_false", dest="fullscreen"
)
values = parser.parse_args()
for i in values.terminal:
window = json.load(i)
splits(window, False, HORIZONTAL, True)
splits(window, True, HORIZONTAL, False)
if values.debug:
print(json.dumps(
("-F" if values.fullscreen else "")
+ " \;\n ".format(id=id).join(COMMAND)
))
else:
os.startfile(
"C://Program Files/Git/bin/bash.exe",
arguments=" -c "
+ json.dumps(
"wt.exe -w {id} ".format(id=id)
+ ("-F" if values.fullscreen else "")
+ " \; ".format(id=id).join(COMMAND)
),
)
parser.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment