;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013 Cyril Roelandt <tipecaml@gmail.com>
;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2016 Matthew Jordan <matthewjordandevops@yandex.com>
;;; Copyright © 2017 Vasile Dumitrascu <va511e@yahoo.com>
;;; Copyright © 2017 Stefan Reichör <stefan@xsteve.at>
;;; Copyright © 2019–2021 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2019 Oleg Pykhalov <go.wigust@gmail.com>
;;; Copyright © 2020, 2021 Brice Waegeneire <brice@waegenei.re>
;;; Copyright © 2020 Edouard Klein <edk@beaver-labs.com>
;;; Copyright © 2021 Matthew James Kraai <kraai@ftbfs.org>
;;; Copyright © 2024 Ashish SHUKLA <ashish.is@lostca.se>
;;; Copyright © 2024 Zheng Junjie <873216071@qq.com>
;;; Copyright © 2025 Alex Bosco <me@alexbos.co>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages tmux)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix gexp)
  #:use-module (guix packages)
  #:use-module (guix git-download)
  #:use-module (guix build-system cargo)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (guix build-system pyproject)
  #:use-module (guix build-system python)
  #:use-module (gnu packages)
  #:use-module (gnu packages autotools)
  #:use-module (gnu packages bash)
  #:use-module (gnu packages bison)
  #:use-module (gnu packages check)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages libevent)
  #:use-module (gnu packages ncurses)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python-build)
  #:use-module (gnu packages python-check)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages sphinx)
  #:use-module (gnu packages ssh)
  #:use-module (gnu packages tls)
  #:use-module (gnu packages version-control))

(define-public tmux
  (package
    (name "tmux")
    (version "3.6a")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/tmux/tmux")
                    (commit version)))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1fzp15rc5rzrq0nmj54qj5c0kadc9p56wjalr7p0z10qsr3v40sp"))))
    (build-system gnu-build-system)
    (inputs
     (list libevent ncurses))
    (native-inputs
     (list autoconf automake bison pkg-config))
    (home-page "https://github.com/tmux/tmux/wiki")
    (synopsis "Terminal multiplexer")
    (description
     "tmux is a terminal multiplexer: it enables a number of terminals (or
windows), each running a separate program, to be created, accessed, and
controlled from a single screen.  tmux may be detached from a screen and
continue running in the background, then later reattached.")
    (license license:isc)))

(define-public tmux-themepack
  (package
    (name "tmux-themepack")
    (version "1.1.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/jimeh/tmux-themepack")
                    (commit version)))
              (sha256
               (base32
                "00dmd16ngyag3n46rbnl9vy82ih6g0y02yfwkid32a1c8vdbvb3z"))
              (file-name (git-file-name name version))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f                    ; no test suite
       #:phases (modify-phases %standard-phases
                  (delete 'configure)
                  (delete 'build)
                  (replace 'install
                    (lambda* (#:key outputs #:allow-other-keys)
                      (let* ((out (string-append
                                   (assoc-ref outputs "out")
                                   "/share/" ,name)))
                        (copy-recursively "powerline" (string-append out "/powerline"))
                        (for-each (lambda (file) (copy-file file (string-append out "/" file)))
                                  '("basic.tmuxtheme"
                                    "default.tmuxtheme"
                                    "themepack.tmux"))))))))
    (home-page "https://github.com/jimeh/tmux-themepack")
    (synopsis "Collection of themes for Tmux")
    (description
     "This package provides several themes for Tmux, the terminal multiplexer.")
    (license license:wtfpl2)))

