summaryrefslogtreecommitdiff
path: root/fish/conf.d/quickedit.fish
blob: eee50050465404f35e221434d9be3f652e625de2 (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
set -gx QUICKEDIT_DIR ~/QuickEdit

function quickedit::postaction
    set -l file_path $argv[1]
    set -l make_run 0

    if test -f Makefile
        make
        set make_run 1
    end

    # Go to git toplevel dir (if exists)
    cd (dirname $file_path)
    set -l git_dir (git rev-parse --show-toplevel 2>/dev/null)
    if test $status -eq 0
        cd $git_dir
    end
    if not test $make_run -eq 1
        if test -f Makefile
            make
        else if test -f Justfile
            just
        end
    end
    if test -d .git
        git commit -a -m Update
        git pull
        git push
    end
end

function quickedit
    set -l prev_dir (pwd)
    set -l grep_pattern .

    if test (count $argv) -gt 0
        set grep_pattern $argv[1]
    end

    cd $QUICKEDIT_DIR
    set -l index_age 99999
    if test -f .index
        if test (uname) = Darwin
            set index_age (math (date +%s) - (stat -f %m .index))
        else
            set index_age (math (date +%s) - (stat -c %Y .index))
        end
    end
    if test $index_age -gt 86400
        echo Indexing quickedit
        find -L . -type f -not -path '*/.*' | sort >$QUICKEDIT_DIR/.index.tmp && mv $QUICKEDIT_DIR/.index.tmp $QUICKEDIT_DIR/.index
    end

    set files (grep -E "$grep_pattern" $QUICKEDIT_DIR/.index)
    switch (count $files)
        case 0
            echo No result found
            return
        case 1
            set file_path $files[1]
        case '*'
            set file_path (printf '%s\n' $files | fzf)
    end

    if editor::helix::open_with_lock $file_path
        quickedit::postaction $file_path
    end

    cd $prev_dir
end

function slowedit
    if test -f $QUICKEDIT_DIR/.index
        rm $QUICKEDIT_DIR/.index
    end
    quickedit $argv
end

abbr -e E slowedit
abbr -a e quickedit
abbr -a er "ranger $QUICKEDIT_DIR"
abbr -a cdquickedit "cd $QUICKEDIT_DIR"
abbr -a cdnotes 'cd ~/Notes'
abbr -a cdfish 'cd ~/.config/fish/conf.d'
abbr -a cddocs 'cd ~/Documents'
abbr -a cdocs 'cd ~/Documents'