#python #clipboard #utility #utilities

jabba-lib

一个受Python启发的实用库。

9个版本

0.1.8 2022年8月1日
0.1.7 2022年7月31日

#290 in 编程语言

Download history 4/week @ 2024-03-10 37/week @ 2024-03-31 2/week @ 2024-04-07

每月60次下载
3 crate 中使用

MIT/Apache

56KB
1K SLoC

jabba-lib

一个用于Rust的通用实用库。其API主要受Python编程语言启发。

一些示例

这只是个预告。这个库有更多的模块,并且——在大多数情况下——模块包含的函数比下面显示的还要多。

console

从标准输入读取。

use jabba_lib::jconsole;

fn main() {
    let name = jconsole::input("Name: ");
    let name = name.trim();
    println!("Hello {}!", name);
}

process

调用外部命令。

use jabba_lib::jprocess as jproc;

fn main() {
    let cmd = "ls -al";
    jproc::exec_cmd(cmd);
}

fs

逐行读取文本文件。

use jabba_lib::jfs;
use std::io::BufRead;

fn main() {
    let f = jfs::open("Cargo.toml").unwrap();
    for line in f.lines() {
        let line = line.unwrap();
        println!("{}", line);
    }
}

math

use jabba_lib::jmath;

fn main() {
    assert_eq!(jmath::is_palindrome(101), true);
    assert_eq!(jmath::is_prime(97), true);
    assert_eq!(jmath::get_divisors(28), [1, 2, 4, 7, 14, 28]);
    assert_eq!(jmath::factorial(5), 120);
    assert_eq!(jmath::factorial_bigint(33).to_string(), "8683317618811886495518194401280000000");
}

string

use jabba_lib::jstring;

fn main() {
    let name = "Dave";
    let reversed = jstring::str_rev(name); // evaD
    println!("{} <-> {}", name, reversed);

    let name = "anna";
    println!("{} is palindrome: {}", name, jstring::is_palindrome(name));
}

random

use jabba_lib::jrandom;

fn main() {
    let number = jrandom::randrange(1, 10); // 10 is excluded
    println!("random number from [1, 10): {}", number);

    let number = jrandom::randint(1, 100); // 100 is included
    println!("random number from [1, 100]: {}", number);

    let mut numbers = vec![1, 2, 3, 4, 5];
    jrandom::shuffle(&mut numbers);
    println!("shuffled: {:?}", numbers); // could be [3, 5, 1, 4, 2]
}

clipboard

支持的平台:Linux(带X服务器)、Windows。

在Linux下,您必须安装程序 xsel。您可以使用您的包管理器安装它。

在Linux下,文本将被粘贴到两个剪贴板中(到“primary”和“clipboard”)。

use jabba_lib::jclipboard;

fn main() {
    let text = "hello";

    jabba_lib::jclipboard::check(); // verify if your platform is supported

    jabba_lib::jclipboard::set_text(text).unwrap();
    println!("The text {:?} was pasted on the clipboard", text);

    let read = jabba_lib::jclipboard::get_text().unwrap();
    println!("Contents of the clipboard: {:?}", read);

    assert_eq!(read, text);
}

spell

将数字拼写成文字。

use jabba_lib::jspell;

fn main() {
    assert_eq!(jspell::spell_number(5), "five");
    assert_eq!(jspell::spell_number(11), "eleven");
    assert_eq!(jspell::spell_number(101), "one hundred and one");
    assert_eq!(jspell::spell_number(999), "nine hundred and ninety-nine");
}

time

use jabba_lib::jtime;

fn main() {
    let wait = 1.5;

    println!("Waiting for {:.2} seconds...", wait);
    jtime::sleep(wait);
    println!("Done.");
}

更多信息请查看 examples/ 文件夹。

依赖项

~2–11MB
~126K SLoC