Advertisement
Guest User

Untitled

a guest
May 29th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #
  2. # tmux_funcs.sh
  3. #
  4. # This file provides some basic helper functions for
  5. # working with TMUX from a shell script.
  6. #
  7. # Copyright (c) 2015, Alexander Rudy
  8. # All rights reserved.
  9. #
  10. # Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are
  12. # met:
  13. #
  14. # 1. Redistributions of source code must retain the above copyright
  15. # notice, this list of conditions and the following disclaimer.
  16. #
  17. # 2. Redistributions in binary form must reproduce the above copyright
  18. # notice, this list of conditions and the following disclaimer in the
  19. # documentation and/or other materials provided with the distribution.
  20. #
  21. # 3. Neither the name of the copyright holder nor the names of its
  22. # contributors may be used to endorse or promote products derived from
  23. # this software without specific prior written permission.
  24. #
  25. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECTTHIS SOFTWARE
  30. # IS PRAL,THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  32. # BUT NOT LIMITFITS;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  33. # AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. # INCLUDING,LUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  35. # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  36. # DAMAGE.
  37.  
  38. # Check if tmux has any alive panes in a given window.
  39. tmux_alive_panes() {
  40. tmux list-panes -t "$1" | grep -v "dead" &> /dev/null
  41. }
  42.  
  43. # Check if tmux has any dead panes in a given window.
  44. tmux_dead_panes() {
  45. tmux list-panes -t "$1" | grep "dead" &> /dev/null
  46. }
  47.  
  48. # Check if tmux has *any* dead windows, filtered by first and second argument.
  49. tmux_dead_window() {
  50. tmux list-windows -t "$1" | grep "$2" | grep "dead" &> /dev/null
  51. }
  52.  
  53. # Get the number of panes in the first argument target.
  54. tmux_n_panes() {
  55. tmux list-panes -t $1 | wc -l | tr -d ' '
  56. }
  57.  
  58. # Check if tmux has a listed window.
  59. tmux_has_window() {
  60. tmux list-windows -t "$1" | grep "$2" &> /dev/null
  61. }
  62.  
  63. # Check if tmux has a listed session
  64. tmux_has_session() {
  65. tmux has -t "$1" &> /dev/null;
  66. }
  67.  
  68. # Create a new window, or respond it.
  69. # Call sequence:
  70. # tmux_new_or_respawn "session_name" "window_name" ["command"]
  71. tmux_new_or_respawn() {
  72. session="$1"
  73. window="$2"
  74. cmd="$3"
  75. if ! tmux_has_window $session $window; then
  76. if [ -z "$cmd" ]; then
  77. echo "$session:$window spwaning"
  78. tmux new-window -t $session -n $window
  79. else
  80. echo "$session:$window spawining with $cmd"
  81. tmux new-window -t $session -n $window "$cmd"
  82. fi
  83. elif tmux_dead_window "$session" "$window"; then
  84. echo "$session:$window respawning"
  85. tmux respawn-window -t "$session:$window" &> /dev/null
  86. fi
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement