summaryrefslogtreecommitdiff
path: root/.config/nushell/wishlist.nu
blob: 10cc0f52db38079a3f8dadb00d87102668b3c76e (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
export def add [
    name: string, # name of the item
    --comment: string, # comment for the item
    --sources: list<string>, # a source to acquire the item, typically a URL
    --importance: int # a number to indicate the importance, choose the scaling yourself, default: 0
] {
    let item_id = random uuid    
    {
        id: $item_id,
        name: $name,
        comment: ($comment | default ''),
        acquired: false,
        importance: ($importance | default 0)
    } | into sqlite $env.WISHLIST_DB_PATH

    print $"Added ($name) successfully to the wishlist."

    if $sources != null and ($sources | length) > 0 {
        $sources | each { |source|
            let link_id = random uuid
            { 
                id: (random uuid),
                item_id: $item_id,
                content: $source
            } | into sqlite $env.WISHLIST_DB_PATH -t sources
        }
        print $"Added sources for ($name) successfully."
    }
    show --id $item_id
}

export def show [
    --id: string, # id of the item, identify using `wishlist show --explore`
    --explore # show in nushell explorer
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist is empty. If it shouldn't be empty, check the WISHLIST_DB_PATH environment variable." 
        return
    }
    mut wishlist = open $env.WISHLIST_DB_PATH | get main

    if $id != null {
        $wishlist = $wishlist | where id == $id
    }

    if ($explore) {
        return ($wishlist | explore -p)
    }

    return $wishlist
}

export def set-comment [
    id: string, # id of the item, identify using `wishlist show --explore`
    comment: string
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "UPDATE main SET comment = ? WHERE id = ?" -p [$comment, $id]
    print $"Updated comment for item with id ($id) successfully."
    show --id $id
}

export def set-importance [
    id: string, # id of the item, identify using `wishlist show --explore`
    importance: int
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "UPDATE main SET importance = ? WHERE id = ?" -p [$importance, $id]
    print $"Updated importance for item with id ($id) successfully."
    show --id $id
}

export def check [
    id: string, # id of the item, identify using `wishlist show --explore`
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "UPDATE main SET acquired = true WHERE id = ?" -p [$id]
    print $"Checked off item with id ($id) successfully."
    show --id $id

}

export def remove [
    id: string, # id of the item, identify using `wishlist show --explore`
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "DELETE FROM main WHERE id = ?" -p [$id]
    print $"Removed item with id ($id) successfully."
    show
}

# Get the highest value of importance in the wishlist
export def highest-importance [] {
    show | get importance | reduce { |it, acc| if ($it > $acc) { $it } else { $acc }}
}

# Get the lowest value of importance in the wishlist
export def lowest-importance [] {
    show | get importance | reduce { |it, acc| if ($it < $acc) { $it } else { $acc }}
}

export def "sources show" [
    --id: string, # id of the source, identify using `wishlist sources show --explore`
    --item-id: string, # id of the item, identify using `wishlist show --explore`
    --explore # show in nushell explorer
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    mut sources = open $env.WISHLIST_DB_PATH | get sources

    if $id != null {
        $sources = $sources | where id == $id
    } else if $item_id != null {
        $sources = $sources | where item_id == $item_id
    }

    if ($explore) {
        return ($sources | explore -p)
    }

    return $sources
}

export def "sources add" [
    content: string # content of the source to acquire the item, typically a URL
    item_id: string, # id of the item, identify using `wishlist show --explore`
] {
    let source_id = random uuid
    {
        id: $source_id,
        item_id: $item_id,
        content: $content,
    } | into sqlite $env.WISHLIST_DB_PATH -t sources
    print $"Added new source to item with id ($item_id) successfully."
    sources show --id $source_id
}

export def "sources update" [
    id: string # id of the source, identify using `wishlist sources show --explore`
    content: string # new content for the given source
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "UPDATE sources SET content = ? WHERE id = ?" -p [$content, $id]
    print $"Updated source with id ($id) successfully."
    sources show --id $id
}

export def "sources remove" [
    id: string, # id of the source, identify using `wishlist sources show --explore`
] {
    if not ($env.WISHLIST_DB_PATH | path exists) {
        print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." 
        return
    }

    open $env.WISHLIST_DB_PATH | query db "DELETE FROM sources WHERE id = ?" -p [$id]
    print $"Removed source with id ($id) successfully."
    sources show
}