feat: 加入了默认远程模板源
feat: 在list时能够自动获取远程模板源
这个提交包含在:
@ -42,6 +42,7 @@ status_synced = "synced"
|
||||
status_unsynced = "not synced"
|
||||
not_found = "Source '{0}' not found"
|
||||
removed = "Source '{0}' removed"
|
||||
cannot_remove_default = "Cannot remove default source '{0}'"
|
||||
empty = "No template sources configured"
|
||||
hint_add = "Hint: use 'muc latex source add <name> <url>' to add a source"
|
||||
item = " {0} [{1}] -> {2}"
|
||||
|
||||
@ -42,6 +42,7 @@ status_synced = "已同步"
|
||||
status_unsynced = "未同步"
|
||||
not_found = "未找到名为 '{0}' 的源"
|
||||
removed = "已移除源 '{0}'"
|
||||
cannot_remove_default = "无法移除默认源 '{0}'"
|
||||
empty = "尚未配置任何模板源"
|
||||
hint_add = "提示: 使用 'muc latex source add <name> <url>' 添加源"
|
||||
item = " {0} [{1}] -> {2}"
|
||||
|
||||
@ -43,20 +43,31 @@ fn ensure_dirs() -> Result<()> {
|
||||
pub fn load_sources() -> Result<Vec<SourceEntry>> {
|
||||
ensure_dirs()?;
|
||||
let path = sources_path()?;
|
||||
if !path.exists() {
|
||||
return Ok(vec![]);
|
||||
let mut all = default_sources();
|
||||
if path.exists() {
|
||||
let content = fs::read_to_string(&path)
|
||||
.with_context(|| i18n::tr_fmt("config.read_failed", &[path.display().to_string()]))?;
|
||||
let config: SourcesConfig = serde_json::from_str(&content)
|
||||
.with_context(|| i18n::tr_fmt("config.parse_failed", &[path.display().to_string()]))?;
|
||||
for s in config.sources {
|
||||
if !all.iter().any(|d| d.name == s.name) {
|
||||
all.push(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
let content = fs::read_to_string(&path)
|
||||
.with_context(|| i18n::tr_fmt("config.read_failed", &[path.display().to_string()]))?;
|
||||
let config: SourcesConfig = serde_json::from_str(&content)
|
||||
.with_context(|| i18n::tr_fmt("config.parse_failed", &[path.display().to_string()]))?;
|
||||
Ok(config.sources)
|
||||
Ok(all)
|
||||
}
|
||||
|
||||
pub fn save_sources(sources: &[SourceEntry]) -> Result<()> {
|
||||
ensure_dirs()?;
|
||||
let defaults = default_sources();
|
||||
let user_only: Vec<_> = sources
|
||||
.iter()
|
||||
.filter(|s| !defaults.iter().any(|d| d.name == s.name))
|
||||
.cloned()
|
||||
.collect();
|
||||
let config = SourcesConfig {
|
||||
sources: sources.to_vec(),
|
||||
sources: user_only,
|
||||
};
|
||||
let content =
|
||||
serde_json::to_string_pretty(&config).context(i18n::tr("config.serialize_failed"))?;
|
||||
@ -64,6 +75,17 @@ pub fn save_sources(sources: &[SourceEntry]) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn default_sources() -> Vec<SourceEntry> {
|
||||
vec![SourceEntry {
|
||||
name: "Offical".into(),
|
||||
url: "https://git.jamaccao.cn/MUCUtils/LaTeXTemplatesRepoistry/raw/branch/master/latest.json".into(),
|
||||
}]
|
||||
}
|
||||
|
||||
pub fn is_default_source(name: &str) -> bool {
|
||||
default_sources().iter().any(|s| s.name == name)
|
||||
}
|
||||
|
||||
pub fn load_cached_manifest(source_name: &str) -> Result<Option<SourceManifest>> {
|
||||
let path = cache_dir()?.join(format!("{}.json", source_name));
|
||||
if !path.exists() {
|
||||
|
||||
@ -4,6 +4,8 @@ use anyhow::{Context, Result};
|
||||
use reqwest::blocking::Client;
|
||||
use std::io::Cursor;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
mod config;
|
||||
mod source;
|
||||
mod template;
|
||||
@ -25,11 +27,16 @@ pub fn handle(args: LatexArgs) -> Result<()> {
|
||||
|
||||
|
||||
fn all_templates() -> Vec<Template> {
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
let mut templates = Vec::new();
|
||||
|
||||
for source in config::load_sources().unwrap_or_default() {
|
||||
if let Ok(Some(manifest)) = config::load_cached_manifest(&source.name) {
|
||||
templates.extend(manifest.templates);
|
||||
for t in manifest.templates {
|
||||
if seen.insert(t.name.to_lowercase()) {
|
||||
templates.push(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,6 +44,7 @@ fn all_templates() -> Vec<Template> {
|
||||
}
|
||||
|
||||
fn list_templates() -> Result<()> {
|
||||
let _ = source::sync_all();
|
||||
let templates = all_templates();
|
||||
|
||||
if templates.is_empty() {
|
||||
|
||||
@ -3,7 +3,7 @@ use reqwest::blocking::Client;
|
||||
|
||||
use crate::i18n;
|
||||
|
||||
use super::config::{self, load_sources, save_sources, SourceEntry};
|
||||
use super::config::{self, is_default_source, load_sources, save_sources, SourceEntry};
|
||||
use super::template::SourceManifest;
|
||||
|
||||
pub fn add_source(name: &str, url: &str) -> Result<()> {
|
||||
@ -63,6 +63,10 @@ pub fn list_sources() -> Result<()> {
|
||||
}
|
||||
|
||||
pub fn remove_source(name: &str) -> Result<()> {
|
||||
if is_default_source(name) {
|
||||
anyhow::bail!(i18n::tr_fmt("latex.source.cannot_remove_default", &[name.to_string()]));
|
||||
}
|
||||
|
||||
let mut sources = load_sources()?;
|
||||
let len_before = sources.len();
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户