fancy_tree/config/colors/
mod.rs1use super::ConfigFile;
3use crate::color::Color;
4use crate::git::status::Status;
5use crate::lua::interop;
6use crate::tree::Entry;
7use mlua::{FromLua, Lua};
8use std::path::Path;
9
10#[derive(Debug)]
12pub struct Colors {
13 for_icon: mlua::Function,
15 git_statuses: GitStatuses,
16}
17
18impl Colors {
19 pub fn for_icon<P>(
21 &self,
22 entry: &Entry<P>,
23 default_choice: Option<Color>,
24 ) -> mlua::Result<Option<Color>>
25 where
26 P: AsRef<Path>,
27 {
28 let path = entry.path();
29 let attributes = interop::FileAttributes::from(entry);
30
31 self.for_icon.call((path, attributes, default_choice))
32 }
33
34 pub fn for_untracked_git_status(
36 &self,
37 status: Status,
38 default_choice: Option<Color>,
39 ) -> mlua::Result<Option<Color>> {
40 self.git_statuses.untracked.call((status, default_choice))
41 }
42
43 pub fn for_tracked_git_status(
45 &self,
46 status: Status,
47 default_choice: Option<Color>,
48 ) -> mlua::Result<Option<Color>> {
49 self.git_statuses.tracked.call((status, default_choice))
50 }
51}
52
53impl ConfigFile for Colors {
54 const FILENAME: &'static str = "colors.lua";
55 const DEFAULT_MODULE: &'static str = include_str!("./colors.lua");
56}
57
58impl FromLua for Colors {
59 fn from_lua(value: mlua::Value, lua: &Lua) -> mlua::Result<Self> {
60 const FOR_ICON_KEY: &str = "icons";
61 const GIT_STATUSES_KEY: &str = "git_statuses";
62
63 let table = mlua::Table::from_lua(value, lua)?;
64 let for_icon = table.get::<mlua::Function>(FOR_ICON_KEY)?;
65 let git_statuses = table.get::<GitStatuses>(GIT_STATUSES_KEY)?;
66
67 let colors = Self {
68 for_icon,
69 git_statuses,
70 };
71 Ok(colors)
72 }
73}
74
75#[derive(Debug)]
77struct GitStatuses {
78 tracked: mlua::Function,
80 untracked: mlua::Function,
82}
83
84impl FromLua for GitStatuses {
85 fn from_lua(value: mlua::Value, lua: &Lua) -> mlua::Result<Self> {
86 const TRACKED_KEY: &str = "tracked";
87 const UNTRACKED_KEY: &str = "untracked";
88
89 let table = mlua::Table::from_lua(value, lua)?;
90 let tracked = table.get::<mlua::Function>(TRACKED_KEY)?;
91 let untracked = table.get::<mlua::Function>(UNTRACKED_KEY)?;
92
93 let git_statuses = Self { tracked, untracked };
94 Ok(git_statuses)
95 }
96}