Skip to content

refactor: consolidate small duplicated helpers across the codebase#44

Merged
hiroppy merged 1 commit into
mainfrom
refactor/small-helper-consolidation
Apr 22, 2026
Merged

refactor: consolidate small duplicated helpers across the codebase#44
hiroppy merged 1 commit into
mainfrom
refactor/small-helper-consolidation

Conversation

@hiroppy

@hiroppy hiroppy commented Apr 22, 2026

Copy link
Copy Markdown
Owner

Summary

Four independent cleanups from the earlier code-reuse review, bundled because each is a single-concept sweep:

  1. `tmux/commands.rs` + `tmux/panes.rs` — route four `run_tmux(["display-message", ...])` direct callers through the existing `display_message()` helper. `pane_session_name`, `get_pane_path`, `get_sidebar_pane_info`, and `select_pane` now share the same trim-and-fallback contract.
  2. `src/time.rs` (new) — centralise `now_epoch_secs()` / `now_epoch_millis()` for hook dispatch, TUI refresh, and the desktop-notification dedup store. `cli/hook/context/meta.rs`, `desktop_notification.rs`, and `state/refresh.rs` each reinvented the same `SystemTime::now().duration_since(UNIX_EPOCH)` fallback.
  3. `state/notices.rs` — replace the inline rect hit-test in `notices_copy_target_at` with the existing `state::layout::point_in_rect` helper.
  4. `ui/bottom/git.rs` — extract `diff_stat_spans(ins, del, theme)` to replace structurally identical `+ins/-del` span builders in `files.rs` and `header.rs`.

Base

Targets `refactor/unify-notify-lifecycle` (#43) as part of the refactor chain:
`split-large-modules` → `tmux-option-constants` (#42) → `unify-notify-lifecycle` (#43) → this PR.

Test plan

  • `cargo build`
  • `cargo test` — 1146 tests pass
  • `cargo clippy --all-targets -- -D warnings` — clean
  • `cargo fmt --check` — clean

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Consolidated time utility functions into a centralized module for improved code maintainability.
    • Refactored git diff stat rendering to reduce code duplication and improve consistency.
    • Simplified tmux command handling and internal helper utilities.

@coderabbitai

coderabbitai Bot commented Apr 22, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2bce78ad-90cc-4995-ae2f-b84e1bfdf938

📥 Commits

Reviewing files that changed from the base of the PR and between 507f24c and 4c3e2cd.

📒 Files selected for processing (15)
  • src/cli/hook/activity.rs
  • src/cli/hook/context.rs
  • src/cli/hook/context/meta.rs
  • src/cli/hook/handlers/run.rs
  • src/cli/hook/notifications.rs
  • src/desktop_notification.rs
  • src/lib.rs
  • src/state/notices.rs
  • src/state/refresh.rs
  • src/time.rs
  • src/tmux/commands.rs
  • src/tmux/panes.rs
  • src/ui/bottom/git.rs
  • src/ui/bottom/git/files.rs
  • src/ui/bottom/git/header.rs

📝 Walkthrough

Walkthrough

This pull request consolidates time-related utility functions into a new crate-level time module, removes duplicate implementations from other modules, and updates all import paths. Additionally, it refactors duplicated diff-stat formatting logic into a shared helper function and simplifies tmux command execution patterns.

Changes

Cohort / File(s) Summary
Time Module Centralization
src/time.rs, src/lib.rs
Created new public time module with now_epoch_secs() and now_epoch_millis() functions; both compute Unix epoch time with 0 fallback on clock skew. Includes unit tests verifying alignment between the two functions.
Time Function Re-export Removal
src/cli/hook/context.rs, src/cli/hook/context/meta.rs
Removed now_epoch_secs and now_epoch_millis re-exports from pub(super) use meta in context.rs; deleted the underlying function implementations and test from meta.rs.
Time Import Updates
src/cli/hook/activity.rs, src/cli/hook/handlers/run.rs, src/cli/hook/notifications.rs, src/state/refresh.rs, src/desktop_notification.rs
Updated five files to import time functions from crate::time::* instead of local/super modules. Removed local now_epoch_secs() implementation from desktop_notification.rs and inlined SystemTime logic in refresh.rs.
Bounds-Check Refactoring
src/state/notices.rs
Replaced inline row/col boundary checks for [copy] label hit-testing with call to super::layout::point_in_rect(row, col, t.area).
Diff-Stat Rendering Extraction
src/ui/bottom/git.rs, src/ui/bottom/git/files.rs, src/ui/bottom/git/header.rs
Extracted duplicated diff-stat formatting logic (+{ins}, /, -{del} with theme colors and width calculation) into new diff_stat_spans() helper. Updated files.rs and header.rs to call the new helper instead of inline construction.
Tmux Command Simplification
src/tmux/commands.rs, src/tmux/panes.rs
Refactored to use display_message() helper instead of manually building run_tmux argument arrays. Replaced Option-based control flow with empty-string fallbacks from unwrap_or_default() in command execution patterns.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 Hoppy refactoring day!
Time scattered, now gathered in one place,
Git spans styled, tmux simplified with grace—
Duplication hops away,
Order blooms in the code array! 🌸

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/small-helper-consolidation

Comment @coderabbitai help to get the list of available commands and usage tips.

@hiroppy hiroppy force-pushed the refactor/unify-notify-lifecycle branch from 8f6e959 to cf3455f Compare April 22, 2026 10:10
@hiroppy hiroppy force-pushed the refactor/small-helper-consolidation branch from f749821 to c623866 Compare April 22, 2026 10:10
@hiroppy hiroppy changed the base branch from refactor/unify-notify-lifecycle to main April 22, 2026 10:10
Four independent cleanups surfaced by the earlier code-reuse review
pass, bundled because each is a single-concept sweep:

1. tmux/commands.rs + tmux/panes.rs — route four `run_tmux(["display-message", "-t", …, "-p", …])` direct callers through the existing `display_message()` helper. `pane_session_name`, `get_pane_path`, `get_sidebar_pane_info`, and `select_pane` now share the same trim-and-fallback contract.
2. src/time.rs (new) — centralise `now_epoch_secs()` / `now_epoch_millis()` for hook dispatch, TUI refresh, and the desktop-notification dedup store. `cli/hook/context/meta.rs`, `desktop_notification.rs`, and `state/refresh.rs` each reinvented the same `SystemTime::now().duration_since(UNIX_EPOCH)…` fallback; now they call the shared helpers.
3. state/notices.rs — replace the inline rect hit-test in `notices_copy_target_at` with the existing `state::layout::point_in_rect` helper so both popup click paths share one predicate.
4. ui/bottom/git.rs — extract `diff_stat_spans(ins, del, theme)` to replace structurally identical `+ins/-del` span builders in `files.rs` and `header.rs`.

No behavior change. All 1146 tests pass; clippy / fmt clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hiroppy hiroppy force-pushed the refactor/small-helper-consolidation branch from c623866 to 4c3e2cd Compare April 22, 2026 10:12
@hiroppy hiroppy merged commit 2d8c6f3 into main Apr 22, 2026
@hiroppy hiroppy deleted the refactor/small-helper-consolidation branch April 22, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant