about summary refs log tree commit diff stats
path: root/pkgs/by-name/ne/neorg/functions/inputs.sh
blob: d47b129a721eaf6d31f973d2230619e40893b087 (plain) (blame)
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
#!/usr/bin/env dash

_git_commit() {
    message="$1"

    cd "$(dirname "%NEORG_INPUTS_STORAGE_FILE")" || die "BUG. This should exist."
    [ -d .git ] || git init
    git add .
    git commit --message "$message" --no-gpg-sign
}

inputs0add() {
    url_file="$1"

    mkdir --parents "$(dirname "%NEORG_INPUTS_STORAGE_FILE")"

    {
        # Add another newline
        echo ""
        # echo "# $url_file "

        clean "$url_file"
    } >>"%NEORG_INPUTS_STORAGE_FILE" &&
        msg2 "Successfully added file '$url_file' with $(clean "$url_file" | wc -l) entries to the url list"

    _git_commit "Add entries from '$url_file' ($(clean "$url_file" | wc -l))"
}

inputs0review() {
    base_profile="$1"

    [ -f "%NEORG_INPUTS_STORAGE_FILE" ] || die "'%NEORG_INPUTS_STORAGE_FILE' is not a file. Have you added something with 'inputs_add' yet?"

    done_urls="$(mktmp)"

    # We assume that the project is not yet open.
    firefox -P "$base_profile" &
    # Give it some time to start up.
    sleep 2

    if [ "$(wc -l <"%NEORG_INPUTS_STORAGE_FILE")" -gt 100 ]; then
        echo "Your would want to review more than 100 inputs. Limiting it to the first 100 entries."
    fi

    head --lines=100 "%NEORG_INPUTS_STORAGE_FILE" | while read -r url; do
        echo "-> '$url'"
        firefox -P "$base_profile" "$url"

        echo "$url" >>"$done_urls"
    done

    # Wait for the Firefox process from above to finish.
    wait

    tmp="$(mktmp)"
    # source: https://stackoverflow.com/a/24324455
    awk 'NR==FNR {a[$0]=1; next} !a[$0]' "$done_urls" "%NEORG_INPUTS_STORAGE_FILE" >"$tmp"

    mv "$tmp" "%NEORG_INPUTS_STORAGE_FILE"

    _git_commit "Dump entries into firefox profile '$base_profile'"
}