summarylogtreecommitdiffstats
path: root/paclast
blob: 0de5156b3a4516d9750f44912a180f4fa2471643 (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
#!/bin/sh
# list installed pacman packages in chronological order.
# example usage: $ paclast -tc | head

main() {
    opts="$(getopt -otcph -- "$@")" && eval set -- "$opts" || help
    r= ; c= ; p=
    while true; do case "$1" in
        -t) t='|relative'; shift ;;
        -c) c='|colorize'; shift ;;
        -p) p="|${PAGER:-less}"; shift ;;
        --) break ;;
         *) help ;;
    esac; done
    eval list $t $c $p
}

list() {
    list_installed | get_explicitly_installed | sort -rsk1,1 |
        awk ' BEGIN{FS=OFS=""}
            {$1=$18=$19=$20=$21=$22=$23=$24=$25=$26="";$12=$27=" "; print $0}'
}

# print a list of all installed packages and their first install date.
list_installed() {
    awk '/installed/ {print $1" "$4}' /var/log/pacman.log | tac | sort -uk2,2
}

# filter only explicitly installed packages through stdin
get_explicitly_installed() {
    awkcmd="$(
        pacman -Qqe | while IFS= read -r expkg; do
            printf '$2=="'$expkg'" || '
        done | rev | cut -c5- | rev
    )"
    awk "$awkcmd"
}

# insert a relative date column in every line.
relative() {
    while read -r line; do
        pkg="$( echo "$line" | cut -d' ' -f3)"
        time="$(echo "$line" | cut -d' ' -f2)"
        date="$(echo "$line" | cut -d' ' -f1)"
        days_ago="$(get_days_ago "$date")"
        echo "$date $time ($days_ago days ago) $pkg"
    done
}

# print how many days ago a given date is.
# the digit 2 after $given_date indicates the time 02:00:00
# it's there because if it's omitted, date(1) assumes the time
# to be 00:00:00, and such time might be invalid because of daylight saving.
get_days_ago() {
    given_date="$1"
    echo $(( ($(date +%s) - $(date -d "$given_date 2" +%s)) / (86400) ))
}

# colorize the output
colorize() {
    while read -r line; do # add nice colors to output.
        printf '\033[1;32m%s\n' "$line"
        read -r line
        printf '\033[1;36m%s\n' "$line"
    done

    # revert terminal's text color back to normal.
    printf '\033[0m'
}

help() {
cat << 'eof'
list installed pacman packages in chronological order.
Usage: paclast [-tcp]
Options:
  -t     add an 'x days ago' column to output.
  -c     colorize output.
  -p     show output in pager.
eof
exit
}

main "$@"
exit