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

Added conditional_before_commands #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions catmux/resources/example_session.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
---
parameters:
# Parameters specified here can be used below and they can be overwritten at runtime
show_layouts: false # This parameter is used together with the if flag below
conditional_command: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, a more descriptive name would be good, so it is clear that the name is an arbitrary example, rather than a functionality. E.g. print_additional_intro.

replacement_param: world\! # Parameters can also be used as replacement templates

common:
# before_commands will be executed in each pane before doing anything else. Use this e.g. for
# - sourcing an environment
# - ssh-ing to another machine
before_commands:
- echo "This is a catmux window :)" # before commands can be specified global and per window
- if: conditional_command
command: echo "This is conditional"

# The default window will be the one displayed when first connecting to the tmux session
default_window: some_other_window
parameters:
# Parameters specified here can be used below and they can be overwritten at runtime
show_layouts: false # This parameter is used together with the if flag below
replacement_param: world\! # Parameters can also be used as replacement templates

# Everything will be opened in windows. Windows can have multiple splits.
windows:
- name: tiled
Expand Down
23 changes: 21 additions & 2 deletions catmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def init_from_yaml(self, yaml_data):
"""Initialize config directly by an already loaded yaml structure."""

self.__yaml_data = yaml_data
self._parse_common()
self._parse_parameters()
self._parse_common()
self._parse_windows()

def run(self, parent_server: libtmux.Server, debug=False):
Expand Down Expand Up @@ -101,11 +101,30 @@ def _parse_common(self):
if "common" in self.__yaml_data and self.__yaml_data["common"]:
common = self.__yaml_data["common"]
if "before_commands" in common and common["before_commands"]:
self._before_commands = common["before_commands"]
# self._before_commands = common["before_commands"]
self._parse_before_commands(common["before_commands"])
Comment on lines +104 to +105
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to keep old code around.

Suggested change
# self._before_commands = common["before_commands"]
self._parse_before_commands(common["before_commands"])
self._parse_before_commands(common["before_commands"])


if "default_window" in common and common["default_window"]:
self._default_window = common["default_window"]

def _parse_before_commands(self, commands):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is parsing before_commands different to commands? Maybe we should unify the concepts? In the end, both should be the same, just parsed from a different section of the config. This would obviously mean that commands would also receive an option to make them conditional, but that's not necessarily a bad thing,

Copy link
Contributor Author

@nspielbau nspielbau Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we could also extend this to the normal commands but I'll need to have a closer look at that

Sorry for all the editing I got confused :D

command_list = []
for command in commands:
if isinstance(command, str):
command_list.append(command)
elif isinstance(command, dict):
conditional_param = command.get("if")
print(conditional_param)
if conditional_param not in self._parameters:
print(
"Skipping command because parameter "
+ conditional_param
+ " was not found."
)
elif self._parameters[conditional_param]:
command_list.append(command.get("command"))
self._before_commands = command_list

def _parse_overwrites(self, data_string):
"""Separates a comma-separated list of foo=val1,bar=val2 into a dictionary."""
if data_string is None:
Expand Down
24 changes: 24 additions & 0 deletions test/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ def test_empty_before_commands():
assert session._default_window == None


def test_conditional_before_commands():
CONFIG = """---
parameters:
param_true: true
param_false: false
common:
before_commands:
- if: param_true
command: echo "true"
- if: param_false_
command: echo "false"
- if: param_does_not_exist
command: echo "missing"
windows:
- name: foo
commands:
- echo "bar"
"""
session = Session("name")
session.init_from_yaml(yaml.safe_load(CONFIG))

assert len(session._before_commands) == 1


def test_missing_parameter_for_if_unless():
CONFIG = """common:
before_commands:
Expand Down
Loading