lib.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2015, Alberto Corona <alberto@0x1a.us>
  2. // All rights reserved. This file is part of core-utils, distributed under the
  3. // GPL v3 license. For full terms please see the LICENSE file.
  4. #![crate_type = "lib"]
  5. #![feature(path_relative_from)]
  6. extern crate term;
  7. use std::io::prelude::Write;
  8. use std::process;
  9. use std::path::{PathBuf};
  10. pub enum Status {
  11. Ok,
  12. Error,
  13. OptError,
  14. }
  15. pub fn exit(status: Status) {
  16. process::exit(status as i32);
  17. }
  18. pub fn to_rel(path: &PathBuf, rel_from: &PathBuf) -> PathBuf {
  19. match path.relative_from(&rel_from) {
  20. Some(s) => { return s.to_path_buf() },
  21. None => { panic!() },
  22. };
  23. }
  24. pub fn path_err(status: Status, mesg: String, item: PathBuf) {
  25. match term::stdout() {
  26. Some(mut term) => {
  27. term.fg(term::color::RED).unwrap();
  28. (write!(term, "{}: {}\n",item.display(), mesg)).unwrap();
  29. exit(status);
  30. }
  31. None => {},
  32. };
  33. }
  34. pub fn err(prog: &str, status: Status, mesg: String) {
  35. match term::stdout() {
  36. Some(mut term) => {
  37. term.fg(term::color::RED).unwrap();
  38. (write!(term, "{}: {}\n", prog, mesg)).unwrap();
  39. exit(status);
  40. }
  41. None => {},
  42. };
  43. }
  44. pub fn join_vects(first: Vec<String>, second: Vec<String>) -> Vec<String> {
  45. let mut collection: Vec<String> = first;
  46. for i in second.iter() {
  47. collection.push(i.clone());
  48. }
  49. return collection;
  50. }
  51. pub fn copyright(prog: &str, vers: &str, yr: &str, auth: Vec<&str>) {
  52. print!("{} (core-utils) {}\n\
  53. Copyright (C) {} core-utils developers\n\
  54. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
  55. This is free software: you are free to change and redistribute it.\n\
  56. There is NO WARRANTY, to the extent permitted by law.\n\n", prog, vers, yr);
  57. print!("Written by ");
  58. for pers in auth.iter() {
  59. print!("{} ", pers);
  60. }
  61. print!("\n");
  62. }