Skip to content

Commit

Permalink
feat: add custom command to config
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwjackson committed Apr 11, 2024
1 parent 1b349ed commit 3146e5e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 80 deletions.
29 changes: 14 additions & 15 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ pkgs.resholve.mkDerivation rec {
find $src -type f -exec bash -c 'file="$1"; install -Dm 755 "$file" "$out/''${file#$src/}"' -- {} \;
# substituteInPlace $out/bin/* \
# --replace /etc/tmesh $out/etc/tmesh
# mkdir -p $out/etc/tmesh
# cp $src/etc/tmesh/*.tmux.conf $out/etc/tmesh
substituteInPlace $out/bin/choose-session.sh \
--replace "@tmux" "${pkgs.lib.meta.getExe pkgs.tmux}" \
--replace "@jq" "${pkgs.lib.meta.getExe pkgs.jq}"
runHook postInstall
'';
Expand All @@ -38,38 +36,39 @@ pkgs.resholve.mkDerivation rec {
pkgs.fzf
]
++ (with pkgs; [
bash
coreutils
tmux
coreutils
gnused
jq
fd
findutils
bash
gawk
gnused
jq
nettools
tmux
yq-go
]);
fake = {
external = [
# Make sure we can self reference our scripts
"tmesh"
"tmesh-server"
"choose-host"
"choose-session"
"tmesh"
"tmesh-server"
];
};
execer = [
# resholve cannot verify args from these apps
"cannot:${pkgs.tmux}/bin/tmux"
"cannot:${pkgs.fzf}/bin/fzf"
"cannot:${pkgs.fd}/bin/fd"
"cannot:${pkgs.fzf}/bin/fzf"
"cannot:${pkgs.tmux}/bin/tmux"
"cannot:${pkgs.yq-go}/bin/yq"
];
};
};

meta = with pkgs.lib; {
homepage = "https://github.com/simonwjackson/tmesh";
description = "Effortlessly manage tmux sessions across multiple hosts.";
homepage = "https://github.com/simonwjackson/tmesh";
license = licenses.gpl2Only;
platforms = platforms.linux;
};
Expand Down
41 changes: 41 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
inputs: {
config,
lib,
pkgs,
...
}: let
inherit (pkgs.stdenv.hostPlatform) system;
pname = "tmesh";

