aboutsummaryrefslogtreecommitdiff
path: root/wmutils/bin/group
blob: 0d105dc0fb41f11bb2d2d8aba9870f66124b39da (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
#!/bin/sh
#
# wmg by:
#
# ▓█████▄  ▄████▄   ▄▄▄     ▄▄▄█████▓
# ▒██▀ ██▌▒██▀ ▀█  ▒████▄   ▓  ██▒ ▓▒
# ░██   █▌▒▓█    ▄ ▒██  ▀█▄ ▒ ▓██░ ▒░
# ░▓█▄   ▌▒▓▓▄ ▄██▒░██▄▄▄▄██░ ▓██▓ ░ 
# ░▒████▓ ▒ ▓███▀ ░ ▓█   ▓██▒ ▒██▒ ░ 
#  ▒▒▓  ▒ ░ ░▒ ▒  ░ ▒▒   ▓▒█░ ▒ ░░   
#  ░ ▒  ▒   ░  ▒     ▒   ▒▒ ░   ░    
#  ░ ░  ░ ░          ░   ▒    ░      
#    ░    ░ ░            ░  ░        
#  ░      ░                          

GRP_DIR="/tmp/wmutils/groups"
USE_FIFO=yes

setup() {
  [ -d "$GRP_DIR" ] || mkdir -p $GRP_DIR
  if [ -n "$USE_FIFO" ]
  then
    [ -p "$GRP_DIR/.fifo" ] || mkfifo $GRP_DIR/.fifo
  fi
}

sed_apply() {
  TMP=`mktemp /tmp/grp.XXXXXX`
  sed $1 < $2 > $TMP && mv $TMP $2 || rm $TMP
  [ -n "$DEBUG" ] && echo "sed $1 < $2 > $TMP && mv $TMP $2 || rm $TMP"
}

pop() {
  sed_apply "/$1/d" $2
}

push() {
  echo $1 >> $GRP_DIR/$2
}

view() {
  while read WID
  do mapw -m $WID
  done < $GRP_DIR/$1
}

hide() {
  while read WID
  do mapw -u $WID
  done < $GRP_DIR/$1
}

pop_from_all() {
  for GRP in $GRP_DIR/*
  do
    pop $1 $GRP
  done
}

case $1 in
view)
  setup
  if [ -f "$GRP_DIR/$2" ]
  then
    for GRP in $GRP_DIR/*
    do hide `basename $GRP`
    done
    view $2
  else
    echo "group does not exist"
    exit 1
  fi

  # customize your fifo printing here
  if [ -n "$USE_FIFO" ]
  then
    for GRP in $GRP_DIR/*
    do
      GRP_BASE="`basename $GRP`"
      GRP_NUM=`wc -l < $GRP | tr -cd '[:digit:]'`
      [ $GRP_NUM -ne 0 ] && printf '^fg(#888)' || printf '^fg(#555)'
      [ $GRP_BASE -eq $2 ] && printf '^fg(orange)<'
      printf "$GRP_BASE^fg()"
      [ $GRP_BASE -eq $2 ] && printf '>^fg()'
    done > $GRP_DIR/.fifo
    echo >> $GRP_DIR/.fifo
  fi

  ;;
hide)
  setup
  hide $2
  ;;
add)
  setup
  pop_from_all $2
  push $2 $3
  hide $3
  view $3
  ;;
remove)
  setup
  pop_from_all $2
  ;;
esac