output.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. use crate::server::Diagnostic;
  2. use colored::Colorize;
  3. use dioxus_cli_config::{crate_root, CrateConfig};
  4. use std::{path::PathBuf, process::Command};
  5. #[derive(Debug, Default)]
  6. pub struct PrettierOptions {
  7. pub changed: Vec<PathBuf>,
  8. pub warnings: Vec<Diagnostic>,
  9. pub elapsed_time: u128,
  10. }
  11. #[derive(Debug, Clone)]
  12. pub struct WebServerInfo {
  13. pub ip: String,
  14. pub port: u16,
  15. }
  16. pub fn print_console_info(
  17. config: &CrateConfig,
  18. options: PrettierOptions,
  19. web_info: Option<WebServerInfo>,
  20. ) {
  21. // Don't clear the screen if the user has set the DIOXUS_LOG environment variable to "trace" so that we can see the logs
  22. if Some("trace") != std::env::var("DIOXUS_LOG").ok().as_deref() {
  23. if let Ok(native_clearseq) = Command::new(if cfg!(target_os = "windows") {
  24. "cls"
  25. } else {
  26. "clear"
  27. })
  28. .output()
  29. {
  30. print!("{}", String::from_utf8_lossy(&native_clearseq.stdout));
  31. } else {
  32. // Try ANSI-Escape characters
  33. print!("\x1b[2J\x1b[H");
  34. }
  35. }
  36. let mut profile = if config.release { "Release" } else { "Debug" }.to_string();
  37. if config.custom_profile.is_some() {
  38. profile = config.custom_profile.as_ref().unwrap().to_string();
  39. }
  40. let hot_reload = if config.hot_reload { "RSX" } else { "Normal" };
  41. let crate_root = crate_root().unwrap();
  42. let custom_html_file = if crate_root.join("index.html").is_file() {
  43. "Custom [index.html]"
  44. } else {
  45. "None"
  46. };
  47. let url_rewrite = if config.dioxus_config.web.watcher.index_on_404 {
  48. "True"
  49. } else {
  50. "False"
  51. };
  52. let proxies = &config.dioxus_config.web.proxy;
  53. if options.changed.is_empty() {
  54. println!(
  55. "{} @ v{} [{}]",
  56. "Dioxus".bold().green(),
  57. clap::crate_version!(),
  58. chrono::Local::now().format("%H:%M:%S").to_string().dimmed()
  59. );
  60. } else {
  61. println!(
  62. "Project Reloaded: {}\n",
  63. format!(
  64. "Changed {} files. [{}]",
  65. options.changed.len(),
  66. chrono::Local::now().format("%H:%M:%S").to_string().dimmed()
  67. )
  68. .purple()
  69. .bold()
  70. );
  71. }
  72. if let Some(WebServerInfo { ip, port }) = web_info {
  73. if config.dioxus_config.web.https.enabled == Some(true) {
  74. println!(
  75. " > Local address: {}",
  76. format!("https://localhost:{}/", port).blue()
  77. );
  78. println!(
  79. " > Network address: {}",
  80. format!("https://{}:{}/", ip, port).blue()
  81. );
  82. println!(" > HTTPS: {}", "Enabled".to_string().green());
  83. } else {
  84. println!(
  85. " > Local address: {}",
  86. format!("http://localhost:{}/", port).blue()
  87. );
  88. println!(
  89. " > Network address: {}",
  90. format!("http://{}:{}/", ip, port).blue()
  91. );
  92. println!(" > HTTPS status: {}", "Disabled".to_string().red());
  93. }
  94. }
  95. println!();
  96. println!(" > Hot Reload Mode: {}", hot_reload.cyan());
  97. println!(
  98. " > Watching: [ {} ]",
  99. config
  100. .dioxus_config
  101. .web
  102. .watcher
  103. .watch_path
  104. .iter()
  105. .cloned()
  106. .chain(["Cargo.toml", "Dioxus.toml"].iter().map(PathBuf::from))
  107. .map(|f| f.display().to_string())
  108. .collect::<Vec<String>>()
  109. .join(", ")
  110. .cyan()
  111. );
  112. if !proxies.is_empty() {
  113. println!(" > Proxies :");
  114. for proxy in proxies {
  115. println!(" - {}", proxy.backend.blue());
  116. }
  117. }
  118. println!(" > Custom index.html: {}", custom_html_file.green());
  119. println!(" > Serve index.html on 404: {}", url_rewrite.purple());
  120. println!();
  121. println!(
  122. " > Build Features: [ {} ]",
  123. config
  124. .features
  125. .clone()
  126. .unwrap_or_default()
  127. .join(", ")
  128. .green()
  129. );
  130. println!(" > Build Profile: {}", profile.green());
  131. println!(
  132. " > Build took: {} millis",
  133. options.elapsed_time.to_string().green().bold()
  134. );
  135. println!();
  136. if options.warnings.is_empty() {
  137. log::info!("{}\n", "A perfect compilation!".green().bold());
  138. } else {
  139. log::warn!(
  140. "{}",
  141. format!(
  142. "There were {} warning messages during the build. Run `cargo check` to see them.",
  143. options.warnings.len() - 1
  144. )
  145. .yellow()
  146. .bold()
  147. );
  148. }
  149. }