1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-13 15:50:05 +08:00

Compare commits

..

4 Commits

Author SHA1 Message Date
50159a0ca8 chore: uses 2021-01-03 13:12:55 +08:00
aa8b4ed447 style: style 2021-01-03 13:06:39 +08:00
a805d7cb1c feat: add stdout_or_file_write 2021-01-03 13:05:35 +08:00
a8ae90ab4d style: naming 2021-01-03 12:24:45 +08:00
9 changed files with 41 additions and 28 deletions

View File

@@ -1,13 +1,9 @@
#[macro_use]
extern crate lazy_static;
#[macro_use] extern crate lazy_static;
extern crate term;
use std::error::Error;
use std::io::Error as IoError;
use std::io::ErrorKind;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::io::{Error as IoError, ErrorKind};
use std::fmt::{Display, Formatter, Result as FmtResult};
pub mod util_io;
pub mod util_os;

View File

@@ -1,14 +1,13 @@
use std::process;
use clap::{App, Arg, ArgMatches};
use crate::XResult;
use crate::util_msg;
use crate::{XResult, util_msg};
pub type CommandError = XResult<Option<i32>>;
pub trait Command {
fn name(&self) -> &str;
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError;
fn run(&self, arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError;
}
pub trait DefaultCommand {

View File

@@ -1,4 +1,7 @@
use std::{ env, fs::{ self, File }, io::{ Lines, BufReader }, path::{ Path, PathBuf } };
use std::env;
use std::fs::{self, File};
use std::io::{Lines, BufReader};
use std::path::{Path, PathBuf};
use crate::{iff, util_os, util_io, util_msg, new_box_ioerror, XResult};
pub struct JoinFilesReader {

View File

@@ -1,7 +1,5 @@
use std::process::Command;
use crate::XResult;
use crate::util_msg;
use crate::util_cmd::run_command_and_wait;
use crate::{XResult, util_msg, util_cmd};
const LANG: &str = "LANG";
const EN_US: &str = "en_US";
@@ -59,7 +57,7 @@ pub fn git_push(working_dir: Option<&str>) {
let mut cmd = new_git_command(working_dir);
cmd.arg("push");
util_msg::print_info(&format!("Exec: {:?}", cmd));
if let Err(e) = run_command_and_wait(&mut cmd) {
if let Err(e) = util_cmd::run_command_and_wait(&mut cmd) {
util_msg::print_error(&format!("Run git push failed: {}", e));
}
}
@@ -71,7 +69,7 @@ pub fn git_add(working_dir: Option<&str>, files: &[String]) {
cmd.arg(&f);
}
util_msg::print_info(&format!("Exec: {:?}", cmd));
if let Err(e) = run_command_and_wait(&mut cmd) {
if let Err(e) = util_cmd::run_command_and_wait(&mut cmd) {
util_msg::print_error(&format!("Run git add failed: {}", e));
}
}
@@ -85,7 +83,7 @@ pub fn git_commit(working_dir: Option<&str>, message: &str, files: &[String]) {
cmd.arg(&f);
}
util_msg::print_info(&format!("Exec: {:?}", cmd));
if let Err(e) = run_command_and_wait(&mut cmd) {
if let Err(e) = util_cmd::run_command_and_wait(&mut cmd) {
util_msg::print_error(&format!("Run git commit failed: {}", e));
}
}

View File

@@ -1,13 +1,30 @@
use std::{ fs::File, io::{ self, ErrorKind, prelude::* } };
use std::io::{self, Write, ErrorKind, prelude::*};
use std::fs::File;
use std::time::{SystemTime, Duration};
use crate::{ XResult, new_box_ioerror };
use crate::{SimpleError, XResult};
use crate::util_size;
use crate::util_msg;
use crate::util_file;
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
pub fn stdout_or_file_write(file: Option<&str>, overwrite: bool) -> XResult<Box<dyn Write>> {
match file {
None => Ok(Box::new(io::stdout())),
Some(output) => {
if let Ok(_) = File::open(output) {
if !overwrite {
return Err(SimpleError::new(format!("File exists: {}", output)).into());
}
}
Ok(Box::new(File::create(output).map_err(|e| {
SimpleError::new(format!("Create file: {}, failed: {}", output, e))
})?))
},
}
}
pub struct PrintStatusContext {
pub print_interval_time: Duration,
pub print_interval_bytes: i64,
@@ -69,7 +86,7 @@ pub fn get_read_stdin_or_file(file: &str) -> XResult<Box<dyn Read>> {
} else {
match File::open(&util_file::resolve_file_path(file)) {
Ok(f) => Ok(Box::new(f)),
Err(err) => Err(new_box_ioerror(&format!("Open file {}, erorr: {}", file, err))),
Err(err) => Err(SimpleError::new(format!("Open file {}, erorr: {}", file, err)).into()),
}
}
}

View File

@@ -1,4 +1,6 @@
use std::{ env, io::{ self, Write }, sync::{ Arc, Mutex } };
use std::env;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
lazy_static! {
pub static ref IS_ATTY: bool = is_atty();

View File

@@ -1,5 +1,4 @@
use std::io;
use std::io::Write;
use std::io::{self, Write};
pub const RED: &str = "\x1B[91m";
pub const GREEN: &str = "\x1B[92m";

View File

@@ -1,5 +1,4 @@
use std::time::SystemTime;
use std::time::Duration;
use std::time::{SystemTime, Duration};
pub fn get_current_secs() -> u64 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0 /* SHOULD NOT HAPPEN */ )