mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use crate::{errors::*, utils::ui::uicore::APP_INTERNAL_INFOMATION};
  2. use app_dirs2::{app_dir, AppDataType, AppInfo};
  3. use std::{
  4. cell::RefCell,
  5. path::{Path, PathBuf},
  6. rc::Rc,
  7. };
  8. use yaml::YamlPerferences;
  9. use yaml_rust::{Yaml, YamlLoader};
  10. use super::APP_INFO;
  11. pub mod yaml;
  12. const SYNTAX_PATH: &str = "syntaxes";
  13. const THEME_PATH: &str = "themes";
  14. const INPUT_CONFIG_PATH: &str = "input";
  15. const THEME_KET: &str = "theme";
  16. const LANGUAGE_KEY: &str = "language";
  17. const LANGUAGE_SYNTAX_KEY: &str = "syntax";
  18. const LINE_WRAPPING_KEY: &str = "line_wrapping";
  19. pub trait Perferences {
  20. /// 载入
  21. fn load(&mut self);
  22. /// 是否自动换行
  23. fn line_wrapping(&self) -> bool;
  24. // tab宽度
  25. fn tab_width(&self) -> usize;
  26. // 是否使用空格模拟tab
  27. fn soft_tab(&self) -> bool;
  28. // 设置的主题文件路径
  29. fn theme_path(&self) -> Result<PathBuf> {
  30. #[cfg(not(feature = "dragonos"))]
  31. {
  32. app_dir(AppDataType::UserConfig, &APP_INFO, THEME_PATH)
  33. .chain_err(|| "Couldn't create a themes directory or build a path tp it")
  34. }
  35. #[cfg(feature = "dragonos")]
  36. Ok(PathBuf::new())
  37. }
  38. // 输入映射配置文件路径
  39. fn input_config_path(&self) -> Result<PathBuf> {
  40. #[cfg(not(feature = "dragonos"))]
  41. {
  42. app_dir(AppDataType::UserConfig, &APP_INFO, INPUT_CONFIG_PATH)
  43. .chain_err(|| "Couldn't create a themes directory or build a path tp it")
  44. }
  45. #[cfg(feature = "dragonos")]
  46. Ok(PathBuf::new())
  47. }
  48. // 设置的主题名字
  49. fn theme_name(&self) -> Option<String>;
  50. // 返回设置的语法定义:例:test.rs -> rs test.cpp -> cpp
  51. fn syntax_definition_name(&self, path: &Path) -> Option<String>;
  52. }
  53. pub struct PerferencesManager;
  54. impl PerferencesManager {
  55. pub fn load() -> Result<Rc<RefCell<dyn Perferences>>> {
  56. match Self::load_extend()? {
  57. Some(_) => todo!(),
  58. None => return Ok(Rc::new(RefCell::new(Self::load_default_perferences()?))),
  59. }
  60. }
  61. pub fn user_syntax_path() -> Result<PathBuf> {
  62. app_dir(AppDataType::UserConfig, &APP_INFO, SYNTAX_PATH)
  63. .chain_err(|| "Couldn't create syntax directory or build a path to it.")
  64. }
  65. fn load_default_perferences() -> Result<YamlPerferences> {
  66. let yaml = YamlLoader::load_from_str(include_str!("default.yaml"))
  67. .chain_err(|| "Couldn't parse default config file")?
  68. .into_iter()
  69. .next()
  70. .chain_err(|| "No default preferences document found")?;
  71. Ok(YamlPerferences::new(yaml))
  72. }
  73. fn load_extend() -> Result<Option<Rc<RefCell<dyn Perferences>>>> {
  74. // 可能涉及加载其他格式的配置文件
  75. Ok(None)
  76. }
  77. }
  78. #[cfg(test)]
  79. pub struct DummyPerferences;
  80. #[cfg(test)]
  81. impl Perferences for DummyPerferences {
  82. fn line_wrapping(&self) -> bool {
  83. true
  84. }
  85. fn tab_width(&self) -> usize {
  86. 2
  87. }
  88. fn soft_tab(&self) -> bool {
  89. todo!()
  90. }
  91. fn theme_path(&self) -> Result<PathBuf> {
  92. todo!()
  93. }
  94. fn theme_name(&self) -> Option<String> {
  95. todo!()
  96. }
  97. fn load(&mut self) {
  98. todo!()
  99. }
  100. fn syntax_definition_name(&self, path: &Path) -> Option<String> {
  101. todo!()
  102. }
  103. fn input_config_path(&self) -> Result<PathBuf> {
  104. todo!()
  105. }
  106. }