summaryrefslogtreecommitdiffstats
path: root/lass/5pkgs/q/default.nix
blob: 644be0d174c09d8f76d6272b455760bd0ad86d60 (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
{ pkgs, ... }:
let
  q-cal = let
    # XXX 23 is the longest line of cal's output
    pad = ''{
      ${pkgs.gnused}/bin/sed '
            # rtrim
            s/ *$//

            # delete last empty line
            ''${/^$/d}
          ' \
        | ${pkgs.gawk}/bin/awk '{printf "%-23s\n", $0}' \
        | ${pkgs.gnused}/bin/sed '
              # colorize header
              1,2s/.*/&/

              # colorize week number
              s/^[ 1-9][0-9]/&/
            '
    }'';
  in ''
    ${pkgs.coreutils}/bin/paste \
        <(${pkgs.utillinux}/bin/cal -mw \
              $(${pkgs.coreutils}/bin/date +'%m %Y' -d 'last month') \
            | ${pad}
        ) \
        <(${pkgs.utillinux}/bin/cal -mw \
            | ${pkgs.gnused}/bin/sed '
                # colorize day of month
                s/\(^\| \)'"$(${pkgs.coreutils}/bin/date +%e)"'\>/&/
              ' \
            | ${pad}
        ) \
        <(${pkgs.utillinux}/bin/cal -mw \
              $(${pkgs.coreutils}/bin/date +'%m %Y' -d 'next month') \
            | ${pad}
        ) \
      | ${pkgs.gnused}/bin/sed 's/\t/  /g'
  '';

  q-isodate = ''
    ${pkgs.coreutils}/bin/date \
        '+%Y-%m-%dT%H:%M:%S%:z'
  '';

  q-gitdir = ''
    if test -d .git; then
      #git status --porcelain
      branch=$(
        ${pkgs.git}/bin/git branch \
          | ${pkgs.gnused}/bin/sed -rn 's/^\* (.*)/\1/p'
      )
      echo "± $LOGNAME@''${HOSTNAME-$(${pkgs.nettools}/bin/hostname)}:$PWD .git $branch"
    fi
  '';

  q-power_supply = ''
    for uevent in /sys/class/power_supply/*/uevent; do
      if test -f $uevent; then
        eval "$(${pkgs.gnused}/bin/sed -n '
          s/^\([A-Z_]\+=\)\(.*\)/\1'\'''\2'\'''/p
        ' $uevent)"

        if test "x''${POWER_SUPPLY_CHARGE_NOW-}" = x; then
          continue
        fi

        charge_percentage=$(echo "
          scale=2
          $POWER_SUPPLY_CHARGE_NOW / $POWER_SUPPLY_CHARGE_FULL
        " | ${pkgs.bc}/bin/bc)

        lfc=$POWER_SUPPLY_CHARGE_FULL
        rc=$POWER_SUPPLY_CHARGE_NOW
        #rc=2800
        N=78; N=76
        N=10
        n=$(echo $N-1 | ${pkgs.bc}/bin/bc)
        centi=$(echo "$rc*100/$lfc" | ${pkgs.bc}/bin/bc)
        deci=$(echo "$rc*$N/$lfc" | ${pkgs.bc}/bin/bc)
        energy_evel=$(
          echo -n '☳ ' # TRIGRAM FOR THUNDER
          if   test $centi -ge 42; then echo -n ''
          elif test $centi -ge 23; then echo -n ''
          elif test $centi -ge 11; then echo -n ''
          else                        echo -n ''; fi
          for i in $(${pkgs.coreutils}/bin/seq 1 $deci); do
            echo -n ■
          done
          echo -n ''
          for i in $(${pkgs.coreutils}/bin/seq $deci $n); do
            echo -n ■
          done
          echo '' $rc #/ $lfc
        )
        echo "$energy_evel $charge_percentage"
      fi
    done
  '';

  q-virtualization = ''
    echo "VT: $(${pkgs.systemd}/bin/systemd-detect-virt)"
  '';

  q-wireless = ''
    for dev in $(
      ${pkgs.iw}/bin/iw dev \
        | ${pkgs.gnused}/bin/sed -n 's/^\s*Interface\s\+\([0-9a-z]\+\)$/\1/p'
    ); do
      inet=$(${pkgs.iproute}/bin/ip addr show $dev \
        | ${pkgs.gnused}/bin/sed -n '
            s/.*inet \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p
          ') \
        || unset inet
      ssid=$(${pkgs.iw}/bin/iw dev $dev link \
        | ${pkgs.gnused}/bin/sed -n '
            s/.*\tSSID: \(.*\)/\1/p
          ') \
        || unset ssid
      echo "$dev''${inet+ $inet}''${ssid+ $ssid}"
    done
  '';

  q-online = ''
    if ${pkgs.curl.bin}/bin/curl -s google.com >/dev/null; then
      echo 'online'
    else
      echo offline
    fi
  '';

  q-thermal_zone = ''
    for i in /sys/class/thermal/thermal_zone*; do
      type=$(${pkgs.coreutils}/bin/cat $i/type)
      temp=$(${pkgs.coreutils}/bin/cat $i/temp)
      printf '%s %s°C\n' $type $(echo $temp / 1000 | ${pkgs.bc}/bin/bc)
    done
  '';

  q-todo = ''
    TODO_file=$HOME/TODO
    if test -e "$TODO_file"; then
      ${pkgs.coreutils}/bin/cat "$TODO_file" \
        | ${pkgs.gawk}/bin/gawk -v now=$(${pkgs.coreutils}/bin/date +%s) '
            BEGIN { print "remind=0" }
            /^[0-9]/{
              x = $1
              gsub(".", "\\\\&", x)
              rest = substr($0, index($0, " "))
              rest = $0
              sub(" *", "", rest)
              gsub(".", "\\\\&", rest)
              print "test $(${pkgs.coreutils}/bin/date +%s -d"x") -lt "now" && \
                echo \"\x1b[38;5;208m\""rest esc "\"\x1b[m\" && \
                (( remind++ ))"
            }
            END { print "test $remind = 0 && echo \"nothing to remind\"" }
          ' \
        | {
          # bash needed for (( ... ))
          ${pkgs.bash}/bin/bash
        }
    else
      echo "$TODO_file: no such file or directory"
    fi
  '';

in
# bash needed for <(...)
pkgs.writeBashBin "q" ''
  set -eu
  export PATH=/var/empty
  ${q-cal}
  echo
  ${q-isodate}
  (${q-gitdir}) &
  (${q-power_supply}) &
  (${q-virtualization}) &
  (${q-wireless}) &
  (${q-online}) &
  (${q-thermal_zone}) &
  wait
  ${q-todo}
''