summaryrefslogtreecommitdiffstats
path: root/pkgs/simple/font-size-alacritty.nix
blob: d37f0f055f2a778d6ab495f1ad9f6210816272f8 (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
{ pkgs }:

pkgs.writeDashBin "font-size-alacritty" ''
  # usage: font-size-alacritty (+N|-N|=N)
  # Increase by, decrease by, or set font size to the value N.

  set -efu

  min_size=5

  op=''${1%%[0-9]*}
  op=''${op:-=}

  value=''${1#[=+-]}

  window_id=$(${pkgs.xdotool}/bin/xdotool getactivewindow)

  current_size=$(
    ${pkgs.xorg.xprop}/bin/xprop -notype -id $window_id FONT_SIZE |
    ${pkgs.gnused}/bin/sed -rn 's/.* = ([0-9]+)$/\1/p'
  )

  # usage: set_font_size WINDOW_ID FONT_SIZE
  set_font_size() {
    ${pkgs.alacritty}/bin/alacritty msg config -w $1 font.size=$2
    ${pkgs.xorg.xprop}/bin/xprop -id $1 -f FONT_SIZE 32c -set FONT_SIZE $2
  }

  # usage: reset_font_size WINDOW_ID
  reset_font_size() {
    ${pkgs.alacritty}/bin/alacritty msg config -w $1 font.size=$min_size
    ${pkgs.xorg.xprop}/bin/xprop -id $1 -remove FONT_SIZE
  }

  # usage: make_next_size
  make_next_size() {
    case $op in
      -) next_size=$(expr $current_size - $value) ;;
      =) next_size=$value ;;
      +)
        next_size=$(expr $current_size + $value)
        test $next_size -ge $min_size || next_size=$min_size
        ;;
    esac
  }

  if test -z "$current_size"; then
    current_size=0
    make_next_size
    if test $next_size -ge $min_size; then
      ${pkgs.alacritty}/bin/alacritty msg config -w $window_id \
          font.normal.family='Input Mono' \
          font.normal.style=Condensed \
          font.bold.family='Input Mono' \
          font.bold.style=Bold
      set_font_size $window_id $next_size
    fi
  else
    make_next_size
    if test $next_size -ge $min_size; then
      set_font_size $window_id $next_size
    else
      ${pkgs.alacritty}/bin/alacritty msg config -w $window_id -r
      reset_font_size $window_id
    fi
  fi
''