(define-public tmuxifier
  (package
    (name "tmuxifier")
    (version "0.13.0")
    (source (origin
             (method git-fetch)
             (uri (git-reference
                   (url "https://github.com/jimeh/tmuxifier")
                   (commit (string-append "v" version))))
             (file-name (git-file-name name version))
             (sha256
              (base32
               "1b6a1cw2mnml84k5vhbcp58kvp94xlnlpp4kwdhqw4jrzfgcjfzd"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f
       #:phases (modify-phases %standard-phases
                  (delete 'configure)
                  (delete 'build)
                  (replace 'install
                    (lambda* (#:key outputs #:allow-other-keys)
                      (let* ((out    (assoc-ref %outputs "out"))
                             (bindir (string-append out "/bin"))
                             (share  (string-append out "/share/" ,name)))
                        (install-file "bin/tmuxifier" bindir)
                        (substitute* (string-append bindir "/tmuxifier")
                          (("set -e")
                           (string-append "TMUXIFIER=" share "\nset -e")))
                        (for-each (lambda (init-script)
                                    (install-file init-script (string-append
                                                               share "/init")))
                                  '("init.sh" "init.tcsh" "init.fish"))
                        (for-each (lambda (dir)
                                    (copy-recursively dir (string-append
                                                           share "/" dir)))
                                  '("completion" "lib" "libexec"
                                    "templates"))))))))
    (home-page "https://github.com/jimeh/tmuxifier")
    (synopsis "Powerful session, window & pane management for Tmux")
    (description "Tmuxifier allows you to easily create, edit, and load
@code{layout} files, which are simple shell scripts where you use the tmux
command and helper commands provided by tmuxifier to manage Tmux sessions and
windows.")
    (license license:expat)))

(define-public python-libtmux
  (package
    (name "python-libtmux")
    (version "0.53.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/tmux-python/libtmux")
             (commit (string-append "v" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32 "19l3kglg2yxw4i1czr93071ymlyj0in3d6yhl9nwd5dm7a3bjs4l"))))
    (build-system pyproject-build-system)
    (arguments
     (list
      #:test-flags
      #~(list "-c" "/dev/null" ; Missing development dependency.
              "-k" "not test_capture_pane_start")  ; Permission denied.
      #:phases
      #~(modify-phases %standard-phases
          (add-before 'check 'configure-tests
            (lambda _
              ;; Fix <https://github.com/tmux-python/libtmux/issues/265>.
              (setenv "LANG" "en_US.utf8"))))))
    (propagated-inputs (list procps)) ;tests need top
    (native-inputs (list python-hatchling python-pytest tmux))
    (home-page "https://github.com/tmux-python/libtmux")
    (synopsis "Python API for tmux")
    (description
     "Libtmux is the tool behind @command{tmuxp}, a tmux workspace
manager in Python.  It creates object mappings to traverse, inspect and interact
with live tmux sessions.")
    (license license:expat)))

(define-public tmux-xpanes
  (package
    (name "tmux-xpanes")
    (version "4.2.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/greymd/tmux-xpanes")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "0k37zswn6s9iclm9yasil9jhy768qn9fv3m489irfwrx1vc8lp9w"))))
    (build-system trivial-build-system)
    (inputs
     (list bash))
    (arguments
     `(#:modules ((guix build utils))
       #:builder
       (begin
         (use-modules (guix build utils))
         (setenv "PATH" (string-append (assoc-ref %build-inputs "bash") "/bin"))
         (copy-recursively (assoc-ref %build-inputs "source") ".")
         (substitute* "bin/xpanes"
           (("/bin/bash") (which "bash")))
         (install-file "bin/xpanes" (string-append %output "/bin"))
         (install-file "man/xpanes.1" (string-append %output "/man/man1"))
         #t)))
    (home-page "https://github.com/greymd/tmux-xpanes")
    (synopsis "Tmux based terminal divider")
    (description "This package provides tmux-based terminal divider.

@code{xpanes} or @code{tmux-xpanes} (alias of @code{xpanes}) commands have
following features:

@itemize
@item Split tmux window into multiple panes.
@item Build command lines & execute them on the panes.
@item Runnable from outside of tmux session.
@item Runnable from inside of tmux session.
@item Record operation log.
@item Flexible layout arrangement for panes.
@item Display pane title on each pane.
@item Generate command lines from standard input (Pipe mode).
@end itemize")
    (license license:expat)))

(define-public tmux-plugin-resurrect
  (let ((commit "a2ddfb96b94bb64a7a2e3f5fa2a7c57dce8ad579")
        (revision "0"))
    (package
      (name "tmux-plugin-resurrect")
      (version (git-version "0" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/tmux-plugins/tmux-resurrect/")
                      (commit commit)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "1gc8z99na1d4scn2kq4alwyn43h3r7ykz9bkhcypjh8iri6dsl0c"))))
      (build-system trivial-build-system)
      (arguments
       `(#:modules ((guix build utils))
         #:builder (begin
                     (use-modules (guix build utils))
                     (let ((out (string-append %output
                                               "/share/tmux-plugins/resurrect/")))
                       (mkdir-p out)
                       (copy-recursively (assoc-ref %build-inputs "source") out)))))
      (synopsis "Restore tmux environment after system restart")
      (description
       "This plugin goes to great lengths to save and restore all the details
from your tmux environment.  Here's what's been taken care of:

@itemize
@item all sessions, windows, panes and their order
@item current working directory for each pane
@item exact pane layouts within windows (even when zoomed)
@item active and alternative session
@item active and alternative window for each session
@item windows with focus
@item active pane for each window
@item \"grouped sessions\" (useful feature when using tmux with multiple monitors)
@item programs running within a pane! More details in the restoring programs doc.
@end itemize

Optional:

@itemize
@item restoring vim and neovim sessions
@item restoring pane contents
@end itemize")
      (home-page "https://github.com/tmux-plugins/tmux-resurrect/")
      (license license:expat))))

