summaryrefslogtreecommitdiff
path: root/config/X/xmobar/scripts/winfo
blob: 033ab495a526528a3266ff5bdf241b7e2483cdb1 (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
#!/bin/bash
XMDIR="$HOME/.config/xmobar"

terminal="$(\
  grep -m 1 "myTerminal *=" ~/.config/xmonad/xmonad.hs \
    | cut -f 2 -d '"' \
)"

# Gets info over active window in X server
# made to work with xmobar
# Make option to change the terminal

# ID of focused window
GetWinID () {
  xdotool getWindowfocus
}

# Gets name of program running active window
GetWinName () {
  xdotool getwindowclassname $(GetWinID)
}

GetWinIcons () {
    NAME="$(GetWinName | cut -f 1 -d " ")"
    test -z "$NAME" || \
    grep -i -m 1 "${NAME}" "$XMDIR/scripts/winfo_icons" | cut -f1 -d' '
}

# Get current directory of active window 
# for st
GetCWD (){
  # Parent PID of process running in window
  PARENT_PID="$(\
    xdotool getwindowpid $(GetWinID)\
    )"

  # PID of process runnning in window
  PROCESS="$(\
    pstree -p ${PARENT_PID} \
      | head -n1 \
      | sed \
      -e 's/^.*-\(.*\)$/\1/g' \
      -e 's/^ *\([^{:( ]*\).*$/\1/g' \
  )"

  echo -n "${PROCESS}" \
    | sed \
    -e "s;^zsh$;<fn=1></fn>&;g" \
    -e "s;^htop$;<fn=1></fn> &;g" \
    -e "s;^ranger$;<fn=1></fn> &;g"
}

# Get title of window / name of process
# takes one argument, window name
GetWinTitle() {
  # Max length of string
  MAX_LEN=64
  xdotool GetWindowName $(GetWinID) \
    | sed \
    -e 's/ — Mozilla Firefox//g' \
    -e "s;${HOME};~;g" \
    -e "s/^\(.\{$MAX_LEN\}\).*$/\1../g" \
    -e 's/ *\.\.$/\.\./g' \
    -e "s;^${terminal^}$;$(GetCWD);g"
}

# Help function
Help () {
  echo "-- This is winfo --"
  echo "Gets information over focused window"
  echo
  echo "Usage: getFWinfo [OPTION]"
  echo "  -h   display this help and exit"
  echo "  -n   for name"
  echo "  -i   for icons"
  echo "  -t   for titles"
}

##############################################
#################### MAIN ####################
##############################################

# Handles options
if [ $# -eq 0 ]
then
  Help
  exit 0
fi

while getopts ":hinTt" option
do
  case "$option" in

    h) # Prints help message
      Help
      exit 0
      ;;

    T) # Get terminal specified by xmonad
      echo ${terminal}
      exit 0
      ;;

    i) # Get window icons, uses GetWinName
      GetWinIcons
      exit 0
      ;;

    n) # Get window name "WM_CLASS"
      GetWinName
      # Capitalize
      exit 0
      ;;

    t) # Get window title, like name of webpage/working dir
      GetWinTitle
      exit 0
      ;;

  esac
done
  echo "Invalid option."
  Help
  exit 1