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
|
export def "books add" [
name: string, # name of the book
author: string, # name of the book's author
--finished # mark the book already read - default: false
] {
let book_id = random uuid
{
id: $book_id,
name: $name,
author: $author
finished: ($finished | default false)
} | into sqlite $env.READINGORDER_DB_PATH -t books
print $"Added ($name) successfully to list of books."
books show --id $book_id
}
export def "books show" [
--id: string, # id of the book, identify using `reading-order books show --explore`
--explore # show in nushell explorer
] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
mut books = open $env.READINGORDER_DB_PATH | get books
if $id != null {
$books = $books | where id == $id
}
if ($explore) {
return ($books | explore -p)
}
return $books
}
export def "books set-name" [
id: string, # id of the book, identify using `reading-order books show --explore`
name: string
] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
open $env.READINGORDER_DB_PATH | query db "UPDATE books SET name = ? WHERE id = ?" -p [$name, $id]
print $"Updated name for book with id ($id) successfully."
books show --id $id
}
export def "books set-author-name" [
id: string, # id of the book, identify using `reading-order books show --explore`
author: string
] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
open $env.READINGORDER_DB_PATH | query db "UPDATE books SET author = ? WHERE id = ?" -p [$author, $id]
print $"Updated author name for book with id ($id) successfully."
books show --id $id
}
export def "books set-finished" [
id: string, # id of the book, identify using `reading-order books show --explore`
finished: bool = true,
] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
open $env.READINGORDER_DB_PATH | query db "UPDATE books SET finished = ? WHERE id = ?" -p [$finished, $id]
print $"Updated finished status for book with id ($id) successfully."
books show --id $id
}
# Add a reading order
export def add [
book1_id: string, # id of the book to be read before book 2, identify using `reading-order books show --explore`
book2_id: string, # id of the book to be read after book 1, identify using `reading-order books show --explore`
] {
let reading_order_id = random uuid
{
id: $reading_order_id,
book1: $book1_id,
book2: $book2_id,
} | into sqlite $env.READINGORDER_DB_PATH
print $"Added reading order successfully."
show --id $reading_order_id
}
export def show [
--id: string, # id of the book, identify using `reading-order books show --explore`
--explore # show in nushell explorer
] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
mut reading_orders = open $env.READINGORDER_DB_PATH | get main
if $id != null {
return ($reading_orders | where id == $id)
}
if ($explore) {
return ($reading_orders | explore -p)
}
return $reading_orders
}
export def read-next [] {
if not ($env.READINGORDER_DB_PATH | path exists) {
print "List of books is empty. If it shouldn't be empty, check the READINGORDER_DB_PATH environment variable."
return
}
let reading_orders = open $env.READINGORDER_DB_PATH | get main
mut books = open $env.READINGORDER_DB_PATH | get books | where { |book|
if $book.finished == 1 {
return false
}
for order in $reading_orders {
if $order.book2 != $book.id {
continue
}
let book_to_read_before = books show --id $order.book1
if $book_to_read_before.finished != 1 {
return false
}
}
return true
}
return $books
}
|