~calmbit/suki

f92a4392605a6e334a260d5034e87f35b50ed8c5 — CalmBit 7 months ago
init commit
9 files changed, 230 insertions(+), 0 deletions(-)

A .gitignore
A .vscode/launch.json
A .vscode/settings.json
A Cargo.lock
A Cargo.toml
A LICENSE
A README.md
A example.suki
A src/main.rs
A  => .gitignore +6 -0
@@ 1,6 @@
/target
**/*.rs.bk
*.png
*.jpg
*.suki
!example.suki
\ No newline at end of file

A  => .vscode/launch.json +45 -0
@@ 1,45 @@
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'suki'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=suki",
                    "--package=suki"
                ],
                "filter": {
                    "name": "suki",
                    "kind": "bin"
                }
            },
            "args": ["t", "s"],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in executable 'suki'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--bin=suki",
                    "--package=suki"
                ],
                "filter": {
                    "name": "suki",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}
\ No newline at end of file

A  => .vscode/settings.json +9 -0
@@ 1,9 @@
{
    "spellright.language": [
        "en"
    ],
    "spellright.documentTypes": [
        "markdown",
        "latex"
    ]
}
\ No newline at end of file

A  => Cargo.lock +6 -0
@@ 1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "suki"
version = "0.1.0"


A  => Cargo.toml +9 -0
@@ 1,9 @@
[package]
name = "suki"
version = "0.1.0"
authors = ["CalmBit <EB5473@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

A  => LICENSE +19 -0
@@ 1,19 @@
Copyright (c) 2019 simpl

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file

A  => README.md +9 -0
@@ 1,9 @@
suki
========
the simple unique krap itemizer

dead simple file tagger, utilizing single-file unoptimized databases for storing
and retrieving filenames by matching tags.

filenames are output to stdout as a list, so suki should be fairly composable
with other UNIX-esque applications.
\ No newline at end of file

A  => example.suki +4 -0
@@ 1,4 @@
coffee:
	1.jpg
	2.png
	3.jpg

A  => src/main.rs +123 -0
@@ 1,123 @@
const VERSION: &'static str = env!("CARGO_PKG_VERSION");

use std::io::Read;

fn main() -> Result<(), String> {
    let args: Vec<String> = std::env::args().collect();
    
    if args.is_empty() {
        print_help();
        return Ok(());
    }

    let a = match args.get(1) {
        Some(s) => String::from(s),
        None => panic!("args but no args? sanity check failed")
    };

    match a.as_ref() {
        "t" | "tag" => {
            match args.get(2) {
                Some(s) => return tag(s, args.split_at(3).1),
                None => return Err(String::from("no filename supplied to tag"))
            };
        },
        "r" | "remove" => println!("remove"),
        "s" | "search" => println!("search"),
        "h" | "help" => {
            print_help();
            return Ok(());
        },
        "v" | "version" => {
            print_version();
            return Ok(());
        }
        _ => println!("bad")
    };

    Ok(())

}

fn tag(filename: &str, tags: &[String]) -> Result<(), String> {
    let dir = curr_dir();
    println!("file: {}, tags: {:?}", filename, tags);
    let mut file = match open_suki(&dir) {
        Ok(f) => f,
        Err(e) => panic!(e)
    };

    let mut buf = String::new();
    file.read_to_string(&mut buf);
    let mut label_list: Vec<&str> = vec!();
    let mut line_no = 0;
    for l in buf.split_terminator('\n') {
        line_no += 1;
        if l.starts_with('\t') {
            continue;
        }
        if l.ends_with(':') {
            label_list.push(l)
        } else {
            return Err(format!("bad syntax at line {} - missing ':' at end of label descriptor", line_no))
        }
    }
    println!("{:?}", label_list);

    Ok(())
}

fn print_version() {
    println!("{} version {} - the simple unique krap itemizer", bin_name(), VERSION);
}

fn print_help() {
    print_version();
    println!("commands:");
    println!("\t<t | tag> [flags] <filename> [tags]      adds file to the specified tags");
    println!("\t<r | remove> [flags] <filename> [tags]   removes the tag(s) from the file specified");
    println!("\t<s | search> [flags] [tags]              searches the tag database for files with the corresponding tag(s)");
    println!("\t<h | help>                               displays this help");
    println!("\t<v | version>                            displays version");
    println!("flags:");
    println!("\t-r                                       recursive search");
}

fn suki_path(path: &str) -> String {
    let mut path = String::from(path);
    path.push_str("/.suki");

    path
}

fn open_suki(path: &str) -> std::io::Result<std::fs::File>{
    let path = suki_path(path);
    std::fs::OpenOptions::new()
        .read(true)
        .append(true)
        .create(true)
        .open(std::path::Path::new(&path))
}

fn bin_name() -> String {
    match std::env::current_exe() {
        Ok(p) => match p.file_stem() {
            Some(s) => match s.to_str() {
                Some(st) => String::from(st),
                None => panic!("Path stem invalid unicode from '{:?}'", s)
            },
            None => panic!("Unable to resolve file stem from '{:?}'", p)
        }
        Err(e) => panic!("{}", e)
    }
}

fn curr_dir() -> String {
    match std::env::current_dir() {
        Ok(p) => match p.to_str() {
            Some(s) => String::from(s),
            None => panic!("unable to resolve dir {:?} to string", p)
        },
        Err(e) => panic!("{}", e)
    }
}
\ No newline at end of file