summaryrefslogtreecommitdiffstats
path: root/pkgs/simple/xdpytools/src/xdpysel
blob: e08015576690e4908aad7211316f38f59445323b (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
#! /bin/sh
# usage: xdpysel {prev,next}
# Print the number of the selected X display.

find /tmp/.X11-unix -mindepth 1 -maxdepth 1 |
jq -Rrs --arg command "$1" '
  (
    split("\n") |
    map(
      select(.!="") |
      match("^.*/X([0-9]+)$").captures[0].string |
      tonumber
    )
  )
    as $all_displays |

  (
    env.DISPLAY |
    match("^:([0-9]+)(?:[.][0-9]+)?$").captures[0].string |
    tonumber
  )
    as $current_display |

  ($all_displays | length) as $all_displays_count |

  ($all_displays|index($current_display))
    as $current_index |

  (($current_index + 1) % $all_displays_count)
    as $next_index |

  (($all_displays_count + $current_index - 1) % $all_displays_count)
    as $prev_index |

  $all_displays[$prev_index] as $prev_display |
  $all_displays[$next_index] as $next_display |

  {
    prev: $prev_display,
    next: $next_display,
  }[$command]
    as $result |

  if $result | type == "number" then
    $result
  else
    "xdpysel: bad argument: \($command)\n" | halt_error(-1)
  end
'