Plugin for nnn file manager, to only delete files from the last-selected directory. Written in Odin ("odinlang").
/*
This code is licensed under the following standard ISC license:
Copyright © 2024 Brett Mahar.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
// Build with: `odin build nnn-delete-pwd-only.odin -file -o:speed`
// Place executable in ~/.config/nnn/plugins/
// and set environmental variable NNN_PLUG=x:nnn-delete-pwd-only
package main
import "core:os"
import "core:fmt"
import "core:strings"
import "core:path/filepath"
import "base:builtin"
filename : string = ""
theName : string = ""
slashCount : int = 0
largest : int = 0
smallest : int = 200
A1 := [dynamic]string{}
A2 := [dynamic]string{}
A3 := [dynamic]int{}
main :: proc() {
parseSelection()
recurse_ls()
countSlash()
getLargest()
deleter()
os.remove("~/.config/nnn/.selection")
}
parseSelection :: proc() {
f, err := os.read_entire_file("~/.config/nnn/.selection")
defer delete(f)
if err == false {
fmt.println(" Selection file does not exist .. have you selected anything?", err)
}
listString := strings.clone(string(f))
file_names := strings.split(listString, "\x00")
defer delete(file_names)
lastDir, _ := filepath.split(file_names[len(file_names) -1 ])
// Filter out any files or dirs from `selection` that don't match the last-selected directory:
for i := 0; i < len(file_names); i += 1 {
filename = file_names[i]
currentDir, _ := filepath.split(filename)
if lastDir == currentDir {
append(&A1, filename)
}
}
}
recurse_ls :: proc() {
lengthIs := len(A1)
for i := 0; i < lengthIs; i += 1 {
theName = string(A1[i])
filepath.walk(
theName, proc(info: os.File_Info, in_err: os.Errno, user_data: rawptr) -> (err: os.Errno, skip_dir: bool) {
assert(err == os.ERROR_NONE)
x := info.fullpath
append(&A2, x)
return
}, nil)
}
}
countSlash :: proc() {
for k := 0; k < len(A2); k += 1 {
slashCount = 0
for codepoint, index in A2[k] {
if codepoint == '/' {
slashCount += 1
}
}
append(&A3, slashCount)
}
}
getLargest :: proc() {
for h := 0; h < len(A3); h += 1 {
if A3[h] > largest {
largest = A3[h]
}
if A3[h] < smallest {
smallest = A3[h]
}
}
}
deleter :: proc() {
for largest >= smallest {
for h := 0; h < len(A3); h += 1 {
if A3[h] == largest {
if os.is_dir(A2[h]) {
os.remove_directory(A2[h])
} else {
os.remove(A2[h])
}
}
}
largest -= 1
}
}
© Brett Mahar
site nginx on openbsd