package = inputs.self.packages.${system}.${pname};
cfg = config.services.${pname};
jsonConfig = builtins.toJSON cfg.settings;
jsonConfigFile = pkgs.writeText "config.json" jsonConfig;
in {
options.services.${pname} = {
enable = lib.mkEnableOption "${pname}";

settings = lib.mkOption {
type = lib.types.attrs;
default = {};
description = "Configuration settings.";
};

package = lib.mkOption {
type = lib.types.package;
default = package;
description = "The package to use for ${pname}.";
};
};

config = lib.mkIf cfg.enable {
environment.etc."${pname}/config.json".source = jsonConfigFile;

environment.systemPackages = [
(pkgs.writeScriptBin "${pname}-wrapped" ''
#!${pkgs.stdenv.shell}
exec ${pkgs.lib.meta.getExe cfg.package} --config ${jsonConfigFile} "$@"
'')
];
};
}
64 changes: 5 additions & 59 deletions src/bin/choose-session.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,18 @@ function replace_spaces_with_delimiter() {

export -f replace_spaces_with_delimiter

display_icon() {
icon_inactive=""
icon_active=""

if [ "$1" ]; then
echo "$icon_active"
else
echo "$icon_inactive"
fi
}

export -f display_icon

fzf_format_entry() {
local project_path="$1"
local project_name session_name json_obj tmux_status=""
local project_name json_obj tmux_status=""

project_root_dir=$(dirname "$project_path")
project_name=$(basename "$project_root_dir")
json_obj='{"path":"'"${project_root_dir}"'","type":"code","session":"'"${project_name}"'"}'

if tmux has-session -t "$session_name" >/dev/null 2>&1; then
tmux_status=""
else
if tmux -L "${TMESH_SOCKET}" has-session -t "${project_name}" >/dev/null 2>&1; then
tmux_status=""
else
tmux_status=""
fi

printf "%s;%s;%s\n" "${json_obj}" "${tmux_status}" "${project_name}" |
Expand All @@ -61,47 +48,6 @@ get_code_projects() {
--search-path "${code_path}"
}

# fzf_format_entry() {
# json_obj="$1"
# session_name=$(jq -r '.session' <<<"$json_obj")
# tmux_status=$(
# tmux has-session -t "$session_name" >/dev/null 2>&1 &&
# echo "" ||
# echo ""
# )
#
# printf "%s;%s;%s\n" "$json_obj" "$tmux_status" "$session_name" |
# replace_delimiter_with_spaces ';' 2
# }
#
# get_code_projects() {
# local code_path="${1}"
# fd \
# --type directory \
# --hidden '^.bare$|^.git$' \
# --search-path "${code_path}" |
# while read -r project_path; do
# parent_dir=$(basename "$(dirname "$project_path")")
# project_name=$(basename "$project_path")
# session_name="${parent_dir}/${project_name}"
# json_obj=$(printf '{ "path": "%s", "type": "code", "session": "%s" }' "$project_path" "$session_name")
# fzf_format_entry "$json_obj"
# done
# }

# {
# get_code_projects "/glacier/snowscape/code"
# } |
# fzf \
# --header Projects \
# --preview 'tmux capture-pane -e -pt $(jq -r ".session" <<< "{}" 2> /dev/null)' \
# --bind 'ctrl-c:abort' \
# --delimiter=$'\u2007' \
# --with-nth=2.. |
# replace_spaces_with_delimiter ';' 2 |
# awk -F';' '{print $1}'
# )

selection=$(
{
get_code_projects "/glacier/snowscape/code" |
Expand All @@ -111,7 +57,7 @@ selection=$(
} |
fzf \
--header Projects \
--preview 'tmux capture-pane -e -pt $(jq -r ".session" <<< "{}" 2> /dev/null)' \
--preview '@tmux capture-pane -e -pt $(@jq -r ".session" <<< "{}" 2> /dev/null)' \
--bind 'ctrl-c:abort' \
--delimiter=$'\u2007' \
--with-nth=2.. |
Expand Down
28 changes: 24 additions & 4 deletions src/bin/tmesh
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
#!/usr/bin/env bash

while [[ "$#" -gt 0 ]]; do
case $1 in
--config)
if [[ "$#" -gt 1 ]]; then
TMESH_CONFIG="$2"
shift 2
else
echo "Error: --config option requires an argument."
exit 1
fi
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
done

TMUX="${TMUX:-}"
TERM="${TERM:-}"
SERVER="${SERVER:=$(hostname)}"
SCRIPT_DIR="$(dirname "$0")"

TMESH_DEFAULT_SESSION=${TMESH_DEFAULT_SESSION=:-""}
TMESH_USER_CONFIG=${TMESH_USER_CONFIG:-}
TMESH_SOCKET=${TMESH_SOCKET:-"tmesh"}
export TMESH_SOCKET=${TMESH_SOCKET:-"tmesh"}

read -r -d '' tmesh_default_config <<EOM
# INFO: https://github.com/tmux/tmux/wiki/Clipboard#terminal-support---tmux-inside-tmux
Expand Down Expand Up @@ -35,13 +53,15 @@ tmux_config=(

tryStartTmesh() {
if ! tmux -L "${TMESH_SOCKET}" has-session -t "${TMESH_DEFAULT_SESSION}" 2>/dev/null; then
tmux \
TMESH_CONFIG="${TMESH_CONFIG}" \
tmux \
-f <(printf "%s\n" "${tmux_config[@]}") \
-L "${TMESH_SOCKET}" \
new-session \
-s "${TMESH_DEFAULT_SESSION}" \
-d \
mosh "${SERVER}" -- sh -c "TMUX_REMOTE_INSTANCE_SOCKET=$TMUX_REMOTE_SOCKET ${SCRIPT_DIR}/tmesh-server"
-d
# \
# mosh "${SERVER}" -- sh -c "TMESH_SOCKET=$TMESH_SOCKET ${SCRIPT_DIR}/tmesh-server"
fi
}

Expand Down
6 changes: 4 additions & 2 deletions src/bin/tmesh-server
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
SCRIPT_DIR="$(dirname "$0")"
TMESH_SERVER_INSTANCE_SOCKET=${TMESH_SERVER_INSTANCE_SOCKET:-"workspace"}
TMUX_CONFIG=${TMUX_CONFIG:="${SCRIPT_DIR}/../etc/tmesh/tmesh-server.tmux.conf"}
# nvim -c "terminal" -c "startinsert" >/dev/null 2>&1
TMESH_CONFIG=${TMESH_CONFIG:="/etc/tmesh/config.json"}

COMMAND="$(yq -e '.local-tmesh-server.command' ./config.yaml 2>/dev/null || echo "${SHELL}")"

tmux \
-L "${TMESH_SERVER_INSTANCE_SOCKET}" \
Expand All @@ -15,4 +17,4 @@ tmux \
-f "${TMUX_CONFIG}" \
new-session \
-s terminals \
"${SHELL}"
"${COMMAND}" >/dev/null 2>&1

0 comments on commit 3146e5e

Please sign in to comment.