-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsattr.nu
94 lines (77 loc) · 2.38 KB
/
fsattr.nu
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
def 'attr get' [path] {
let output = getfattr -dm '' -- $path;
if not ($output|is-empty) {
$output|from toml
} else {
{}
}
}
def 'attr set' [path, name, value] {
setfattr -n $name -v $value -- $path
}
def 'attr remove' [path, name] {
setfattr -x $name -- $path
}
# List all the tags in this directory
def 'tags here' [path = '.'] {
ls $path | reduce --fold [] {|it, acc| $acc | append (try {tags list $it.name}) }|uniq
}
# List all the tags on a file
def 'tags list' [path] {
let tags = attr get $path|get -i user.xdg.tags;
if $tags != null {
$tags | split row ','
} else {
[]
}
}
# Add a tag to a file
def 'tags add' [path, name] {
let existing = tags list $path;
attr set $path 'user.xdg.tags' ($existing | append $name | str join ',');
}
# Remove a tag from a file
def 'tags remove' [path, name] {
let existing = tags list $path;
let indices = ($existing|enumerate|where item == $name|get index);
let result = if not ($indices|is-empty) { $existing|drop nth ($indices|first) } else { $existing };
attr set $path 'user.xdg.tags' ($result | str join ',');
}
# Get the rating for a file
def rating [path] -> int {
let score = attr get $path|get -i user.baloo.rating;
return (if ($score|is-empty) { null } else { $score|into int });
}
# Set the rating for a file
def 'rating set' [
path,
score: int # Must be between 1 and 10.
] {
attr set $path 'user.baloo.rating' ($score|into string)
}
alias rate = rating set; # FIXME: Name may clash with something
# Remove the rating from a file
def 'rating remove' [path] {
attr remove $path 'user.baloo.rating'
}
# Sets a file's comment if a message is provided, else gets it.
def comment [path, message?] {
if ($message|is-empty) {
return (attr get $path|get -i user.xdg.comment)
} else {
attr set $path 'user.xdg.comment' $message;
}
}
# Remove a file's comment
def 'comment remove' [path] {
attr remove $path 'user.xdg.comment'
}
# TODO: Consider adding glob support, https://www.nushell.sh/book/moving_around.html#glob-patterns-wildcards
# def --wrapped 'ls attr' [...rest] {
# Pipe the output of 'ls' into this to see attributes too.
def 'with attr' [] {
$in
| insert rating {|row| rating $row.name}
| insert tags {|row| tags list $row.name}
| insert comment {|row| comment $row.name}
}