|
| 1 | +use log::{Level, LevelFilter}; |
| 2 | +use owo_colors::{ |
| 3 | + OwoColorize, |
| 4 | + Stream::{Stderr, Stdout}, |
| 5 | +}; |
| 6 | +use std::env; |
| 7 | +use std::process::Command; |
| 8 | + |
| 9 | +use crate::config::{Config, ConfigFile}; |
| 10 | +use crate::platform::Platform; |
| 11 | + |
| 12 | +pub struct Application { |
| 13 | + pub(crate) path: String, |
| 14 | + pub(crate) config_file: ConfigFile, |
| 15 | + pub(crate) config: Config, |
| 16 | + pub(crate) platform: Platform, |
| 17 | + verbosity: LevelFilter, |
| 18 | +} |
| 19 | + |
| 20 | +impl Application { |
| 21 | + pub fn new(verbosity: LevelFilter) -> Application { |
| 22 | + let platform = Platform::new(); |
| 23 | + let config_file = ConfigFile::new(); |
| 24 | + let config_model = config_file.load(); |
| 25 | + |
| 26 | + // Set the path to the repository for the entire application |
| 27 | + let path = if !config_model.repo.is_empty() { |
| 28 | + config_model.repo.to_string() |
| 29 | + } else { |
| 30 | + match env::current_dir() { |
| 31 | + Ok(p) => p.display().to_string(), |
| 32 | + Err(_) => ".".to_string(), |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + Application { |
| 37 | + path: path.to_string(), |
| 38 | + config_file: config_file, |
| 39 | + config: config_model, |
| 40 | + platform: platform, |
| 41 | + verbosity: verbosity, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn exit(&self, code: i32) { |
| 46 | + std::process::exit(code); |
| 47 | + } |
| 48 | + |
| 49 | + pub fn abort<T: AsRef<str>>(&self, text: T) { |
| 50 | + self.display_error(text); |
| 51 | + self.exit(1); |
| 52 | + } |
| 53 | + |
| 54 | + pub fn command<T: AsRef<str>>(&self, program: T) -> Command { |
| 55 | + let mut cmd = Command::new(program.as_ref()); |
| 56 | + cmd.current_dir(self.path.clone()); |
| 57 | + cmd |
| 58 | + } |
| 59 | + |
| 60 | + pub fn display<T: AsRef<str>>(&self, text: T) { |
| 61 | + // Simply bold rather than bright white for terminals with white backgrounds |
| 62 | + println!( |
| 63 | + "{}", |
| 64 | + text.as_ref().if_supports_color(Stdout, |text| text.bold()) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + #[allow(dead_code)] |
| 69 | + pub fn display_trace<T: AsRef<str>>(&self, text: T) { |
| 70 | + if Level::Trace <= self.verbosity { |
| 71 | + eprintln!( |
| 72 | + "{}", |
| 73 | + text.as_ref().if_supports_color(Stderr, |text| text.bold()) |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + #[allow(dead_code)] |
| 79 | + pub fn display_debug<T: AsRef<str>>(&self, text: T) { |
| 80 | + if Level::Debug <= self.verbosity { |
| 81 | + eprintln!( |
| 82 | + "{}", |
| 83 | + text.as_ref().if_supports_color(Stderr, |text| text.bold()) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[allow(dead_code)] |
| 89 | + pub fn display_info<T: AsRef<str>>(&self, text: T) { |
| 90 | + if Level::Info <= self.verbosity { |
| 91 | + eprintln!( |
| 92 | + "{}", |
| 93 | + text.as_ref().if_supports_color(Stderr, |text| text.bold()) |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #[allow(dead_code)] |
| 99 | + pub fn display_success<T: AsRef<str>>(&self, text: T) { |
| 100 | + if Level::Info <= self.verbosity { |
| 101 | + eprintln!( |
| 102 | + "{}", |
| 103 | + text.as_ref() |
| 104 | + .if_supports_color(Stderr, |text| text.bright_cyan()) |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + #[allow(dead_code)] |
| 110 | + pub fn display_waiting<T: AsRef<str>>(&self, text: T) { |
| 111 | + if Level::Info <= self.verbosity { |
| 112 | + eprintln!( |
| 113 | + "{}", |
| 114 | + text.as_ref() |
| 115 | + .if_supports_color(Stderr, |text| text.bright_magenta()) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + #[allow(dead_code)] |
| 121 | + pub fn display_warning<T: AsRef<str>>(&self, text: T) { |
| 122 | + if Level::Warn <= self.verbosity { |
| 123 | + eprintln!( |
| 124 | + "{}", |
| 125 | + text.as_ref() |
| 126 | + .if_supports_color(Stderr, |text| text.bright_yellow()) |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + pub fn display_error<T: AsRef<str>>(&self, text: T) { |
| 132 | + if Level::Error <= self.verbosity { |
| 133 | + eprintln!( |
| 134 | + "{}", |
| 135 | + text.as_ref() |
| 136 | + .if_supports_color(Stderr, |text| text.bright_red()) |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments