~kirkland/byobu/trunk

« back to all changes in this revision

Viewing changes to usr/bin/byobu-layout.in

  • Committer: kirkland at ubuntu
  • Date: 2023-11-22 22:45:43 UTC
  • Revision ID: kirkland@ubuntu.com-20231122224543-v5igclwtj4xga8tm
Permanently moved Byobu source code repository to https://github.com/dustinkirkland/byobu

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh -e
2
 
#
3
 
#    layout: save and restore byobu layouts
4
 
#    Copyright (C) 2011-2014 Dustin Kirkland <kirkland@byobu.org>
5
 
#
6
 
#    Authors: Dustin Kirkland <kirkland@byobu.org>
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, version 3 of the License.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
PKG="byobu"
21
 
[ -r "$HOME/.byoburc" ] && . "$HOME/.byoburc"
22
 
[ -z "${BYOBU_PREFIX}" ] && export BYOBU_PREFIX="@prefix@" || export BYOBU_PREFIX
23
 
. "${BYOBU_PREFIX}/lib/${PKG}/include/common"
24
 
 
25
 
# Get the layouts directory
26
 
DIR="$BYOBU_CONFIG_DIR/layouts"
27
 
mkdir -p "$DIR"
28
 
PRESETS="even-horizontal even-vertical main-horizontal main-vertical tiled"
29
 
current_panes=$(tmux list-panes | wc -l)
30
 
 
31
 
list_layouts() {
32
 
        echo
33
 
        echo "Byobu Saved Layouts"
34
 
        local count=0 i= desc= count= p=
35
 
        for i in $PRESETS "$DIR"/*; do
36
 
                desc=${i##*/}
37
 
                count=$(expr $count + 1)
38
 
                [ -f "$i" ] && p=$(head -n1 "$i")
39
 
                [ -n "$p" ] && p=" ($p splits)"
40
 
                echo "  $count. $desc$p"
41
 
        done
42
 
        echo
43
 
        _RET="$count"
44
 
}
45
 
 
46
 
case "$1" in
47
 
        "save")
48
 
                layout=$(tmux list-windows | grep "(active)$" | sed -e "s/.*\[layout //" -e "s/\] .*(active)$//")
49
 
                panes=$(tmux list-panes | wc -l)
50
 
                if [ -n "$2" ]; then
51
 
                        name="$2"
52
 
                else
53
 
                        echo
54
 
                        echo "Restore layout with <shift-F8>, save a layout with <shift-ctrl-F8>"
55
 
                        while true; do
56
 
                                list_layouts
57
 
                                echo -n "Enter a unique name to save this layout: "
58
 
                                name=$(head -n1)
59
 
                                valid=1
60
 
                                for i in $PRESETS "$DIR"/*; do
61
 
                                        i=${i##*/}
62
 
                                        if [ "$i" = "$name" ]; then
63
 
                                                valid=0
64
 
                                        fi
65
 
                                done
66
 
                                [ "$valid" = "1" ] && break
67
 
                        done
68
 
                fi
69
 
                printf "$panes\n$layout\n" > "$BYOBU_CONFIG_DIR/layouts/$name"
70
 
        ;;
71
 
        "restore")
72
 
                if [ -n "$2" ]; then
73
 
                        # Layout selected on the command line
74
 
                        name="$2"
75
 
                else
76
 
                        # List the saved layouts, prompt the user to select one
77
 
                        list_layouts
78
 
                        count="$_RET"
79
 
                        while true; do
80
 
                                echo -n "Select a layout to restore [1-$count]: "
81
 
                                selected=$(head -n1)
82
 
                                if [ -n "$selected" ] && [ $selected -le $count ] && [ $selected -ge 1 ]; then
83
 
                                        break
84
 
                                fi
85
 
                        done
86
 
                        count=0
87
 
                        for i in $PRESETS "$DIR"/*; do
88
 
                                count=$(expr $count + 1)
89
 
                                if [ $count -eq $selected ]; then
90
 
                                        name=$(basename "$i")
91
 
                                        break
92
 
                                fi
93
 
                        done
94
 
                fi
95
 
                # Get the details of the selected layout
96
 
                panes=
97
 
                layout=
98
 
                if [ -f "$DIR/$name" ]; then
99
 
                        panes=$(head -n1 "$DIR/$name")
100
 
                        layout=$(tail -n1 "$DIR/$name")
101
 
                else
102
 
                        if [ $current_panes -eq 1 ]; then
103
 
                                panes=4
104
 
                        else
105
 
                                panes=0
106
 
                        fi
107
 
                        layout="$name"
108
 
                fi
109
 
                # Create panes if nececessary to restore the layout
110
 
                while [ $(tmux list-panes | wc -l) -lt $panes ]; do
111
 
                        tmux split-window
112
 
                        tmux select-layout tiled
113
 
                done
114
 
                # Finally, restore the layout and refresh
115
 
                tmux select-layout "$layout"
116
 
                tmux source "$BYOBU_PREFIX/share/byobu/profiles/tmuxrc"
117
 
        ;;
118
 
        list)
119
 
                list_layouts
120
 
                exit 0
121
 
        ;;
122
 
        *)
123
 
                echo "ERROR: Invalid argument, try [save|restore|list]" 2>&1
124
 
                exit 1
125
 
        ;;
126
 
esac