summaryrefslogtreecommitdiff
path: root/fish/conf.d/tmputils.fish
blob: fb93b3827dfdc00b31e157c9304db906cd90a697 (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
set -gx TMPUTILS_DIR ~/data/tmp
set -gx TMPUTILS_TMPFILE ~/.tmpfile

function tmpdir
    set -l name $argv[1]
    set -l dir "$TMPUTILS_DIR/$name"
    if not test -d $dir
        mkdir -p $dir
    end
    cd $dir
end

function tmpnew
    set -l name $argv[1]
    tmpdir $argv
    tmux::attach $name
end

function tmpls
    if not test -d $TMPUTILS_DIR
        return
    end
    ls $TMPUTILS_DIR
end

function tmptee
    set -l name $argv[1]
    if test -z "$name"
        set name (date +%s)
    else
        set -e argv[1]
    end
    set -l file "$TMPUTILS_DIR/$name"
    if not test -d $TMPUTILS_DIR
        mkdir -p $TMPUTILS_DIR
    end
    tee $argv $file
    echo $file >$TMPUTILS_TMPFILE
end

function tmpcat
    set -l name $argv[1]
    if test -z "$name"
        cat (tmpfile)
        return
    end
    cat "$TMPUTILS_DIR/$name"
end

function tmpedit
    set -l name $argv[1]
    if test -z "$name"
        $EDITOR (tmpfile)
        return
    end
    $EDITOR "$TMPUTILS_DIR/$name"
end

function tmpgrep
    set -l name $argv[1]
    set -e argv[1]
    tmcpat $name | grep $argv
end

function tmpfile
    cat $TMPUTILS_TMPFILE
end

function tmpmove
    set -l name (basename (pwd))
    set -l src (pwd)
    set -l dest ~/Notes/tmp/$name

    if test "$src" != "$TMPUTILS_DIR/$name"
        echo "tmpmove: not inside a tmp directory ($TMPUTILS_DIR/<name>)"
        return 1
    end

    mkdir -p ~/Notes/tmp
    mv $src $dest
    cd $dest
    echo "Moved $src -> $dest"
end

function tmputils::__stat_mtime --argument file
    # Portable file mtime in seconds since epoch (Linux vs macOS)
    if test (uname) = Darwin
        stat -f %m "$file" 2>/dev/null
    else
        stat -c %Y "$file" 2>/dev/null
    end
end

function tmputils::clean
    if not test -d "$TMPUTILS_DIR"
        echo "tmputils::clean: TMPUTILS_DIR ($TMPUTILS_DIR) does not exist"
        return 1
    end

    set -l old_dir "$TMPUTILS_DIR/OLD"
    mkdir -p $old_dir

    set -l datestamp (date +%Y%m%d)
    set -l threshold 31
    set -l now (date +%s)

    for folder in $TMPUTILS_DIR/*
        # Skip the OLD directory itself and non-directories
        test "$folder" = "$old_dir"; and continue
        test -d "$folder"; or continue

        # Find the most recently modified file inside the folder (including subdirs)
        set -l newest
        if test (uname) = Darwin
            set newest (find "$folder" -type f -exec stat -f %m {} + 2>/dev/null | sort -rn | head -1)
        else
            set newest (find "$folder" -type f -printf '%T@\n' 2>/dev/null | sort -rn | head -1)
        end

        # Determine mtime: newest file, or folder mtime if empty
        set -l mtime
        if test -n "$newest"
            set mtime (math floor "$newest")
        else
            set mtime (tmputils::__stat_mtime "$folder")
        end

        # Skip if we couldn't determine mtime
        if test -z "$mtime"
            echo "tmputils::clean: skipping $folder (could not read mtime)"
            continue
        end

        set -l age_days (math \( $now - $mtime \) / 86400)

        if test -n "$age_days"; and test "$age_days" -ge $threshold
            set -l basename (basename "$folder")
            set -l dest "$old_dir/$basename.$datestamp"
            echo "Moving $folder -> $dest (stale $age_days days)"
            mv "$folder" "$dest"
        end
    end
end

abbr -a cdtmp "cd $TMPUTILS_DIR"
abbr -a tmpn tmpnew
abbr -a temp tmpnew
abbr -a tmp tmpnew