feat: console only in debug, update config parsing
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
# bropicker config
|
||||||
|
# Расположение: %APPDATA%\bropicker\config.toml
|
||||||
|
# Открывается из пикера кнопкой шестерёнки (subl/notepad)
|
||||||
|
|
||||||
|
# [[browsers]] — один блок на запись пикера.
|
||||||
|
# path — полный путь к exe (или имя из PATH)
|
||||||
|
# flags — аргументы запуска; пробелы внутри значения — в кавычках
|
||||||
|
# icon — необязательно: путь к png/svg, иначе подбирается по имени
|
||||||
|
|
||||||
|
[[browsers]]
|
||||||
|
name = "Firefox"
|
||||||
|
path = 'C:\Program Files\Mozilla Firefox\firefox.exe'
|
||||||
|
flags = ""
|
||||||
|
|
||||||
|
# Профили Chrome: имена каталогов профилей смотри на chrome://version
|
||||||
|
# (Profile Path: ...\User Data\Default → "Default",
|
||||||
|
# ...\User Data\Profile 1 → "Profile 1")
|
||||||
|
|
||||||
|
[[browsers]]
|
||||||
|
name = "Chrome — Work"
|
||||||
|
path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
|
||||||
|
flags = '--profile-directory="Default"'
|
||||||
|
|
||||||
|
[[browsers]]
|
||||||
|
name = "Chrome — Personal"
|
||||||
|
path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
|
||||||
|
flags = '--profile-directory="Profile 1"'
|
||||||
|
|
||||||
|
[[browsers]]
|
||||||
|
name = "Firefox — Work"
|
||||||
|
path = 'C:\Program Files\Mozilla Firefox\firefox.exe'
|
||||||
|
flags = '-P "Work"'
|
||||||
|
|
||||||
|
# Запомненные домены: домен = имя записи из [[browsers]]
|
||||||
|
[remembered]
|
||||||
|
"github.com" = "Chrome — Work"
|
||||||
|
|
||||||
|
[settings]
|
||||||
|
remember_choice = true
|
||||||
|
always_ask = true
|
||||||
@@ -149,6 +149,13 @@ HTML-макеты → PNG: **headless Edge** (`--headless --screenshot`), он
|
|||||||
cwd = system32 и иконки бы отвалились); проверено запуском из
|
cwd = system32 и иконки бы отвалились); проверено запуском из
|
||||||
LOCALAPPDATA с cwd=System32
|
LOCALAPPDATA с cwd=System32
|
||||||
- [x] Release-профиль: lto + codegen-units=1 + strip
|
- [x] Release-профиль: lto + codegen-units=1 + strip
|
||||||
|
- [x] Release без консоли: `windows_subsystem="windows"` только для не-debug
|
||||||
|
(в debug консоль остаётся); весь вывод → `%APPDATA%\bropicker\bropicker.log`
|
||||||
|
(config::log с unix-timestamp)
|
||||||
|
- [x] Парсер флагов понимает кавычки (`split_flags`) — профили браузеров:
|
||||||
|
`flags = '--profile-directory="Profile 1"'` (Chrome), `-P "Work"` (Firefox);
|
||||||
|
пример: docs/config.example.toml
|
||||||
|
- [x] Cold start release: 131 ms (после добавления файлового лога)
|
||||||
|
|
||||||
## Этап F — релиз
|
## Этап F — релиз
|
||||||
|
|
||||||
|
|||||||
+25
-6
@@ -1,8 +1,27 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub fn log(msg: &str) {
|
||||||
|
use std::io::Write;
|
||||||
|
let Ok(appdata) = std::env::var("APPDATA") else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let dir = Path::new(&appdata).join("bropicker");
|
||||||
|
if std::fs::create_dir_all(&dir).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let path = dir.join("bropicker.log");
|
||||||
|
let ts = std::time::SystemTime::now()
|
||||||
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
|
.map(|d| d.as_secs())
|
||||||
|
.unwrap_or(0);
|
||||||
|
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(&path) {
|
||||||
|
let _ = writeln!(f, "[{ts}] {msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub struct BrowserEntry {
|
pub struct BrowserEntry {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -57,7 +76,7 @@ pub fn load() -> Config {
|
|||||||
let path = config_path();
|
let path = config_path();
|
||||||
match std::fs::read_to_string(&path) {
|
match std::fs::read_to_string(&path) {
|
||||||
Ok(s) => toml::from_str(&s).unwrap_or_else(|e| {
|
Ok(s) => toml::from_str(&s).unwrap_or_else(|e| {
|
||||||
eprintln!("[bp] config parse error ({path:?}): {e}");
|
crate::config::log(&format!("[bp] config parse error ({path:?}): {e}"));
|
||||||
Config::default()
|
Config::default()
|
||||||
}),
|
}),
|
||||||
Err(_) => Config::default(),
|
Err(_) => Config::default(),
|
||||||
@@ -72,17 +91,17 @@ pub fn save(cfg: &Config) {
|
|||||||
match toml::to_string_pretty(cfg) {
|
match toml::to_string_pretty(cfg) {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
if let Err(e) = std::fs::write(&path, s) {
|
if let Err(e) = std::fs::write(&path, s) {
|
||||||
eprintln!("[bp] config save failed ({path:?}): {e}");
|
log(&format!("[bp] config save failed ({path:?}): {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!("[bp] config serialize failed: {e}"),
|
Err(e) => log(&format!("[bp] config serialize failed: {e}")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensure_browsers(cfg: &mut Config) {
|
pub fn ensure_browsers(cfg: &mut Config) {
|
||||||
if cfg.browsers.is_empty() {
|
if cfg.browsers.is_empty() {
|
||||||
cfg.browsers = detect_browsers();
|
cfg.browsers = detect_browsers();
|
||||||
eprintln!("[bp] autodetected {} browsers", cfg.browsers.len());
|
log(&format!("[bp] autodetected {} browsers", cfg.browsers.len()));
|
||||||
if !cfg.browsers.is_empty() {
|
if !cfg.browsers.is_empty() {
|
||||||
save(cfg);
|
save(cfg);
|
||||||
}
|
}
|
||||||
@@ -163,7 +182,7 @@ pub fn icon_for(name: &str) -> &'static str {
|
|||||||
"logos/opera_48x48.png"
|
"logos/opera_48x48.png"
|
||||||
} else if n.contains("brave") {
|
} else if n.contains("brave") {
|
||||||
"logos/brave_48x48.png"
|
"logos/brave_48x48.png"
|
||||||
} else if n.contains("яндекс") || n.contains("yandex") {
|
} else if n.contains("яндекс") || n.contains("yandex") {
|
||||||
"logos/yandex_48x48.png"
|
"logos/yandex_48x48.png"
|
||||||
} else {
|
} else {
|
||||||
"icons/globe.svg"
|
"icons/globe.svg"
|
||||||
|
|||||||
+42
-15
@@ -1,3 +1,5 @@
|
|||||||
|
#![cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")]
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -11,6 +13,27 @@ use config::{BrowserEntry, Config};
|
|||||||
|
|
||||||
slint::include_modules!();
|
slint::include_modules!();
|
||||||
|
|
||||||
|
fn split_flags(flags: &str) -> Vec<String> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
let mut cur = String::new();
|
||||||
|
let mut in_quotes = false;
|
||||||
|
for c in flags.chars() {
|
||||||
|
match c {
|
||||||
|
'"' => in_quotes = !in_quotes,
|
||||||
|
' ' if !in_quotes => {
|
||||||
|
if !cur.is_empty() {
|
||||||
|
out.push(std::mem::take(&mut cur));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => cur.push(c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !cur.is_empty() {
|
||||||
|
out.push(cur);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
fn load_icon(rel: &str) -> slint::Image {
|
fn load_icon(rel: &str) -> slint::Image {
|
||||||
if let Ok(exe) = std::env::current_exe() {
|
if let Ok(exe) = std::env::current_exe() {
|
||||||
if let Some(dir) = exe.parent() {
|
if let Some(dir) = exe.parent() {
|
||||||
@@ -41,7 +64,7 @@ fn to_ui_config(entry: &BrowserEntry) -> BrowserConfig {
|
|||||||
fn launch(entry: &BrowserEntry, url: &str) -> std::io::Result<()> {
|
fn launch(entry: &BrowserEntry, url: &str) -> std::io::Result<()> {
|
||||||
let mut cmd = std::process::Command::new(&entry.path);
|
let mut cmd = std::process::Command::new(&entry.path);
|
||||||
if !entry.flags.is_empty() {
|
if !entry.flags.is_empty() {
|
||||||
cmd.args(entry.flags.split_whitespace());
|
cmd.args(split_flags(&entry.flags));
|
||||||
}
|
}
|
||||||
cmd.arg(url).spawn().map(|_| ())
|
cmd.arg(url).spawn().map(|_| ())
|
||||||
}
|
}
|
||||||
@@ -114,17 +137,18 @@ fn init(cfg: Config, url: Option<String>) -> State {
|
|||||||
let entry = browser_model.row_data(index).expect("row exists");
|
let entry = browser_model.row_data(index).expect("row exists");
|
||||||
let url = main_window.get_current_url().to_string();
|
let url = main_window.get_current_url().to_string();
|
||||||
|
|
||||||
println!(
|
|
||||||
"Launching [{}]: {:?} {:?} {}",
|
|
||||||
index, entry.path, entry.flags, url
|
|
||||||
);
|
|
||||||
|
|
||||||
let entry_ref = BrowserEntry {
|
let entry_ref = BrowserEntry {
|
||||||
name: entry.name.to_string(),
|
name: entry.name.to_string(),
|
||||||
path: entry.path.to_string(),
|
path: entry.path.to_string(),
|
||||||
flags: entry.flags.to_string(),
|
flags: entry.flags.to_string(),
|
||||||
icon: String::new(),
|
icon: String::new(),
|
||||||
};
|
};
|
||||||
|
let url = main_window.get_current_url().to_string();
|
||||||
|
|
||||||
|
config::log(&format!(
|
||||||
|
"[bp] launching [{}]: {:?} {:?} {}",
|
||||||
|
index, entry_ref.path, entry_ref.flags, url
|
||||||
|
));
|
||||||
|
|
||||||
match launch(&entry_ref, &url) {
|
match launch(&entry_ref, &url) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@@ -137,7 +161,7 @@ fn init(cfg: Config, url: Option<String>) -> State {
|
|||||||
}
|
}
|
||||||
let _ = main_window.window().hide();
|
let _ = main_window.window().hide();
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!("launch failed: {e}"),
|
Err(e) => config::log(&format!("[bp] launch failed: {e}")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -159,10 +183,10 @@ fn init(cfg: Config, url: Option<String>) -> State {
|
|||||||
};
|
};
|
||||||
let url = main_window.get_current_url().to_string();
|
let url = main_window.get_current_url().to_string();
|
||||||
|
|
||||||
println!(
|
config::log(&format!(
|
||||||
"Launching [{}]: {:?} {:?} {}",
|
"[bp] launching [{}]: {:?} {:?} {}",
|
||||||
index, entry.path, entry.flags, url
|
index, entry.path, entry.flags, url
|
||||||
);
|
));
|
||||||
|
|
||||||
match launch(&entry, &url) {
|
match launch(&entry, &url) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@@ -175,7 +199,7 @@ fn init(cfg: Config, url: Option<String>) -> State {
|
|||||||
}
|
}
|
||||||
let _ = main_window.window().hide();
|
let _ = main_window.window().hide();
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!("launch failed: {e}"),
|
Err(e) => config::log(&format!("[bp] launch failed: {e}")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -221,7 +245,7 @@ fn init(cfg: Config, url: Option<String>) -> State {
|
|||||||
.spawn()
|
.spawn()
|
||||||
.or_else(|_| std::process::Command::new("notepad").arg(&path).spawn());
|
.or_else(|_| std::process::Command::new("notepad").arg(&path).spawn());
|
||||||
if let Err(e) = editor {
|
if let Err(e) = editor {
|
||||||
eprintln!("[bp] cannot open editor: {e}");
|
config::log(&format!("[bp] cannot open editor: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -329,9 +353,9 @@ pub fn main() {
|
|||||||
let domain = config::domain_of(url);
|
let domain = config::domain_of(url);
|
||||||
if let Some(name) = cfg.remembered.get(&domain).cloned() {
|
if let Some(name) = cfg.remembered.get(&domain).cloned() {
|
||||||
if cfg.settings.always_ask {
|
if cfg.settings.always_ask {
|
||||||
eprintln!("[bp] '{domain}' remembered -> {name}, but always_ask is on");
|
config::log(&format!("[bp] '{domain}' remembered -> {name}, but always_ask is on"));
|
||||||
} else if let Some(entry) = cfg.browsers.iter().find(|b| b.name == name) {
|
} else if let Some(entry) = cfg.browsers.iter().find(|b| b.name == name) {
|
||||||
eprintln!("[bp] '{domain}' remembered -> launching {name}");
|
config::log(&format!("[bp] '{domain}' remembered -> launching {name}"));
|
||||||
let _ = launch(entry, url);
|
let _ = launch(entry, url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -360,7 +384,10 @@ pub fn main() {
|
|||||||
winit::center_window(mw.window());
|
winit::center_window(mw.window());
|
||||||
mw.invoke_restore_focus();
|
mw.invoke_restore_focus();
|
||||||
}
|
}
|
||||||
eprintln!("[bp] cold start -> event loop: {:?}", start.elapsed());
|
config::log(&format!(
|
||||||
|
"[bp] cold start -> event loop: {:?}",
|
||||||
|
start.elapsed()
|
||||||
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
state.main_window.run().unwrap();
|
state.main_window.run().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user