(define-public tmux-plugin-continuum
  (let ((commit "3e4bc35da41f956c873aea716c97555bf1afce5d")
        (revision "0"))
    (package
      (name "tmux-plugin-continuum")
      (version (git-version "0" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/tmux-plugins/tmux-continuum/")
                      (commit commit)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "1py8qfs2f93hkxhk039m813bjgcs5k54si662gx05g3czqy06pb7"))))
      (build-system trivial-build-system)
      (arguments
       `(#:modules ((guix build utils))
         #:builder (begin
                     (use-modules (guix build utils))
                     (let ((out (string-append %output
                                 "/share/tmux-plugins/continuum/")))
                       (mkdir-p out)
                       (copy-recursively (assoc-ref %build-inputs "source")
                                         out)))))
      (synopsis "Continuous saving of tmux environment")
      (description
       "Features:

@itemize
@item continuous saving of tmux environment
@item automatic tmux start when computer/server is turned on
@item automatic restore when tmux is started
@end itemize

Together, these features enable uninterrupted tmux usage.  No matter the
computer or server restarts, if the machine is on, tmux will be there how you
left it off the last time it was used.")
      (home-page "https://github.com/tmux-plugins/tmux-continuum/")
      (license license:expat))))

(define-public tmux-plugin-mem-cpu-load
  (package
    (name "tmux-plugin-mem-cpu-load")
    (version "3.8.2")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/thewtex/tmux-mem-cpu-load")
                    (commit (string-append "v" version))))
              (sha256
               (base32
                "041hd6s6prk63r121yggxmzhbp40h6r04mrby0q51x43lfgvmvw3"))
              (file-name (git-file-name name version))))
    (build-system cmake-build-system)
    (synopsis "CPU, RAM, and load monitor for use with tmux")
    (description "This package provides a lightweight program for system
monitoring in the status line of tmux.

The memory monitor displays the used and available memory.

The CPU usage monitor outputs a percent CPU usage over all processors.  It
also displays a textual bar graph of the current percent usage.

The system load average is also displayed.")
    (home-page "https://github.com/thewtex/tmux-mem-cpu-load")
    (license license:asl2.0)))

(define-public tmux-plugin-sessionizer
  (package
    (name "tmux-plugin-sessionizer")
    (version "0.4.4")
    (source
     (origin
       (method url-fetch)
       (uri (crate-uri "tmux-sessionizer" version))
       (file-name (string-append name "-" version ".tar.gz"))
       (sha256
        (base32 "0fwdc8jyx9fab442c6zsl3yn8nh1s5h35g97cgqhyp3blxl6h9ix"))
       (snippet #~(begin
                    (use-modules (guix build utils))
                    (substitute* "Cargo.toml"
                      (("\"vendored-openssl\"")
                       ""))))))
    (build-system cargo-build-system)
    (native-inputs (list pkg-config))
    (inputs
     (cons* openssl
            libgit2-1.8
            libssh2
            (cargo-inputs 'tmux-plugin-sessionizer)))
    (arguments
     `(#:install-source? #f))
    (home-page "https://github.com/jrmoulton/tmux-sessionizer")
    (synopsis "Fuzzy find Git repositories and open them as Tmux sessions")
    (description
     "Tmux Sessionizer is a command-line tool to fuzzy find all the Git
repositories in a list of specified folders and open them as a new tmux session.
For @code{git worktrees}, this tool opens all checked out worktrees as new windows.")
    (license license:expat)))
