Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 3, 2024
1 parent 07bb89b commit 47acb5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 59 deletions.
65 changes: 30 additions & 35 deletions tooling/cli/src/helpers/web_dev_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use axum::{
http::{header::CONTENT_TYPE, StatusCode, Uri},
response::IntoResponse,
routing::get,
Router, Server,
serve, Router,
};
use html5ever::{namespace_url, ns, LocalName, QualName};
use kuchiki::{traits::TendrilSink, NodeRef};
Expand Down Expand Up @@ -73,48 +73,43 @@ pub fn start<P: AsRef<Path>>(path: P, ip: IpAddr, port: Option<u16>) -> crate::R
1430
});

let (server, server_url) = loop {
let (listener, server_url) = loop {
let server_url = SocketAddr::new(ip, port);
let server = Server::try_bind(&server_url);

if !auto_port {
break (server, server_url);
if let Ok(listener) = tokio::net::TcpListener::bind(server_url).await {
break (Some(listener), server_url);
}

if server.is_ok() {
break (server, server_url);
if !auto_port {
break (None, server_url);
}

port += 1;
};

let state = Arc::new(State {
serve_dir,
tx,
address: server_url,
});
let state_ = state.clone();
let router = Router::new()
.fallback(move |uri| handler(uri, state_))
.route(
"/__tauri_cli",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
}),
);

match server {
Ok(server) => {
server_url_tx.send(Ok(server_url)).unwrap();
server.serve(router.into_make_service()).await.unwrap();
}
Err(e) => {
server_url_tx
.send(Err(anyhow::anyhow!(
"failed to start development server on {server_url}: {e}"
)))
.unwrap();
}
if let Some(listener) = listener {
let state = Arc::new(State {
serve_dir,
tx,
address: server_url,
});
let state_ = state.clone();
let router = Router::new()
.fallback(move |uri| handler(uri, state_))
.route(
"/__tauri_cli",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
}),
);

server_url_tx.send(Ok(server_url)).unwrap();
serve(listener, router).await.unwrap();
} else {
server_url_tx
.send(Err(anyhow::anyhow!(
"failed to start development server on {server_url}"
)))
.unwrap();
}
})
});
Expand Down
19 changes: 7 additions & 12 deletions tooling/cli/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use image::{
imageops::FilterType,
open, ColorType, DynamicImage, ImageBuffer, ImageEncoder, Rgba,
};
use resvg::usvg::{fontdb, TreeParsing, TreeTextToPath};
use resvg::{tiny_skia, usvg};
use serde::Deserialize;

Expand Down Expand Up @@ -61,21 +60,21 @@ pub struct Options {
}

enum Source {
Svg(resvg::Tree),
Svg(resvg::usvg::Tree),
DynamicImage(DynamicImage),
}

impl Source {
fn width(&self) -> u32 {
match self {
Self::Svg(svg) => svg.size.width() as u32,
Self::Svg(svg) => svg.size().width() as u32,
Self::DynamicImage(i) => i.width(),
}
}

fn height(&self) -> u32 {
match self {
Self::Svg(svg) => svg.size.height() as u32,
Self::Svg(svg) => svg.size().height() as u32,
Self::DynamicImage(i) => i.height(),
}
}
Expand All @@ -84,8 +83,9 @@ impl Source {
match self {
Self::Svg(svg) => {
let mut pixmap = tiny_skia::Pixmap::new(size, size).unwrap();
let scale = size as f32 / svg.size.height();
svg.render(
let scale = size as f32 / svg.size().height();
resvg::render(
&svg,
tiny_skia::Transform::from_scale(scale, scale),
&mut pixmap.as_mut(),
);
Expand Down Expand Up @@ -125,13 +125,8 @@ pub fn command(options: Options) -> Result<()> {
..Default::default()
};

let mut fontdb = fontdb::Database::new();
fontdb.load_system_fonts();

let svg_data = std::fs::read(&input).unwrap();
let mut tree = usvg::Tree::from_data(&svg_data, &opt).unwrap();
tree.convert_text(&fontdb);
resvg::Tree::from_usvg(&tree)
usvg::Tree::from_data(&svg_data, &opt, &Default::default()).unwrap()
};

Source::Svg(rtree)
Expand Down
20 changes: 8 additions & 12 deletions tooling/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod plugin;
mod signer;

use clap::{ArgAction, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum};
use env_logger::fmt::Color;
use env_logger::fmt::style::{AnsiColor, Style};
use env_logger::Builder;
use log::{debug, log_enabled, Level};
use serde::Deserialize;
Expand Down Expand Up @@ -209,27 +209,23 @@ where
let action = action.to_cow_str().unwrap();
is_command_output = action == "stdout" || action == "stderr";
if !is_command_output {
let mut action_style = f.style();
action_style.set_color(Color::Green).set_bold(true);
let style = Style::new().fg_color(Some(AnsiColor::Green.into())).bold();

write!(f, "{:>12} ", action_style.value(action))?;
write!(f, " {style}{}{style:#} ", action)?;
}
} else {
let mut level_style = f.default_level_style(record.level());
level_style.set_bold(true);

let style = f.default_level_style(record.level()).bold();
write!(
f,
"{:>12} ",
level_style.value(prettyprint_level(record.level()))
" {style}{}{style:#} ",
prettyprint_level(record.level())
)?;
}

if !is_command_output && log_enabled!(Level::Debug) {
let mut target_style = f.style();
target_style.set_color(Color::Black);
let style = Style::new().fg_color(Some(AnsiColor::Black.into()));

write!(f, "[{}] ", target_style.value(record.target()))?;
write!(f, "[{style}{}{style:#}] ", record.target())?;
}

writeln!(f, "{}", record.args())
Expand Down

0 comments on commit 47acb5e

Please sign in to comment.