forked from cinco/tmuxido
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b05c188477 | ||
|
|
75d66cd47c | ||
|
|
973042ce7d | ||
|
|
0f27bedc94 | ||
|
|
da6311bc53 | ||
|
|
2b1773375a | ||
|
|
36aaa65945 | ||
|
|
10a38a1f85 | ||
|
|
42bdc1d409 | ||
|
|
a592c99375 |
+52
@@ -76,6 +76,16 @@ steps:
|
|||||||
commands:
|
commands:
|
||||||
- apk add --no-cache curl jq
|
- apk add --no-cache curl jq
|
||||||
- |
|
- |
|
||||||
|
# Delete existing release for this tag if present (handles retag scenarios)
|
||||||
|
EXISTING_ID=$(curl -fsSL \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases/tags/$DRONE_TAG" \
|
||||||
|
| jq -r '.id // empty')
|
||||||
|
if [ -n "$EXISTING_ID" ]; then
|
||||||
|
curl -fsSL -X DELETE \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"https://git.cincoeuzebio.com/api/v1/repos/cinco/Tmuxido/releases/$EXISTING_ID"
|
||||||
|
fi
|
||||||
# Read DRONE_TAG via ENVIRON inside awk to avoid Drone's ${VAR} substitution
|
# Read DRONE_TAG via ENVIRON inside awk to avoid Drone's ${VAR} substitution
|
||||||
# which would replace ${TAG} with an empty string before the shell runs.
|
# which would replace ${TAG} with an empty string before the shell runs.
|
||||||
BODY=$(awk '
|
BODY=$(awk '
|
||||||
@@ -99,3 +109,45 @@ steps:
|
|||||||
depends_on:
|
depends_on:
|
||||||
- build-x86_64
|
- build-x86_64
|
||||||
- build-aarch64
|
- build-aarch64
|
||||||
|
|
||||||
|
- name: publish-github
|
||||||
|
image: alpine
|
||||||
|
environment:
|
||||||
|
GITHUB_TOKEN:
|
||||||
|
from_secret: GITHUB_TOKEN
|
||||||
|
GITHUB_REPO: cinco/tmuxido
|
||||||
|
commands:
|
||||||
|
- apk add --no-cache curl jq
|
||||||
|
- |
|
||||||
|
EXISTING=$(curl -fsSL \
|
||||||
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
|
-H "Accept: application/vnd.github.v3+json" \
|
||||||
|
"https://api.github.com/repos/$GITHUB_REPO/releases/tags/$DRONE_TAG" | jq -r '.id // empty')
|
||||||
|
if [ -n "$EXISTING" ]; then
|
||||||
|
curl -s -X DELETE \
|
||||||
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
|
"https://api.github.com/repos/$GITHUB_REPO/releases/$EXISTING"
|
||||||
|
fi
|
||||||
|
BODY=$(awk '
|
||||||
|
BEGIN { tag = ENVIRON["DRONE_TAG"] }
|
||||||
|
/^## \[/ { in_section = (index($0, "[" tag "]") > 0); next }
|
||||||
|
in_section && /^## \[/ { exit }
|
||||||
|
in_section { print }
|
||||||
|
' CHANGELOG.md)
|
||||||
|
RELEASE_ID=$(curl -fsSL -X POST \
|
||||||
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
|
-H "Accept: application/vnd.github.v3+json" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
"https://api.github.com/repos/$GITHUB_REPO/releases" \
|
||||||
|
-d "{\"tag_name\":\"$DRONE_TAG\",\"name\":\"$DRONE_TAG\",\"body\":$(echo "$BODY" | jq -Rs .)}" \
|
||||||
|
| jq -r '.id')
|
||||||
|
for FILE in tmuxido-x86_64-linux tmuxido-aarch64-linux; do
|
||||||
|
curl -fsSL -X POST \
|
||||||
|
-H "Authorization: token $GITHUB_TOKEN" \
|
||||||
|
-H "Content-Type: application/octet-stream" \
|
||||||
|
"https://uploads.github.com/repos/$GITHUB_REPO/releases/$RELEASE_ID/assets?name=$FILE" \
|
||||||
|
--data-binary @"$FILE"
|
||||||
|
done
|
||||||
|
depends_on:
|
||||||
|
- build-x86_64
|
||||||
|
- build-aarch64
|
||||||
|
|||||||
@@ -4,6 +4,39 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
|
|
||||||
|
## [0.7.0] - 2026-03-01
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- `install.sh` now downloads from GitHub Releases
|
||||||
|
- Self-update now queries the GitHub Releases API for new versions
|
||||||
|
- Releases are published to both Gitea and GitHub
|
||||||
|
|
||||||
|
## [0.6.0] - 2026-03-01
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Periodic update check: on startup, if `update_check_interval_hours` have elapsed since
|
||||||
|
the last check, tmuxido fetches the latest release tag from the Gitea API and prints a
|
||||||
|
notice when a newer version is available (silent on network failure or no update found)
|
||||||
|
- New `update_check` module (`src/update_check.rs`) with injected fetcher for testability
|
||||||
|
- `update_check_interval_hours` config field (default 24, set to 0 to disable)
|
||||||
|
- Cache file `~/.cache/tmuxido/update_check.json` tracks last-checked timestamp and
|
||||||
|
latest known version across runs
|
||||||
|
|
||||||
|
## [0.5.2] - 2026-03-01
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Test for `detect_arch` asserting asset name follows `tmuxido-{arch}-linux` format
|
||||||
|
|
||||||
|
## [0.5.1] - 2026-03-01
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Tmux window creation now targets windows by name instead of numeric index, eliminating
|
||||||
|
"index in use" and "can't find window" errors when `base-index` is not 0
|
||||||
|
- Self-update asset name corrected from `x86_64-linux` to `tmuxido-x86_64-linux` to match
|
||||||
|
what CI actually uploads, fixing 404 on `--update`
|
||||||
|
- CI release pipeline now deletes any existing release for the tag before recreating,
|
||||||
|
preventing 409 Conflict errors on retagged releases
|
||||||
|
|
||||||
## [0.5.0] - 2026-03-01
|
## [0.5.0] - 2026-03-01
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
Generated
+1
-1
@@ -864,7 +864,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tmuxido"
|
name = "tmuxido"
|
||||||
version = "0.5.0"
|
version = "0.7.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tmuxido"
|
name = "tmuxido"
|
||||||
version = "0.5.0"
|
version = "0.7.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
+7
-4
@@ -1,8 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
REPO="cinco/Tmuxido"
|
REPO="cinco/tmuxido"
|
||||||
BASE_URL="https://git.cincoeuzebio.com"
|
BASE_URL="https://github.com"
|
||||||
|
API_URL="https://api.github.com"
|
||||||
INSTALL_DIR="$HOME/.local/bin"
|
INSTALL_DIR="$HOME/.local/bin"
|
||||||
|
|
||||||
arch=$(uname -m)
|
arch=$(uname -m)
|
||||||
@@ -12,8 +13,10 @@ case "$arch" in
|
|||||||
*) echo "Unsupported architecture: $arch" >&2; exit 1 ;;
|
*) echo "Unsupported architecture: $arch" >&2; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
tag=$(curl -fsSL "$BASE_URL/api/v1/repos/$REPO/releases?limit=1&page=1" \
|
tag=$(curl -fsSL \
|
||||||
| grep -o '"tag_name":"[^"]*"' | head -1 | cut -d'"' -f4)
|
-H "Accept: application/vnd.github.v3+json" \
|
||||||
|
"$API_URL/repos/$REPO/releases/latest" \
|
||||||
|
| grep -o '"tag_name":"[^"]*"' | cut -d'"' -f4)
|
||||||
|
|
||||||
[ -z "$tag" ] && { echo "Could not fetch latest release" >&2; exit 1; }
|
[ -z "$tag" ] && { echo "Could not fetch latest release" >&2; exit 1; }
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ pub struct Config {
|
|||||||
pub cache_enabled: bool,
|
pub cache_enabled: bool,
|
||||||
#[serde(default = "default_cache_ttl_hours")]
|
#[serde(default = "default_cache_ttl_hours")]
|
||||||
pub cache_ttl_hours: u64,
|
pub cache_ttl_hours: u64,
|
||||||
|
#[serde(default = "default_update_check_interval_hours")]
|
||||||
|
pub update_check_interval_hours: u64,
|
||||||
#[serde(default = "default_session_config")]
|
#[serde(default = "default_session_config")]
|
||||||
pub default_session: SessionConfig,
|
pub default_session: SessionConfig,
|
||||||
}
|
}
|
||||||
@@ -31,6 +33,10 @@ fn default_cache_ttl_hours() -> u64 {
|
|||||||
24
|
24
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_update_check_interval_hours() -> u64 {
|
||||||
|
24
|
||||||
|
}
|
||||||
|
|
||||||
fn default_session_config() -> SessionConfig {
|
fn default_session_config() -> SessionConfig {
|
||||||
use crate::session::Window;
|
use crate::session::Window;
|
||||||
|
|
||||||
@@ -109,6 +115,7 @@ impl Config {
|
|||||||
max_depth,
|
max_depth,
|
||||||
cache_enabled,
|
cache_enabled,
|
||||||
cache_ttl_hours,
|
cache_ttl_hours,
|
||||||
|
update_check_interval_hours: default_update_check_interval_hours(),
|
||||||
default_session: SessionConfig { windows },
|
default_session: SessionConfig { windows },
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -229,6 +236,7 @@ impl Config {
|
|||||||
max_depth: 5,
|
max_depth: 5,
|
||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
cache_ttl_hours: 24,
|
cache_ttl_hours: 24,
|
||||||
|
update_check_interval_hours: default_update_check_interval_hours(),
|
||||||
default_session: default_session_config(),
|
default_session: default_session_config(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,6 +253,7 @@ mod tests {
|
|||||||
assert_eq!(config.max_depth, 5);
|
assert_eq!(config.max_depth, 5);
|
||||||
assert!(config.cache_enabled);
|
assert!(config.cache_enabled);
|
||||||
assert_eq!(config.cache_ttl_hours, 24);
|
assert_eq!(config.cache_ttl_hours, 24);
|
||||||
|
assert_eq!(config.update_check_interval_hours, 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ pub mod deps;
|
|||||||
pub mod self_update;
|
pub mod self_update;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
pub mod update_check;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use cache::ProjectCache;
|
use cache::ProjectCache;
|
||||||
@@ -192,6 +193,7 @@ mod tests {
|
|||||||
max_depth: 3,
|
max_depth: 3,
|
||||||
cache_enabled,
|
cache_enabled,
|
||||||
cache_ttl_hours: 24,
|
cache_ttl_hours: 24,
|
||||||
|
update_check_interval_hours: 24,
|
||||||
default_session: session::SessionConfig { windows: vec![] },
|
default_session: session::SessionConfig { windows: vec![] },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use std::process::{Command, Stdio};
|
|||||||
use tmuxido::config::Config;
|
use tmuxido::config::Config;
|
||||||
use tmuxido::deps::ensure_dependencies;
|
use tmuxido::deps::ensure_dependencies;
|
||||||
use tmuxido::self_update;
|
use tmuxido::self_update;
|
||||||
|
use tmuxido::update_check;
|
||||||
use tmuxido::{get_projects, launch_tmux_session, show_cache_status};
|
use tmuxido::{get_projects, launch_tmux_session, show_cache_status};
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -48,6 +49,9 @@ fn main() -> Result<()> {
|
|||||||
// Load config
|
// Load config
|
||||||
let config = Config::load()?;
|
let config = Config::load()?;
|
||||||
|
|
||||||
|
// Periodic update check (silent on failure or no update)
|
||||||
|
update_check::check_and_notify(&config);
|
||||||
|
|
||||||
// Handle cache status command
|
// Handle cache status command
|
||||||
if args.cache_status {
|
if args.cache_status {
|
||||||
show_cache_status(&config)?;
|
show_cache_status(&config)?;
|
||||||
|
|||||||
+56
-20
@@ -3,8 +3,9 @@ use std::os::unix::fs::PermissionsExt;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
const REPO: &str = "cinco/Tmuxido";
|
const REPO: &str = "cinco/tmuxido";
|
||||||
const BASE_URL: &str = "https://git.cincoeuzebio.com";
|
const BASE_URL: &str = "https://github.com";
|
||||||
|
const API_BASE: &str = "https://api.github.com";
|
||||||
|
|
||||||
/// Check if running from cargo (development mode)
|
/// Check if running from cargo (development mode)
|
||||||
fn is_dev_build() -> bool {
|
fn is_dev_build() -> bool {
|
||||||
@@ -20,18 +21,33 @@ pub fn current_version() -> &'static str {
|
|||||||
fn detect_arch() -> Result<&'static str> {
|
fn detect_arch() -> Result<&'static str> {
|
||||||
let arch = std::env::consts::ARCH;
|
let arch = std::env::consts::ARCH;
|
||||||
match arch {
|
match arch {
|
||||||
"x86_64" => Ok("x86_64-linux"),
|
"x86_64" => Ok("tmuxido-x86_64-linux"),
|
||||||
"aarch64" => Ok("aarch64-linux"),
|
"aarch64" => Ok("tmuxido-aarch64-linux"),
|
||||||
_ => Err(anyhow::anyhow!("Unsupported architecture: {}", arch)),
|
_ => Err(anyhow::anyhow!("Unsupported architecture: {}", arch)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch latest release tag from Gitea API
|
/// Parse tag_name from a GitHub releases/latest JSON response
|
||||||
fn fetch_latest_tag() -> Result<String> {
|
fn parse_latest_tag(response: &str) -> Result<String> {
|
||||||
let url = format!("{}/api/v1/repos/{}/releases?limit=1&page=1", BASE_URL, REPO);
|
let tag: serde_json::Value =
|
||||||
|
serde_json::from_str(response).context("Failed to parse release API response")?;
|
||||||
|
tag.get("tag_name")
|
||||||
|
.and_then(|t| t.as_str())
|
||||||
|
.map(|t| t.to_string())
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Could not extract tag_name from release"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch latest release tag from GitHub API
|
||||||
|
pub(crate) fn fetch_latest_tag() -> Result<String> {
|
||||||
|
let url = format!("{}/repos/{}/releases/latest", API_BASE, REPO);
|
||||||
|
|
||||||
let output = Command::new("curl")
|
let output = Command::new("curl")
|
||||||
.args(["-fsSL", &url])
|
.args([
|
||||||
|
"-fsSL",
|
||||||
|
"-H",
|
||||||
|
"Accept: application/vnd.github.v3+json",
|
||||||
|
&url,
|
||||||
|
])
|
||||||
.output()
|
.output()
|
||||||
.context("Failed to execute curl. Make sure curl is installed.")?;
|
.context("Failed to execute curl. Make sure curl is installed.")?;
|
||||||
|
|
||||||
@@ -42,17 +58,7 @@ fn fetch_latest_tag() -> Result<String> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = String::from_utf8_lossy(&output.stdout);
|
parse_latest_tag(&String::from_utf8_lossy(&output.stdout))
|
||||||
|
|
||||||
// Parse JSON response to extract tag_name
|
|
||||||
let tag: serde_json::Value =
|
|
||||||
serde_json::from_str(&response).context("Failed to parse release API response")?;
|
|
||||||
|
|
||||||
tag.get(0)
|
|
||||||
.and_then(|r| r.get("tag_name"))
|
|
||||||
.and_then(|t| t.as_str())
|
|
||||||
.map(|t| t.to_string())
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Could not extract tag_name from release"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get path to current executable
|
/// Get path to current executable
|
||||||
@@ -166,7 +172,7 @@ pub fn self_update() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compare two semver versions
|
/// Compare two semver versions
|
||||||
fn version_compare(a: &str, b: &str) -> std::cmp::Ordering {
|
pub(crate) fn version_compare(a: &str, b: &str) -> std::cmp::Ordering {
|
||||||
let parse = |s: &str| {
|
let parse = |s: &str| {
|
||||||
s.split('.')
|
s.split('.')
|
||||||
.filter_map(|n| n.parse::<u32>().ok())
|
.filter_map(|n| n.parse::<u32>().ok())
|
||||||
@@ -198,6 +204,36 @@ mod tests {
|
|||||||
assert!(version.contains('.'));
|
assert!(version.contains('.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_prefix_arch_asset_with_tmuxido() {
|
||||||
|
let arch = detect_arch().expect("should detect supported arch");
|
||||||
|
assert!(
|
||||||
|
arch.starts_with("tmuxido-"),
|
||||||
|
"asset name must start with 'tmuxido-', got: {arch}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
arch.ends_with("-linux"),
|
||||||
|
"asset name must end with '-linux', got: {arch}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_parse_tag_from_github_latest_release_response() {
|
||||||
|
let json = r#"{"tag_name":"0.7.0","name":"0.7.0","body":"release notes"}"#;
|
||||||
|
assert_eq!(parse_latest_tag(json).unwrap(), "0.7.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_return_error_when_tag_name_missing() {
|
||||||
|
let json = r#"{"name":"0.7.0","body":"no tag_name field"}"#;
|
||||||
|
assert!(parse_latest_tag(json).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_return_error_when_response_is_invalid_json() {
|
||||||
|
assert!(parse_latest_tag("not valid json").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_compare_versions_correctly() {
|
fn should_compare_versions_correctly() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|||||||
+24
-56
@@ -41,7 +41,6 @@ impl SessionConfig {
|
|||||||
pub struct TmuxSession {
|
pub struct TmuxSession {
|
||||||
pub(crate) session_name: String,
|
pub(crate) session_name: String,
|
||||||
project_path: String,
|
project_path: String,
|
||||||
base_index: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TmuxSession {
|
impl TmuxSession {
|
||||||
@@ -53,34 +52,12 @@ impl TmuxSession {
|
|||||||
.replace('.', "_")
|
.replace('.', "_")
|
||||||
.replace(' ', "-");
|
.replace(' ', "-");
|
||||||
|
|
||||||
let base_index = Self::get_base_index();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
session_name,
|
session_name,
|
||||||
project_path: project_path.display().to_string(),
|
project_path: project_path.display().to_string(),
|
||||||
base_index,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_base_index() -> usize {
|
|
||||||
// Try to get base-index from tmux
|
|
||||||
let output = Command::new("tmux")
|
|
||||||
.args(["show-options", "-gv", "base-index"])
|
|
||||||
.output();
|
|
||||||
|
|
||||||
if let Ok(output) = output
|
|
||||||
&& output.status.success()
|
|
||||||
{
|
|
||||||
let index_str = String::from_utf8_lossy(&output.stdout);
|
|
||||||
if let Ok(index) = index_str.trim().parse::<usize>() {
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default to 0 if we can't determine
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create(&self, config: &SessionConfig) -> Result<()> {
|
pub fn create(&self, config: &SessionConfig) -> Result<()> {
|
||||||
// Check if we're already inside a tmux session
|
// Check if we're already inside a tmux session
|
||||||
let inside_tmux = std::env::var("TMUX").is_ok();
|
let inside_tmux = std::env::var("TMUX").is_ok();
|
||||||
@@ -167,25 +144,23 @@ impl TmuxSession {
|
|||||||
.status()
|
.status()
|
||||||
.context("Failed to create tmux session")?;
|
.context("Failed to create tmux session")?;
|
||||||
|
|
||||||
// Create panes for first window if specified
|
let first_target = format!("{}:{}", self.session_name, first_window.name);
|
||||||
|
|
||||||
if !first_window.panes.is_empty() {
|
if !first_window.panes.is_empty() {
|
||||||
self.create_panes(self.base_index, &first_window.panes)?;
|
self.create_panes(&first_target, &first_window.panes)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply layout for first window if specified
|
|
||||||
if let Some(layout) = &first_window.layout {
|
if let Some(layout) = &first_window.layout {
|
||||||
self.apply_layout(self.base_index, layout)?;
|
self.apply_layout(&first_target, layout)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create additional windows
|
// Create additional windows, targeting by session name so tmux auto-assigns the index
|
||||||
for (index, window) in config.windows.iter().skip(1).enumerate() {
|
for window in config.windows.iter().skip(1) {
|
||||||
let window_index = self.base_index + index + 1;
|
|
||||||
|
|
||||||
Command::new("tmux")
|
Command::new("tmux")
|
||||||
.args([
|
.args([
|
||||||
"new-window",
|
"new-window",
|
||||||
"-t",
|
"-t",
|
||||||
&format!("{}:{}", self.session_name, window_index),
|
&self.session_name,
|
||||||
"-n",
|
"-n",
|
||||||
&window.name,
|
&window.name,
|
||||||
"-c",
|
"-c",
|
||||||
@@ -194,46 +169,44 @@ impl TmuxSession {
|
|||||||
.status()
|
.status()
|
||||||
.with_context(|| format!("Failed to create window: {}", window.name))?;
|
.with_context(|| format!("Failed to create window: {}", window.name))?;
|
||||||
|
|
||||||
// Create panes if specified
|
let target = format!("{}:{}", self.session_name, window.name);
|
||||||
|
|
||||||
if !window.panes.is_empty() {
|
if !window.panes.is_empty() {
|
||||||
self.create_panes(window_index, &window.panes)?;
|
self.create_panes(&target, &window.panes)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply layout if specified
|
|
||||||
if let Some(layout) = &window.layout {
|
if let Some(layout) = &window.layout {
|
||||||
self.apply_layout(window_index, layout)?;
|
self.apply_layout(&target, layout)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select the first window
|
// Select the first window by name
|
||||||
Command::new("tmux")
|
Command::new("tmux")
|
||||||
.args([
|
.args(["select-window", "-t", &first_target])
|
||||||
"select-window",
|
|
||||||
"-t",
|
|
||||||
&format!("{}:{}", self.session_name, self.base_index),
|
|
||||||
])
|
|
||||||
.status()
|
.status()
|
||||||
.context("Failed to select first window")?;
|
.context("Failed to select first window")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_panes(&self, window_index: usize, panes: &[String]) -> Result<()> {
|
fn create_panes(&self, window_target: &str, panes: &[String]) -> Result<()> {
|
||||||
for (pane_index, command) in panes.iter().enumerate() {
|
for (pane_index, command) in panes.iter().enumerate() {
|
||||||
let target = format!("{}:{}", self.session_name, window_index);
|
|
||||||
|
|
||||||
// First pane already exists (created with the window), skip split
|
// First pane already exists (created with the window), skip split
|
||||||
if pane_index > 0 {
|
if pane_index > 0 {
|
||||||
// Create new pane by splitting
|
|
||||||
Command::new("tmux")
|
Command::new("tmux")
|
||||||
.args(["split-window", "-t", &target, "-c", &self.project_path])
|
.args([
|
||||||
|
"split-window",
|
||||||
|
"-t",
|
||||||
|
window_target,
|
||||||
|
"-c",
|
||||||
|
&self.project_path,
|
||||||
|
])
|
||||||
.status()
|
.status()
|
||||||
.context("Failed to split pane")?;
|
.context("Failed to split pane")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the command to the pane if it's not empty
|
|
||||||
if !command.is_empty() {
|
if !command.is_empty() {
|
||||||
let pane_target = format!("{}:{}.{}", self.session_name, window_index, pane_index);
|
let pane_target = format!("{}.{}", window_target, pane_index);
|
||||||
Command::new("tmux")
|
Command::new("tmux")
|
||||||
.args(["send-keys", "-t", &pane_target, command, "Enter"])
|
.args(["send-keys", "-t", &pane_target, command, "Enter"])
|
||||||
.status()
|
.status()
|
||||||
@@ -244,14 +217,9 @@ impl TmuxSession {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_layout(&self, window_index: usize, layout: &str) -> Result<()> {
|
fn apply_layout(&self, window_target: &str, layout: &str) -> Result<()> {
|
||||||
Command::new("tmux")
|
Command::new("tmux")
|
||||||
.args([
|
.args(["select-layout", "-t", window_target, layout])
|
||||||
"select-layout",
|
|
||||||
"-t",
|
|
||||||
&format!("{}:{}", self.session_name, window_index),
|
|
||||||
layout,
|
|
||||||
])
|
|
||||||
.status()
|
.status()
|
||||||
.with_context(|| format!("Failed to apply layout: {}", layout))?;
|
.with_context(|| format!("Failed to apply layout: {}", layout))?;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::self_update;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
|
struct UpdateCheckCache {
|
||||||
|
last_checked: u64,
|
||||||
|
latest_version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_and_notify(config: &Config) {
|
||||||
|
let cache = load_cache();
|
||||||
|
check_and_notify_internal(
|
||||||
|
config.update_check_interval_hours,
|
||||||
|
cache,
|
||||||
|
&|| self_update::fetch_latest_tag(),
|
||||||
|
&save_cache,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_and_notify_internal(
|
||||||
|
interval_hours: u64,
|
||||||
|
mut cache: UpdateCheckCache,
|
||||||
|
fetcher: &dyn Fn() -> Result<String>,
|
||||||
|
saver: &dyn Fn(&UpdateCheckCache),
|
||||||
|
) -> bool {
|
||||||
|
if interval_hours == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let elapsed = elapsed_hours(cache.last_checked);
|
||||||
|
|
||||||
|
if elapsed >= interval_hours
|
||||||
|
&& let Ok(latest) = fetcher()
|
||||||
|
{
|
||||||
|
let latest_clean = latest.trim_start_matches('v').to_string();
|
||||||
|
let now = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs();
|
||||||
|
cache.last_checked = now;
|
||||||
|
cache.latest_version = latest_clean;
|
||||||
|
saver(&cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
let current = self_update::current_version();
|
||||||
|
let latest_clean = cache.latest_version.trim_start_matches('v');
|
||||||
|
if !latest_clean.is_empty()
|
||||||
|
&& self_update::version_compare(latest_clean, current) == std::cmp::Ordering::Greater
|
||||||
|
{
|
||||||
|
print_update_notice(current, latest_clean);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_update_notice(current: &str, latest: &str) {
|
||||||
|
let msg1 = format!(" Update available: {} \u{2192} {} ", current, latest);
|
||||||
|
let msg2 = " Run tmuxido --update to install. ";
|
||||||
|
let w1 = msg1.chars().count();
|
||||||
|
let w2 = msg2.chars().count();
|
||||||
|
let width = w1.max(w2);
|
||||||
|
let border = "\u{2500}".repeat(width);
|
||||||
|
println!("\u{250c}{}\u{2510}", border);
|
||||||
|
println!("\u{2502}{}\u{2502}", pad_to_chars(&msg1, width));
|
||||||
|
println!("\u{2502}{}\u{2502}", pad_to_chars(msg2, width));
|
||||||
|
println!("\u{2514}{}\u{2518}", border);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pad_to_chars(s: &str, width: usize) -> String {
|
||||||
|
let char_count = s.chars().count();
|
||||||
|
if char_count >= width {
|
||||||
|
s.to_string()
|
||||||
|
} else {
|
||||||
|
format!("{}{}", s, " ".repeat(width - char_count))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cache_path() -> Result<PathBuf> {
|
||||||
|
let cache_dir = dirs::cache_dir()
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Could not determine cache directory"))?
|
||||||
|
.join("tmuxido");
|
||||||
|
Ok(cache_dir.join("update_check.json"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_cache() -> UpdateCheckCache {
|
||||||
|
cache_path()
|
||||||
|
.ok()
|
||||||
|
.and_then(|p| std::fs::read_to_string(p).ok())
|
||||||
|
.and_then(|s| serde_json::from_str(&s).ok())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_cache(cache: &UpdateCheckCache) {
|
||||||
|
if let Ok(path) = cache_path() {
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
let _ = std::fs::create_dir_all(parent);
|
||||||
|
}
|
||||||
|
if let Ok(json) = serde_json::to_string(cache) {
|
||||||
|
let _ = std::fs::write(path, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn elapsed_hours(ts: u64) -> u64 {
|
||||||
|
let now = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs();
|
||||||
|
now.saturating_sub(ts) / 3600
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
|
fn make_cache(last_checked: u64, latest_version: &str) -> UpdateCheckCache {
|
||||||
|
UpdateCheckCache {
|
||||||
|
last_checked,
|
||||||
|
latest_version: latest_version.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_ts() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_not_notify_when_interval_is_zero() {
|
||||||
|
let cache = make_cache(0, "99.0.0");
|
||||||
|
let fetcher_called = RefCell::new(false);
|
||||||
|
|
||||||
|
let result = check_and_notify_internal(
|
||||||
|
0,
|
||||||
|
cache,
|
||||||
|
&|| {
|
||||||
|
*fetcher_called.borrow_mut() = true;
|
||||||
|
Ok("99.0.0".to_string())
|
||||||
|
},
|
||||||
|
&|_| {},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(!result);
|
||||||
|
assert!(!fetcher_called.into_inner());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_not_check_when_interval_not_elapsed() {
|
||||||
|
let cache = make_cache(now_ts(), "");
|
||||||
|
let fetcher_called = RefCell::new(false);
|
||||||
|
|
||||||
|
check_and_notify_internal(
|
||||||
|
24,
|
||||||
|
cache,
|
||||||
|
&|| {
|
||||||
|
*fetcher_called.borrow_mut() = true;
|
||||||
|
Ok("99.0.0".to_string())
|
||||||
|
},
|
||||||
|
&|_| {},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(!fetcher_called.into_inner());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_check_when_interval_elapsed() {
|
||||||
|
let cache = make_cache(0, "");
|
||||||
|
let fetcher_called = RefCell::new(false);
|
||||||
|
|
||||||
|
check_and_notify_internal(
|
||||||
|
1,
|
||||||
|
cache,
|
||||||
|
&|| {
|
||||||
|
*fetcher_called.borrow_mut() = true;
|
||||||
|
Ok(self_update::current_version().to_string())
|
||||||
|
},
|
||||||
|
&|_| {},
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(fetcher_called.into_inner());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_not_notify_when_versions_equal() {
|
||||||
|
let current = self_update::current_version();
|
||||||
|
let cache = make_cache(now_ts(), current);
|
||||||
|
|
||||||
|
let result = check_and_notify_internal(24, cache, &|| unreachable!(), &|_| {});
|
||||||
|
|
||||||
|
assert!(!result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_detect_update_available() {
|
||||||
|
let cache = make_cache(now_ts(), "99.0.0");
|
||||||
|
|
||||||
|
let result = check_and_notify_internal(24, cache, &|| unreachable!(), &|_| {});
|
||||||
|
|
||||||
|
assert!(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_not_detect_update_when_current_is_newer() {
|
||||||
|
let cache = make_cache(now_ts(), "0.0.1");
|
||||||
|
|
||||||
|
let result = check_and_notify_internal(24, cache, &|| unreachable!(), &|_| {});
|
||||||
|
|
||||||
|
assert!(!result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ fn make_config(max_depth: usize) -> Config {
|
|||||||
max_depth,
|
max_depth,
|
||||||
cache_enabled: true,
|
cache_enabled: true,
|
||||||
cache_ttl_hours: 24,
|
cache_ttl_hours: 24,
|
||||||
|
update_check_interval_hours: 24,
|
||||||
default_session: SessionConfig { windows: vec![] },
|
default_session: SessionConfig { windows: vec![] },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user