Skip to content

Commit

Permalink
Merge 55b4bc4 into 7f28caa
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Haylett committed Nov 7, 2019
2 parents 7f28caa + 55b4bc4 commit dd8fa78
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
57 changes: 51 additions & 6 deletions i3_resurrect/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import json
import os
import shlex
import subprocess
import sys
import tempfile
from pathlib import Path

import click
import i3ipc
from natsort import natsorted
import psutil

from . import config
Expand Down Expand Up @@ -207,7 +206,7 @@ def restore_programs(workspace, directory, profile):
else:
util.eprint('Could not find saved programs for workspace '
f'"{workspace}"')
return
sys.exit(1)

for entry in programs:
cmdline = entry['command']
Expand All @@ -229,7 +228,7 @@ def restore_programs(workspace, directory, profile):
else:
command = cmdline

# Execute command as subprocess.
# Execute command via i3 exec.
i3.command(f'exec cd "{working_directory}" && {command}')


Expand All @@ -246,13 +245,15 @@ def restore_layout(workspace, directory, profile):
layout = None
try:
layout = json.loads(layout_file.read_text())
if layout == {}:
return
except FileNotFoundError:
if profile is not None:
util.eprint(f'Could not find saved layout for profile "{profile}"')
else:
util.eprint('Could not find saved layout for workspace '
f'"{workspace}"')
return
sys.exit(1)

window_ids = []
placeholder_window_ids = []
Expand Down Expand Up @@ -323,5 +324,49 @@ def restore_layout(workspace, directory, profile):
util.xdo_map_window(window_id)


@main.command('ls')
@click.option('--directory', '-d',
type=click.Path(file_okay=False),
default=Path('~/.i3/i3-resurrect/').expanduser(),
help=('The directory to search in.\n'
'[default: ~/.i3/i3-resurrect]'))
@click.argument('item',
type=click.Choice(['workspaces', 'profiles']),
default='workspaces')
def list_workspaces(directory, item):
"""
List saved workspaces or profiles.
"""
if item == 'workspaces':
directory = Path(directory)
workspaces = []
for entry in directory.iterdir():
if entry.is_file():
tokens = entry.name.split('_')
workspace = tokens[1]
temp = tokens[2]
file_type = temp[:temp.index('.json')]
workspaces.append(f'Workspace {workspace} {file_type}')
workspaces = natsorted(workspaces)
for workspace in workspaces:
print(workspace)
else:
directory = Path(directory) / 'profiles'
profiles = []
try:
for entry in directory.iterdir():
if entry.is_file():
tokens = entry.name.split('_')
profile = tokens[0]
temp = tokens[1]
file_type = temp[:temp.index('.json')]
profiles.append(f'Profile {profile} {file_type}')
profiles = natsorted(profiles)
for profile in profiles:
print(profile)
except FileNotFoundError:
print('No profiles found')


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pytest==5.0.1
tox==3.13.2
pytest-cov==2.7.1
coveralls==1.8.2
natsort==6.0.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
install_requires=[
'Click',
'i3ipc',
'natsort',
'psutil',
],
entry_points={
Expand Down
26 changes: 18 additions & 8 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ commands =
i3-resurrect save --help
i3-resurrect restore -h
i3-resurrect restore --help
i3-resurrect save -d /tmp/i3-resurrect-saves
i3-resurrect save -d /tmp/i3-resurrect-saves --swallow=class,instance,title
i3-resurrect save -d /tmp/i3-resurrect-saves --swallow=class,instance,title --layout-only
i3-resurrect save -d /tmp/i3-resurrect-saves --swallow=class,instance,title --programs-only
i3-resurrect restore -d /tmp/i3-resurrect-saves --programs-only
i3-resurrect restore -d /tmp/i3-resurrect-saves --layout-only
i3-resurrect save -d /tmp/i3-resurrect-saves -w "2 " --swallow=class,instance,title
i3-resurrect restore -d /tmp/i3-resurrect-saves -w "2 "
i3-resurrect save -d /tmp/i3-resurrect
i3-resurrect save -d /tmp/i3-resurrect --swallow=class,instance,title
i3-resurrect save -d /tmp/i3-resurrect --swallow=class,instance,title --layout-only
i3-resurrect save -d /tmp/i3-resurrect --swallow=class,instance,title --programs-only
sleep 0.5
i3-resurrect restore -d /tmp/i3-resurrect --programs-only
sleep 1
i3-resurrect restore -d /tmp/i3-resurrect --layout-only
sleep 0.5
i3-resurrect save -d /tmp/i3-resurrect -w "2 " --swallow=class,instance,title
sleep 1
i3-resurrect restore -d /tmp/i3-resurrect -w "2 "
sleep 0.5
i3-resurrect ls -d /tmp/i3-resurrect -h
i3-resurrect ls --help
i3-resurrect ls -d /tmp/i3-resurrect
i3-resurrect ls -d /tmp/i3-resurrect workspaces
i3-resurrect ls -d /tmp/i3-resurrect profiles

0 comments on commit dd8fa78

Please sign in to comment.