ui_styles.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. use ansiterm::Style;
  2. use crate::theme::lsc::Pair;
  3. #[rustfmt::skip]
  4. #[derive(Debug, Default, PartialEq)]
  5. pub struct UiStyles {
  6. pub colourful: bool,
  7. pub filekinds: FileKinds,
  8. pub perms: Permissions,
  9. pub size: Size,
  10. pub users: Users,
  11. pub links: Links,
  12. pub git: Git,
  13. pub git_repo: GitRepo,
  14. pub security_context: SecurityContext,
  15. pub file_type: FileType,
  16. pub punctuation: Style, // xx
  17. pub date: Style, // da
  18. pub inode: Style, // in
  19. pub blocks: Style, // bl
  20. pub header: Style, // hd
  21. pub octal: Style, // oc
  22. pub symlink_path: Style, // lp
  23. pub control_char: Style, // cc
  24. pub broken_symlink: Style, // or
  25. pub broken_path_overlay: Style, // bO
  26. }
  27. #[rustfmt::skip]
  28. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  29. pub struct FileKinds {
  30. pub normal: Style, // fi
  31. pub directory: Style, // di
  32. pub symlink: Style, // ln
  33. pub pipe: Style, // pi
  34. pub block_device: Style, // bd
  35. pub char_device: Style, // cd
  36. pub socket: Style, // so
  37. pub special: Style, // sp
  38. pub executable: Style, // ex
  39. pub mount_point: Style, // mp
  40. }
  41. #[rustfmt::skip]
  42. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  43. pub struct Permissions {
  44. pub user_read: Style, // ur
  45. pub user_write: Style, // uw
  46. pub user_execute_file: Style, // ux
  47. pub user_execute_other: Style, // ue
  48. pub group_read: Style, // gr
  49. pub group_write: Style, // gw
  50. pub group_execute: Style, // gx
  51. pub other_read: Style, // tr
  52. pub other_write: Style, // tw
  53. pub other_execute: Style, // tx
  54. pub special_user_file: Style, // su
  55. pub special_other: Style, // sf
  56. pub attribute: Style, // xa
  57. }
  58. #[rustfmt::skip]
  59. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  60. pub struct Size {
  61. pub major: Style, // df
  62. pub minor: Style, // ds
  63. pub number_byte: Style, // sn nb
  64. pub number_kilo: Style, // sn nk
  65. pub number_mega: Style, // sn nm
  66. pub number_giga: Style, // sn ng
  67. pub number_huge: Style, // sn nt
  68. pub unit_byte: Style, // sb ub
  69. pub unit_kilo: Style, // sb uk
  70. pub unit_mega: Style, // sb um
  71. pub unit_giga: Style, // sb ug
  72. pub unit_huge: Style, // sb ut
  73. }
  74. #[rustfmt::skip]
  75. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  76. pub struct Users {
  77. pub user_you: Style, // uu
  78. pub user_someone_else: Style, // un
  79. pub group_yours: Style, // gu
  80. pub group_not_yours: Style, // gn
  81. }
  82. #[rustfmt::skip]
  83. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  84. pub struct Links {
  85. pub normal: Style, // lc
  86. pub multi_link_file: Style, // lm
  87. }
  88. #[rustfmt::skip]
  89. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  90. pub struct Git {
  91. pub new: Style, // ga
  92. pub modified: Style, // gm
  93. pub deleted: Style, // gd
  94. pub renamed: Style, // gv
  95. pub typechange: Style, // gt
  96. pub ignored: Style, // gi
  97. pub conflicted: Style, // gc
  98. }
  99. #[rustfmt::skip]
  100. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  101. pub struct GitRepo {
  102. pub branch_main: Style,
  103. pub branch_other: Style,
  104. pub git_clean: Style,
  105. pub git_dirty: Style,
  106. }
  107. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  108. pub struct SELinuxContext {
  109. pub colon: Style,
  110. pub user: Style, // Su
  111. pub role: Style, // Sr
  112. pub typ: Style, // St
  113. pub range: Style, // Sl
  114. }
  115. #[rustfmt::skip]
  116. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  117. pub struct SecurityContext {
  118. pub none: Style, // Sn
  119. pub selinux: SELinuxContext,
  120. }
  121. /// Drawing styles based on the type of file (video, image, compressed, etc)
  122. #[rustfmt::skip]
  123. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  124. pub struct FileType {
  125. pub image: Style, // im - image file
  126. pub video: Style, // vi - video file
  127. pub music: Style, // mu - lossy music
  128. pub lossless: Style, // lo - lossless music
  129. pub crypto: Style, // cr - related to cryptography
  130. pub document: Style, // do - document file
  131. pub compressed: Style, // co - compressed file
  132. pub temp: Style, // tm - temporary file
  133. pub compiled: Style, // cm - compilation artifact
  134. pub build: Style, // bu - file that is used to build a project
  135. }
  136. impl UiStyles {
  137. pub fn plain() -> Self {
  138. Self::default()
  139. }
  140. }
  141. impl UiStyles {
  142. /// Sets a value on this set of colours using one of the keys understood
  143. /// by the `LS_COLORS` environment variable. Invalid keys set nothing, but
  144. /// return false.
  145. pub fn set_ls(&mut self, pair: &Pair<'_>) -> bool {
  146. #[rustfmt::skip]
  147. match pair.key {
  148. "di" => self.filekinds.directory = pair.to_style(), // DIR
  149. "ex" => self.filekinds.executable = pair.to_style(), // EXEC
  150. "fi" => self.filekinds.normal = pair.to_style(), // FILE
  151. "pi" => self.filekinds.pipe = pair.to_style(), // FIFO
  152. "so" => self.filekinds.socket = pair.to_style(), // SOCK
  153. "bd" => self.filekinds.block_device = pair.to_style(), // BLK
  154. "cd" => self.filekinds.char_device = pair.to_style(), // CHR
  155. "ln" => self.filekinds.symlink = pair.to_style(), // LINK
  156. "or" => self.broken_symlink = pair.to_style(), // ORPHAN
  157. _ => return false,
  158. // Codes we don’t do anything with:
  159. // MULTIHARDLINK, DOOR, SETUID, SETGID, CAPABILITY,
  160. // STICKY_OTHER_WRITABLE, OTHER_WRITABLE, STICKY, MISSING
  161. };
  162. true
  163. }
  164. /// Sets a value on this set of colours using one of the keys understood
  165. /// by the `EZA_COLORS` environment variable. Invalid keys set nothing,
  166. /// but return false. This doesn’t take the `LS_COLORS` keys into account,
  167. /// so `set_ls` should have been run first.
  168. pub fn set_exa(&mut self, pair: &Pair<'_>) -> bool {
  169. #[rustfmt::skip]
  170. match pair.key {
  171. "ur" => self.perms.user_read = pair.to_style(),
  172. "uw" => self.perms.user_write = pair.to_style(),
  173. "ux" => self.perms.user_execute_file = pair.to_style(),
  174. "ue" => self.perms.user_execute_other = pair.to_style(),
  175. "gr" => self.perms.group_read = pair.to_style(),
  176. "gw" => self.perms.group_write = pair.to_style(),
  177. "gx" => self.perms.group_execute = pair.to_style(),
  178. "tr" => self.perms.other_read = pair.to_style(),
  179. "tw" => self.perms.other_write = pair.to_style(),
  180. "tx" => self.perms.other_execute = pair.to_style(),
  181. "su" => self.perms.special_user_file = pair.to_style(),
  182. "sf" => self.perms.special_other = pair.to_style(),
  183. "xa" => self.perms.attribute = pair.to_style(),
  184. "sn" => self.set_number_style(pair.to_style()),
  185. "sb" => self.set_unit_style(pair.to_style()),
  186. "nb" => self.size.number_byte = pair.to_style(),
  187. "nk" => self.size.number_kilo = pair.to_style(),
  188. "nm" => self.size.number_mega = pair.to_style(),
  189. "ng" => self.size.number_giga = pair.to_style(),
  190. "nt" => self.size.number_huge = pair.to_style(),
  191. "ub" => self.size.unit_byte = pair.to_style(),
  192. "uk" => self.size.unit_kilo = pair.to_style(),
  193. "um" => self.size.unit_mega = pair.to_style(),
  194. "ug" => self.size.unit_giga = pair.to_style(),
  195. "ut" => self.size.unit_huge = pair.to_style(),
  196. "df" => self.size.major = pair.to_style(),
  197. "ds" => self.size.minor = pair.to_style(),
  198. "uu" => self.users.user_you = pair.to_style(),
  199. "un" => self.users.user_someone_else = pair.to_style(),
  200. "gu" => self.users.group_yours = pair.to_style(),
  201. "gn" => self.users.group_not_yours = pair.to_style(),
  202. "lc" => self.links.normal = pair.to_style(),
  203. "lm" => self.links.multi_link_file = pair.to_style(),
  204. "ga" => self.git.new = pair.to_style(),
  205. "gm" => self.git.modified = pair.to_style(),
  206. "gd" => self.git.deleted = pair.to_style(),
  207. "gv" => self.git.renamed = pair.to_style(),
  208. "gt" => self.git.typechange = pair.to_style(),
  209. "gi" => self.git.ignored = pair.to_style(),
  210. "gc" => self.git.conflicted = pair.to_style(),
  211. "xx" => self.punctuation = pair.to_style(),
  212. "da" => self.date = pair.to_style(),
  213. "in" => self.inode = pair.to_style(),
  214. "bl" => self.blocks = pair.to_style(),
  215. "hd" => self.header = pair.to_style(),
  216. "oc" => self.octal = pair.to_style(),
  217. "lp" => self.symlink_path = pair.to_style(),
  218. "cc" => self.control_char = pair.to_style(),
  219. "bO" => self.broken_path_overlay = pair.to_style(),
  220. "mp" => self.filekinds.mount_point = pair.to_style(),
  221. "sp" => self.filekinds.special = pair.to_style(), // Catch-all for unrecognized file kind
  222. "im" => self.file_type.image = pair.to_style(),
  223. "vi" => self.file_type.video = pair.to_style(),
  224. "mu" => self.file_type.music = pair.to_style(),
  225. "lo" => self.file_type.lossless = pair.to_style(),
  226. "cr" => self.file_type.crypto = pair.to_style(),
  227. "do" => self.file_type.document = pair.to_style(),
  228. "co" => self.file_type.compressed = pair.to_style(),
  229. "tm" => self.file_type.temp = pair.to_style(),
  230. "cm" => self.file_type.compiled = pair.to_style(),
  231. "bu" => self.file_type.build = pair.to_style(),
  232. "Sn" => self.security_context.none = pair.to_style(),
  233. "Su" => self.security_context.selinux.user = pair.to_style(),
  234. "Sr" => self.security_context.selinux.role = pair.to_style(),
  235. "St" => self.security_context.selinux.typ = pair.to_style(),
  236. "Sl" => self.security_context.selinux.range = pair.to_style(),
  237. _ => return false,
  238. };
  239. true
  240. }
  241. pub fn set_number_style(&mut self, style: Style) {
  242. self.size.number_byte = style;
  243. self.size.number_kilo = style;
  244. self.size.number_mega = style;
  245. self.size.number_giga = style;
  246. self.size.number_huge = style;
  247. }
  248. pub fn set_unit_style(&mut self, style: Style) {
  249. self.size.unit_byte = style;
  250. self.size.unit_kilo = style;
  251. self.size.unit_mega = style;
  252. self.size.unit_giga = style;
  253. self.size.unit_huge = style;
  254. }
  255. }