aboutsummaryrefslogtreecommitdiff
path: root/session
blob: ae732ab8d6a64ae92a0018827650b78a8903a3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
#
# A lightweight session manager.
# Open a work session, which might consist of VIM, tmux, etc sessions.
#

# Parameters
NAME="$0"
BASENAME="`basename $0`"
CONFIG="$HOME/.config/session"
CUSTOM="$CONFIG/custom"
IGNORE="$CONFIG/ignore"

# Check if a given program is not being ignored by configuration
function __session_not_ignored {
  if [ -z "$1" ]; then
    return 1
  fi

  if [ ! -z "$UNIGNORE" ]; then
    if echo "$UNIGNORE" | grep -q " $1 "; then
      return 0
    else
      return 1
    fi
  fi

  if [ ! -e "$IGNORE" ]; then
    return 0
  fi

  if grep -q "^$1$" $IGNORE; then
    return 1
  fi

  return 0
}

# Query for program sessions
function __session_query {
  # Custom
  if __session_not_ignored custom && [ -d "$CUSTOM" ]; then
    ls -1 $CUSTOM
  fi

  # VIM
  if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions" ]; then
    ls -1 $HOME/.local/share/vim/sessions | sed -e 's/\.vim//g'
  fi

  # Tmux
  if __session_not_ignored tmux && [ -d "$HOME/.tmux" ]; then
    ls -1 $HOME/.tmux
  fi

  # Screen
  #if __session_not_ignored screen && [ -d "$HOME/.screen" ]; then
  #  ls -1 $HOME/.screen
  #fi

  # Luakit
  if __session_not_ignored luakit && [ -d "$HOME/.local/share/luakit/sessions" ]; then
    ls -1 $HOME/.local/share/luakit/sessions
  fi

  # Qutebrowser
  if __session_not_ignored qutebrowser && [ -d "$HOME/.local/share/qutebrowser/sessions" ]; then
    ls -1 $HOME/.local/share/qutebrowser/sessions | sed -e 's/.yml$//'
  fi

  # Chromium
  if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles" ]; then
    ls -1 $HOME/.config/chromium-profiles
  fi

  # Firefox
  if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles" ]; then
    ls -1 $HOME/.mozilla/firefox/profiles
  fi
}

# List available sessions
function __session_list {
  n="0"
  __session_query | sort | uniq | while read session; do
    echo -en "$n. $session:"

    # Custom
    if __session_not_ignored custom && [ -e "$CUSTOM/$session" ]; then
      echo -n " custom"
    #else
    #  echo " -"
    fi

    # Check VIM session
    if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions/$session.vim" ]; then
      echo -n " vim"
    #else
    #  echo " -"
    fi

    # Check tmux session
    if __session_not_ignored tmux && [ -e "$HOME/.tmux/$session" ]; then
      echo -n " tmux"
    #else
    #  echo " -"
    fi

    # Screen
    if __session_not_ignored screen && [ -e "$HOME/.screen/$session" ]; then
      echo -n " screen"
    #else
    #  echo " -"
    fi

    # Luakit
    if __session_not_ignored luakit && [ -e "$HOME/.local/share/luakit/sessions/$session" ]; then
      echo -n " luakit"
    #else
    #  echo " -"
    fi

    # Qutebrowser
    if __session_not_ignored qutebrowser && [ -e "$HOME/.local/share/qutebrowser/sessions/$session.yml" ]; then
      echo -n " qutebrowser"
    #else
    #  echo " -"
    fi

    # Chromium
    if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles/$session" ]; then
      echo -n " chromium"
    #else
    #  echo " -"
    fi

    # Firefox
    if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles/$session" ]; then
      echo -n " firefox"
    #else
    #  echo " -"
    fi

    echo ""
    let ++n
  done | column -t -c 6 #| sed -e 's/^/\t/'
}

# Session chooser mennu
function __session_chooser {
  __session_list
  read -rep "Choose session: " n

  local name="`echo $n | awk '{ print $1 }'`"
  #local programs="`echo $n | awk '{ print $2 }'`"
  local programs="`echo $n | sed -e \"s/^$name//\"`"

  if [ ! -z "$name" ]; then
    session="$(__session_list | grep -E "(^$name.| $name:)" | sed -e "s/^[0-9]*. //" | cut -d : -f 1)"

    if [ ! -z "$session" ]; then
      __session_open $session $programs
    fi
  fi
}

# Run a program
function __session_exec {
  if [ -z "$1" ]; then
    return
  fi

  # Ensure it won't be killed if parent exit
  nohup setsid $* >> $HOME/.xsession-errors 2>&1 &

  # Indicate that we should wait for the main script to wait
  # while the program starts
  SHOULD_SLEEP="5"
}

# Open a session
function __session_open {
  local session="$1"
  shift
  local programs="$*"

  if [ ! -z "$programs" ]; then
    UNIGNORE=" $programs "
  fi

  # Custom
  if __session_not_ignored custom && [ -e "$CUSTOM/$session" ]; then
    __session_exec $CUSTOM/$session
  fi

  # Check VIM session
  if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions/$session.vim" ]; then
    if which vim &> /dev/null; then
      __session_exec terminal vim -S $HOME/.local/share/vim/sessions/$session.vim
    fi
  fi

  # Check tmux session
  if __session_not_ignored tmux && [ -e "$HOME/.tmux/$session" ]; then
    if which tmux &> /dev/null; then
      __session_exec terminal shell $session
    fi
  fi

  # Screen
  if __session_not_ignored screen && [ -e "$HOME/.screen/$session" ]; then
    if which screen &> /dev/null; then
      __session_exec terminal wscreen $session
    fi
  fi

  # Luakit
  if __session_not_ignored luakit && [ -e "$HOME/.local/share/luakit/sessions/$session" ]; then
    if which luakit &> /dev/null; then
      export LUAKIT_SESSION="$session"
       __session_exec luakit
    fi
  fi

  # Qutebrowser
  if __session_not_ignored qutebrowser && [ -e "$HOME/.local/share/qutebrowser/sessions/$session.yml" ]; then
    if which qutebrowser &> /dev/null; then
      __session_exec qutebrowser -r $session
    fi
  fi

  # Chromium
  if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles/$session" ]; then
    if which chromium-profile &> /dev/null; then
      __session_exec chromium-profile $session
    fi
  fi

  # Firefox
  if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles/$session" ]; then
    __session_exec firefox-profile $session
  fi
}

# Ensure config folders exist
mkdir -p $CONFIG $CUSTOM

# Dispatch
if [ "$BASENAME" == "session-list" ]; then
  __session_list
elif [ "$BASENAME" == "session-chooser" ]; then
  __session_chooser
elif [ "$BASENAME" == "terminal-session-chooser" ]; then
  terminal session-chooser
elif [ ! -z "$1" ] && echo "$1" | grep -q -- '--list'; then
  __session_list
elif [ ! -z "$1" ] && echo "$1" | grep -q -- '--chooser'; then
  __session_chooser
elif [ -z "$1" ]; then
  echo "usage: $BASENAME [--list|--chooser] <session> <program>"
  echo "available sessions:"
  echo ""
  __session_list
  exit 1
else
  __session_open $*
fi

# Check if we should wait programs to exec
if [ ! -z "$SHOULD_SLEEP" ]; then
  sleep $SHOULD_SLEEP
fi