From 64646500be056d28dcd02a38697fb51c6b75bd15 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Tue, 26 Dec 2017 23:41:05 -0500 Subject: [PATCH] Add .Xresources theme support This commit adds theme support (through .Xresources) for i3 and polybar. As an added bonus, theme.cr has been updated to make changing these colors automatic. The termite config has been updated to reflect the new settings from theme.cr. --- dotfiles/.Xresources | 31 ++++++++++++++++++ dotfiles/.config/i3/config | 15 ++++++--- dotfiles/.config/polybar/config | 25 +++++++------- dotfiles/.config/termite/config | 36 ++++++++++++++++---- src/theme.cr | 2 ++ src/theme_helper/theme_helper.cr | 56 +++++++++++++++++++++++++++++++- 6 files changed, 140 insertions(+), 25 deletions(-) diff --git a/dotfiles/.Xresources b/dotfiles/.Xresources index af59086f..d7d339a3 100644 --- a/dotfiles/.Xresources +++ b/dotfiles/.Xresources @@ -38,3 +38,34 @@ Xft.hintstyle: hintfull ! Make font colors look better (?) Xft.rgba: rgb + +! This line is used to indicate where to add theme colors. Note that everything +! after this line is replaced. See the theme docs for more information. +! [colors] +*foreground: #959DCB +*cursorColor: #959DCB +*background: #292D3E +*color0: #292D3E +*color8: #676E95 +*color7: #959DCB +*color15: #FFFFFF +*color1: #F07178 +*color9: #F07178 +*color2: #C3E88D +*color10: #C3E88D +*color3: #FFCB6B +*color11: #FFCB6B +*color4: #82AAFF +*color12: #82AAFF +*color5: #C792EA +*color13: #C792EA +*color6: #89DDFF +*color14: #89DDFF +*color16: #F78C6C +*color17: #FF5370 +*color18: #444267 +*color19: #32374D +*color20: #8796B0 +*color21: #959DCB + +! vim:ft=xdefaults diff --git a/dotfiles/.config/i3/config b/dotfiles/.config/i3/config index 9d0b1c98..cbbecb14 100644 --- a/dotfiles/.config/i3/config +++ b/dotfiles/.config/i3/config @@ -211,10 +211,15 @@ bindsym $mod+y border pixel 1 # color to green and everything else to a light grey #################################################################### -# Colors: Border Backgr. Text Indicator -client.focused #4f2c4c #4f2c4c #ffffff #4f2c4c -client.focused_inactive #888888 #2b2b2b #ffffff #2b2b2b -client.unfocused #888888 #2b2b2b #ffffff #2b2b2b -client.urgent #900000 #900000 #ffffff #900000 +set_from_resource $text i3wm.color8 +set_from_resource $focused i3wm.color18 +set_from_resource $unfocused i3wm.color19 +set_from_resource $urgent i3wm.color3 + +# Colors: Border Background Text Indicator +client.focused $focused $focused $text $focused +client.focused_inactive $unfocused $unfocused $text $unfocused +client.unfocused $unfocused $unfocused $text $unfocused +client.urgent $urgent $urgent $text $urgent # vim:ft=dosini diff --git a/dotfiles/.config/polybar/config b/dotfiles/.config/polybar/config index 2bfb2f80..64692d1a 100644 --- a/dotfiles/.config/polybar/config +++ b/dotfiles/.config/polybar/config @@ -18,35 +18,36 @@ # ################################################################################## +[color] +background = ${xrdb:color0:#19171C} +text = ${xrdb:color7:#8B8792} +highlight = ${xrdb:color5:#AF80AC} +yellow = ${xrdb:color3:#AF80AC} +blue = ${xrdb:color6:#AF80AC} + [bar/main] modules-left = i3 modules-center = cmus modules-right = cpu ram volume date time monitor = VGA-1 -#foreground = #AF80AC -foreground = #8a7f8f -background = #EE1C1E21 +foreground = ${color.text} +background = ${color.background} font-0 = "Fira Mono:size=22" font-1 = "Noto Sans CJK JP:size=22" height = 88 padding-left = 2 -#underline-size = 5 padding-right = 2 module-margin = 1 [module/i3] type = internal/i3 -#label-focused-background = #AA4f2c4c -label-focused-underline = #B894B0 - -label-focused-foreground = #AF80AC -#label-focused-foreground = #c16772 -label-unfocused-foreground = #8a7f8f -label-urgent-foreground = #e4b226 -label-visible-foreground = #f2f2f2 +label-focused-foreground = ${color.highlight} +label-unfocused-foreground = ${color.text} +label-urgent-foreground = ${color.yellow} +label-visible-foreground = ${color.blue} label-focused-padding = 2 label-unfocused-padding = 2 diff --git a/dotfiles/.config/termite/config b/dotfiles/.config/termite/config index 162db4df..e55c1cf8 100644 --- a/dotfiles/.config/termite/config +++ b/dotfiles/.config/termite/config @@ -19,20 +19,42 @@ ################################################################################## [options] - -# Our font of choice +# 3 fonts of preference, sorted by readability font = Hack 12 +#font = Inconsolata 14 #font = Fira Mono 12 -#font = Inconsolata 12 # No need to show the mouse in the terminal window mouse_autohide = true [colors] +foreground = #959DCB +foreground_bold = #959DCB +cursor = #959DCB +cursor_foreground = #292D3E +background = #292D3E -# 5% Transparency -# Note: 20% is better when you're not using clear-shadow -# background = rgba(63, 63, 63, 0.95) -background = #19171C +color0 = #292D3E +color8 = #676E95 +color7 = #959DCB +color15 = #FFFFFF +color1 = #F07178 +color9 = #F07178 +color2 = #C3E88D +color10 = #C3E88D +color3 = #FFCB6B +color11 = #FFCB6B +color4 = #82AAFF +color12 = #82AAFF +color5 = #C792EA +color13 = #C792EA +color6 = #89DDFF +color14 = #89DDFF +color16 = #F78C6C +color17 = #FF5370 +color18 = #444267 +color19 = #32374D +color20 = #8796B0 +color21 = #959DCB # vim:ft=dosini diff --git a/src/theme.cr b/src/theme.cr index 7d622b29..41f89b76 100644 --- a/src/theme.cr +++ b/src/theme.cr @@ -12,6 +12,8 @@ if ARGV.size > 0 theme : Hash(YAML::Type, YAML::Type) = YAML.parse(File.read file).as_h Theme.set_terminal(theme) + Theme.set_xresources(theme) + system("i3 restart >/dev/null 2>&1") puts "Successfully changed the theme to #{theme["scheme"].to_s}!" else Theme.test diff --git a/src/theme_helper/theme_helper.cr b/src/theme_helper/theme_helper.cr index 196c24d9..7ca0f3fa 100644 --- a/src/theme_helper/theme_helper.cr +++ b/src/theme_helper/theme_helper.cr @@ -45,6 +45,52 @@ module Theme #test_color :white, "color07 base05" end + # Sets all the colors in .Xresources, then reloads xrdb. + # + # This method will replace everything after the `[colors]` + # line with the theme colors, then reload xrdb to make sure + # that everything takes effect. + # + # You should reload i3 (which in turn should reload polybar) + # if you want the changes to take effect immediately. + def set_xresources(theme : Hash(YAML::Type, YAML::Type)) + file : String = ENV["HOME"] + "/.Xresources" + _DNE(file) if !File.exists?(file) + config : String = "" + File.each_line(file) do |line| + config += line + "\n" + break if line.includes?("[colors]") + end + config += add_xcolor_fgbg "foreground", theme["base05"] + config += add_xcolor_fgbg "cursorColor", theme["base06"] + config += add_xcolor_fgbg "background", theme["base00"] + config += add_xcolor 0, theme["base00"] # Black + config += add_xcolor 8, theme["base03"] # Gray + config += add_xcolor 7, theme["base05"] # Silver + config += add_xcolor 15, theme["base07"] # White + config += add_xcolor 1, theme["base08"] # Red + config += add_xcolor 9, theme["base08"] + config += add_xcolor 2, theme["base0B"] # Green + config += add_xcolor 10, theme["base0B"] + config += add_xcolor 3, theme["base0A"] # Yellow + config += add_xcolor 11, theme["base0A"] + config += add_xcolor 4, theme["base0D"] # Blue + config += add_xcolor 12, theme["base0D"] + config += add_xcolor 5, theme["base0E"] # Purple + config += add_xcolor 13, theme["base0E"] + config += add_xcolor 6, theme["base0C"] # Teal + config += add_xcolor 14, theme["base0C"] + config += add_xcolor 16, theme["base09"] # "Extra" colors + config += add_xcolor 17, theme["base0F"] + config += add_xcolor 18, theme["base01"] + config += add_xcolor 19, theme["base02"] + config += add_xcolor 20, theme["base04"] + config += add_xcolor 21, theme["base06"] + config += "\n! vim:ft=xdefaults\n" + File.write(file, config) + system("xrdb ~/.Xresources") + end + # Sets all the colors in termite. # # This is the preferred method of changing your terminal @@ -201,10 +247,18 @@ module Theme return "color#{num.to_s.ljust(2, ' ')} = ##{color.to_s}\n" end - private def add_term_fgbg(type : String, color : YAML::Type) + private def add_term_fgbg(type : String, color : YAML::Type) : String return "#{type.ljust(17, ' ')} = ##{color.to_s}\n" end + private def add_xcolor(num : Int32, color : YAML::Type) : String + return "*color#{num.to_s}: ##{color.to_s}\n" + end + + private def add_xcolor_fgbg(type : String, color : YAML::Type) : String + return "*#{type}: ##{color.to_s}\n" + end + # Prints an error that the file does not exists. def _DNE(file : String) _e("The file " + File.expand_path(file) + " does not exist!")