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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#![deny(rust_2018_idioms)]
#![allow(clippy::single_component_path_imports)]
#![allow(clippy::type_complexity)]
#![cfg_attr(docsrs, feature(kcm))]
#![recursion_limit = "1024"]
//! [![](https://gitlab.com/andrew_ryan/doe/-/raw/master/doe.svg)](https://docs.rs/doe)

#![doc(
    html_logo_url = "https://gitlab.com/andrew_ryan/doe/-/raw/master/html_logo.svg",
    html_favicon_url = "https://gitlab.com/andrew_ryan/doe/-/raw/master/html_favicon.svg"
)]

//!
//! This crate provides various utility functions and modules for performing common tasks in Rust applications. It aims to simplify clipboard operations, file system manipulation, keyboard and mouse input simulation, timer management, and Excel file handling.
//!
//!# Features
//!### ctf
//!```sh
//!cargo add doe -F ctf
//!```
//!allow you to perform various operations with ease, such as string manipulation, urlencode urldecode,hex encode decode,base43 encode decode, and error management.
//!### clip
//!```sh
//!cargo add doe -F clip
//!```
//!simplifies clipboard management, enabling you to effortlessly interact with the system clipboard.
//!### mouse
//!```sh
//!cargo add doe -F mouse
//!```
//!offers intuitive and easy-to-use functions for simulating mouse actions.
//!### keyboard
//!```sh
//!cargo add doe -F keyboard
//!```
//!offers simulating keyboard input action with easy-to-use functions.
//!### kcm
//!kcm is short feature for keyboard clipboard mouse together
//!```sh
//!cargo add doe -F kcm
//!```
//!### xlsx
//!read edit write xlsx file
//!```sh
//!cargo add doe -F xlsx
//!```
//!### docx
//!read edit docx file
//!```sh
//!cargo add doe -F docx
//!```
//!### date
//!get and read edit date string
//!```sh
//!cargo add doe -F date
//!```

/// clipboard module provides functions and structs related to clipboard operations. It allows the user to copy and paste data between the application and the clipboard.
pub mod clipboard;
/// color module contains functions for converting grb format to hex and hex to grb format
pub mod color;
///const data
pub mod consts;
/// ctf module provides utility functions like `permutations`,`hex_encode`,`hex_decode`,`base64_encode`,`base64_decode`,`php_urlencode`,`php_urldecode`.
pub mod ctf;
/// date module contains functions for get now,get_recent_seven_days,normal_date_to_excel_date,excel_date_to_normal_date
pub mod date;
///xlsx module contains functions for reading editing docx file
pub mod docx;
/// The file system (fs) module provides functions and structs for performing operations on the file system. It includes functions for reading, writing, creating, and deleting files and directories.
pub mod fs;
///keyboard module contains functions and structs related to keyboard input. It allows the user to simulate keyboard input, capture keystrokes, and perform other keyboard-related operations.
pub mod keyboard;
///macros module contains all utility macros for development
pub mod macros;
/// mouse module contains functions and structs related to mouse input. It allows the user to simulate mouse input, capture mouse movements and clicks, and perform other mouse-related operations.
pub mod mouse;
/// get all process (name,pid) and kill process by name or pid
pub mod process;
/// A Linear Congruential Generator (LCG) is a simple type of random number generator that produces a sequence of pseudo-random numbers based on a linear recurrence relation.
pub mod rand;
///structs module contains definitions of custom data structures like `Bfn`, `Bts`, `waker_fn`.
pub mod structs;
/// timer module provides functions and structs for creating and managing timers. It allows the user to schedule tasks to be executed at a specific time or interval.
pub mod timer;
///traits module contains trait definitions that define shared behavior and functions
pub mod traits;
///xlsx module contains functions and structs for reading, writing, and manipulating Excel files in the XLSX format.
pub mod xlsx;
///zoml module contains functions for converting data between the ZOML format and CSV format.
pub mod zoml;
///screenshot module contains functions for screenshot and get hex from screenshot image
pub mod screenshot;
///screenshot module contains functions resize add paddings,convert image format
pub mod images;

pub use consts::consts::*;
pub use fs::fs::*;
pub use macros::macros::*;
pub use rand::rand::*;
pub use structs::structs::*;
pub use timer::impl_timer::*;
pub use traits::traits::*;
pub use zoml::zoml::*;

use std::sync::Arc;

pub type DynError = Result<(), Box<dyn std::error::Error>>;
///Rust is a wrapper for the standard library's std::process::Command function, which is used to execute external commands
///```rust
///     doe::system("ls");
///     doe::system("ls -a");
/// ```
pub fn system(command: impl ToString) -> std::io::Result<()> {
    use std::process::Command;
    if cfg!(target_os = "windows") {
        if crate::has_powershell!() {
            Command::new("powershell")
                .arg("-Command")
                .arg(&command.to_string())
                .status()?;
        } else {
            Command::new("cmd")
                .arg("/c")
                .arg(&command.to_string())
                .status()?;
        }
    } else {
        Command::new("sh")
            .arg("-c")
            .arg(&command.to_string())
            .status()?;
    }
    Ok(())
}

pub fn lowercase_letters() -> Arc<str> {
    Arc::from("abcdefghijklmnopqrstuvwxyz")
}
pub fn uppercase_letters() -> Arc<str> {
    Arc::from("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
}

pub fn digits() -> Arc<str> {
    Arc::from("0123456789")
}

pub fn letters_and_digits() -> Arc<str> {
    Arc::from(format!("{}{}{}", lowercase_letters(), uppercase_letters(), digits()).as_ref())
}

///The Vec sort all elements can be repeated to define the longest shortest size
/// ```ignore
///fn main() {
///     use doe::DebugPrint;
///     use doe::utils::generate_all_possible_vec;
///    let v  = generate_all_possible_vec(&vec![1,2,3],1,2);
///    v.dprintln();//[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
///}
///```
pub fn generate_all_possible_vec<T: Clone>(
    elements: &[T],
    min_len: usize,
    max_len: usize,
) -> Vec<Vec<T>> {
    let mut result = Vec::new();
    for len in min_len..=max_len {
        let mut indices = vec![0; len];
        let mut done = false;
        while !done {
            let permutation = indices.iter().map(|&i| elements[i].clone()).collect();
            result.push(permutation);
            done = true;
            for i in (0..len).rev() {
                indices[i] += 1;
                if indices[i] == elements.len() {
                    indices[i] = 0;
                } else {
                    done = false;
                    break;
                }
            }
        }
    }
    result
}
///permutations
/// ```ignore
///fn main() {
///    use doe::utils::permutations;
///    use doe::DebugPrint;
///    permutations("abc").dprintln();//["abc", "acb", "bac", "bca", "cba", "cab"]
///}
/// ```
pub fn permutations(alphabet: &str) -> Vec<String> {
    fn permute(chars: &mut [char], start: usize, result: &mut Vec<String>) {
        if start == chars.len() {
            result.push(chars.iter().collect());
        } else {
            for i in start..chars.len() {
                chars.swap(start, i);
                permute(chars, start + 1, result);
                chars.swap(start, i);
            }
        }
    }

    let mut result = Vec::new();
    let mut chars: Vec<char> = alphabet.chars().collect();
    permute(&mut chars, 0, &mut result);
    result
}