From 156b2d263cc1eb8493112cb5e8792f09fa59e773 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Sun, 24 Dec 2017 15:51:45 -0500 Subject: [PATCH] Update theme.cr to change terminal colors directly --- lib/theme.cr | 167 ++++++++++++++++++++++++++++++++++++++++----------- src/theme.cr | 29 ++++----- 2 files changed, 144 insertions(+), 52 deletions(-) diff --git a/lib/theme.cr b/lib/theme.cr index 0c94e4c8..196c24d9 100644 --- a/lib/theme.cr +++ b/lib/theme.cr @@ -3,6 +3,48 @@ require "colorize" module Theme extend self + # Prints the colors of the theme in the current shell. + # + # Useful when you want to see all the colors of the + # theme that you're using. + # + # NOTE: This method does not change any colors. You should use + # another method like #set_terminal to change colors first. + # + # NOTE: Although most (if not all) Base16 themes follow the + # color specification exactly, you should by no means + # use a color combination if it doesn't work well with + # other programs. An example of where you'd want to + # carefully pick your colors is for the man pages, + # where the bottom text is yellow on cyan. + # + # TODO: In the future, it may be best to add edge cases to + # the #test method to ensure everything looks nice. + # + # TODO: It may also help to add a description of what these + # colors will affect in addition to the terminal + # (e.g. i3 borders, polybar text, etc.) + def test + #test_color :black, "color21 base06" + test_color :red, "color01 base08" + test_color :green, "color02 base0B" + test_color :yellow, "color03 base0A" + test_color :blue, "color04 base0D" + test_color :magenta, "color05 base0E" + test_color :cyan, "color06 base0C" + # Note that color07 and color08 aren't the real numbers here + # The same applies for their respective base values + test_color :light_gray, "color07 base01" + test_color :dark_gray, "color08 base03" + #test_color :light_red, "color09 base08" + #test_color :light_green, "color10 base0B" + #test_color :light_yellow, "color11 base0A" + #test_color :light_blue, "color12 base0D" + #test_color :light_magenta, "color13 base0E" + #test_color :light_cyan, "color14 base0C" + #test_color :white, "color07 base05" + end + # Sets all the colors in termite. # # This is the preferred method of changing your terminal @@ -16,14 +58,51 @@ module Theme # with different themes; however, you should really only # be using one theme at a time. # - # NOTE: This method assumes that (1) `[color]` is at the + # NOTE: This method assumes that (1) `[colors]` is at the # bottom of your config file. This method will - # overwrite all lines after the `[color]` line. + # overwrite all lines after the first line with + # the keyword `[colors]` in it. # # NOTE: This method assumes that you want `# vim:ft=dosini` # at the bottom of your config file. def set_terminal(theme : Hash(YAML::Type, YAML::Type)) file : String = ENV["HOME"] + "/.config/termite/config" + _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_term_fgbg "foreground", theme["base05"] + config += add_term_fgbg "foreground_bold", theme["base06"] + config += add_term_fgbg "cursor", theme["base06"] + config += add_term_fgbg "cursor_foreground", theme["base00"] + config += add_term_fgbg "background", theme["base00"] + config += "\n" + config += add_term 0, theme["base00"] # Black + config += add_term 8, theme["base03"] # Gray + config += add_term 7, theme["base05"] # Silver + config += add_term 15, theme["base07"] # White + config += add_term 1, theme["base08"] # Red + config += add_term 9, theme["base08"] + config += add_term 2, theme["base0B"] # Green + config += add_term 10, theme["base0B"] + config += add_term 3, theme["base0A"] # Yellow + config += add_term 11, theme["base0A"] + config += add_term 4, theme["base0D"] # Blue + config += add_term 12, theme["base0D"] + config += add_term 5, theme["base0E"] # Purple + config += add_term 13, theme["base0E"] + config += add_term 6, theme["base0C"] # Teal + config += add_term 14, theme["base0C"] + config += add_term 16, theme["base09"] # "Extra" colors + config += add_term 17, theme["base0F"] + config += add_term 18, theme["base01"] + config += add_term 19, theme["base02"] + config += add_term 20, theme["base04"] + config += add_term 21, theme["base06"] + config += "\n# vim:ft=dosini\n" + File.write(file, config) end # Sets the background color in termite. @@ -35,12 +114,10 @@ module Theme # # NOTE: You should avoid manually calling #set_back and # #set_shell. Instead, you should use #set_terminal - # instead as this fixes a lot of edge cases. + # instead since it fixes a lot of edge cases. def set_back(theme : Hash(YAML::Type, YAML::Type)) file : String = ENV["HOME"] + "/.config/termite/config" - if !File.exists?(file) - _e("The file " + File.expand_path(file) + " doesn't exist!") - end + _DNE(file) if !File.exists?(file) config : String = "" File.each_line(file) do |line| if line.includes?("background = ") @@ -69,35 +146,35 @@ module Theme # NOTE: Another downside to using #set_shell is that colors # will be reset by commands like `tput reset`. Note # that #set_terminal does not have this issue. - def set_shell(t : Hash(YAML::Type, YAML::Type)) - theme = t.clone - theme.each do |key, value| - theme[key] = value.to_s.insert(4, '/').insert(2, '/') + def set_shell(theme : Hash(YAML::Type, YAML::Type)) + _theme = theme.clone + _theme.each do |key, value| + _theme[key] = value.to_s.insert(4, '/').insert(2, '/') end - add_color 0, theme["base00"] # Black - add_color 1, theme["base08"] # Red - add_color 2, theme["base0B"] # Green - add_color 3, theme["base0A"] # Yellow - add_color 4, theme["base0D"] # Blue - add_color 5, theme["base0E"] # Magenta - add_color 6, theme["base0C"] # Cyan - add_color 7, theme["base05"] # White - add_color 8, theme["base03"] # Bright Black - add_color 9, theme["base08"] # Bright Red - add_color 10, theme["base0B"] # Bright Green - add_color 11, theme["base0A"] # Bright Yellow - add_color 12, theme["base0D"] # Bright Blue - add_color 13, theme["base0E"] # Bright Magenta - add_color 14, theme["base0C"] # Bright Cyan - add_color 15, theme["base07"] # Bright White - add_color 16, theme["base09"] - add_color 17, theme["base0F"] - add_color 18, theme["base01"] - add_color 19, theme["base02"] - add_color 20, theme["base04"] - add_color 21, theme["base06"] - fgbg_color 10, theme["base05"] - fgbg_color 11, theme["base00"] + add_color 0, _theme["base00"] # Black + add_color 1, _theme["base08"] # Red + add_color 2, _theme["base0B"] # Green + add_color 3, _theme["base0A"] # Yellow + add_color 4, _theme["base0D"] # Blue + add_color 5, _theme["base0E"] # Magenta + add_color 6, _theme["base0C"] # Cyan + add_color 7, _theme["base05"] # White + add_color 8, _theme["base03"] # Bright Black + add_color 9, _theme["base08"] # Bright Red + add_color 10, _theme["base0B"] # Bright Green + add_color 11, _theme["base0A"] # Bright Yellow + add_color 12, _theme["base0D"] # Bright Blue + add_color 13, _theme["base0E"] # Bright Magenta + add_color 14, _theme["base0C"] # Bright Cyan + add_color 15, _theme["base07"] # Bright White + add_color 16, _theme["base09"] + add_color 17, _theme["base0F"] + add_color 18, _theme["base01"] + add_color 19, _theme["base02"] + add_color 20, _theme["base04"] + add_color 21, _theme["base06"] + fgbg_color 10, _theme["base05"] + fgbg_color 11, _theme["base00"] cursor_color 12, ";7" end @@ -113,7 +190,27 @@ module Theme print "\033]#{num}#{color}\033\\" end - private def _e(message : String) + private def test_color(color : Symbol, text : String) + print text.colorize(color) + print " " + print "___________________________".colorize.fore(color).back(color) + print "\n" + end + + private def add_term(num : Int32, color : YAML::Type) : String + return "color#{num.to_s.ljust(2, ' ')} = ##{color.to_s}\n" + end + + private def add_term_fgbg(type : String, color : YAML::Type) + return "#{type.ljust(17, ' ')} = ##{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!") + end + + def _e(message : String) puts message.colorize(:red) exit 1 end diff --git a/src/theme.cr b/src/theme.cr index be683bad..3ebffdee 100644 --- a/src/theme.cr +++ b/src/theme.cr @@ -1,23 +1,18 @@ require "theme" require "yaml" -require "colorize" -Dir.cd(ENV["HOME"] + "/Home/new-start") +if ARGV.size > 0 + file : String = ARGV[0] + Theme._DNE(file) if !File.exists?(file) -# Testing for now. -file : String = ARGV.size > 0 ? ARGV[0] : "themes/atelier-cave-light.yaml" + if File.extname(file) != ".yaml" && File.extname(file) != ".yml" + Theme._e("The file #{File.expand_path(file)} doesn't appear to be .yaml!") + end -if !File.exists?(file) - _e("The file " + File.expand_path(file) + " doesn't exist!") -end - -yaml : Hash(YAML::Type, YAML::Type) = YAML.parse(File.read file).as_h - -# Theme.set_terminal(yaml) -Theme.set_shell(yaml) -# Theme.set_back(yaml) - -private def _e(message) - puts message.colorize(:red) - exit 1 + theme : Hash(YAML::Type, YAML::Type) = YAML.parse(File.read file).as_h + + Theme.set_terminal(theme) + puts "Successfully changed the theme to #{theme["scheme"].to_s}!" +else + Theme.test end