mirror of
https://github.com/donovanglover/nix-config.git
synced 2024-12-23 23:29:13 +01:00
Remove theme.cr in favor of Mica
This commit removes theme.cr in favor of Mica, a universal way to change color schemes. This lets me better create abstractions for the scheme changing functionality, making it easier to both add and apply color schemes with different combinations of software.
This commit is contained in:
parent
04730405d5
commit
89b682aed6
26
src/theme.cr
26
src/theme.cr
@ -1,26 +0,0 @@
|
||||
require "./theme_helper"
|
||||
require "yaml"
|
||||
|
||||
if ARGV.size > 0
|
||||
file : String = ARGV[0]
|
||||
Theme._DNE(file) if !File.exists?(file)
|
||||
|
||||
if File.extname(file) != ".yaml" && File.extname(file) != ".yml"
|
||||
Theme._e("The file #{File.expand_path(file)} doesn't appear to be .yaml!")
|
||||
end
|
||||
|
||||
theme : Hash(YAML::Type, YAML::Type) = YAML.parse(File.read file).as_h
|
||||
|
||||
if ARGV.size > 1
|
||||
Theme.preview(theme)
|
||||
else
|
||||
Theme.set_terminal(theme)
|
||||
Theme.set_zathura(theme)
|
||||
Theme.set_xresources(theme)
|
||||
system("i3 restart >/dev/null 2>&1")
|
||||
puts "Successfully changed the theme to #{theme["scheme"].to_s}!"
|
||||
end
|
||||
|
||||
else
|
||||
Theme.test
|
||||
end
|
@ -1,390 +0,0 @@
|
||||
require "colorize"
|
||||
require "./../trucolor"
|
||||
|
||||
# The theme helper handles changing the color scheme for various
|
||||
# programs. It relies on Base16 color schemes and is an easy way
|
||||
# to change color schemes for all the software you use all at once
|
||||
# with a single command.
|
||||
#
|
||||
# Note that software such as vim use the colors directly from the
|
||||
# terminal and therefore do not need an explicit change in a config
|
||||
# file. Other software, however, have the different colors hard-coded
|
||||
# into the config file.
|
||||
#
|
||||
# This software would not exist without the help from a variety of
|
||||
# people that have contributed to the "Base16 movement". Theme creators
|
||||
# made optimal color schemes and other contributors decided on which
|
||||
# colors to use for different software. It is because of their open
|
||||
# contributions that I am able to make this program the way it is.
|
||||
#
|
||||
# To learn more, here is the complete list of the Base16 repositories
|
||||
# I used in the development of this software:
|
||||
#
|
||||
# https://github.com/chriskempson/base16
|
||||
# https://github.com/chriskempson/base16-vim
|
||||
# https://github.com/chriskempson/base16-shell
|
||||
# https://github.com/chriskempson/base16-xresources
|
||||
# https://github.com/nicodebo/base16-zathura
|
||||
# https://github.com/samme/base16-styles
|
||||
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
|
||||
|
||||
# Previews a given theme.
|
||||
#
|
||||
# Unlike #test, #preview takes a given theme and prints
|
||||
# those colors to the terminal.
|
||||
def preview(theme : Hash(YAML::Type, YAML::Type))
|
||||
preview_color theme["base00"], "base00"
|
||||
preview_color theme["base01"], "base01"
|
||||
preview_color theme["base02"], "base02"
|
||||
preview_color theme["base03"], "base03"
|
||||
preview_color theme["base04"], "base04"
|
||||
preview_color theme["base05"], "base05"
|
||||
preview_color theme["base06"], "base06"
|
||||
preview_color theme["base07"], "base07"
|
||||
preview_color theme["base08"], "base08"
|
||||
preview_color theme["base09"], "base09"
|
||||
preview_color theme["base0A"], "base0A"
|
||||
preview_color theme["base0B"], "base0B"
|
||||
preview_color theme["base0C"], "base0C"
|
||||
preview_color theme["base0D"], "base0D"
|
||||
preview_color theme["base0E"], "base0E"
|
||||
preview_color theme["base0F"], "base0F"
|
||||
end
|
||||
|
||||
# Sets all the colors in zathura.
|
||||
#
|
||||
# This method will replace everything after the `[colors]`
|
||||
# line with the theme colors. You can then open a new
|
||||
# zathura window to see the changes instantly.
|
||||
def set_zathura(theme : Hash(YAML::Type, YAML::Type))
|
||||
file : String = ENV["HOME"] + "/.config/zathura/zathurarc"
|
||||
_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_zcolor "default-bg", theme["base00"]
|
||||
config += add_zcolor "default-fg", theme["base01"]
|
||||
config += add_zcolor "statusbar-bg", theme["base02"]
|
||||
config += add_zcolor "statusbar-fg", theme["base04"]
|
||||
config += add_zcolor "inputbar-bg", theme["base00"]
|
||||
config += add_zcolor "inputbar-fg", theme["base07"]
|
||||
config += add_zcolor "notification-bg", theme["base00"]
|
||||
config += add_zcolor "notification-fg", theme["base07"]
|
||||
config += add_zcolor "notification-error-bg", theme["base00"]
|
||||
config += add_zcolor "notification-error-fg", theme["base08"]
|
||||
config += add_zcolor "notification-warning-bg", theme["base00"]
|
||||
config += add_zcolor "notification-warning-fg", theme["base08"]
|
||||
config += add_zcolor "highlight-color", theme["base0A"]
|
||||
config += add_zcolor "highlight-active-color", theme["base0D"]
|
||||
config += add_zcolor "completion-bg", theme["base01"]
|
||||
config += add_zcolor "completion-fg", theme["base0D"]
|
||||
config += add_zcolor "completion-highlight-bg", theme["base0D"]
|
||||
config += add_zcolor "completion-highlight-fg", theme["base07"]
|
||||
config += add_zcolor "recolor-lightcolor", theme["base00"]
|
||||
config += add_zcolor "recolor-darkcolor", theme["base06"]
|
||||
config += "\n# vim:ft=conf\n"
|
||||
File.write(file, config)
|
||||
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
|
||||
# colors. It also lets you avoid a few edge cases that
|
||||
# #set_back and #set_shell are prone to.
|
||||
#
|
||||
# The only downside to #set_terminal is that you have to
|
||||
# open a new window in order for the changes to take effect.
|
||||
#
|
||||
# It is also possible to have several windows open each
|
||||
# with different themes; however, you should really only
|
||||
# be using one theme at a time.
|
||||
#
|
||||
# NOTE: This method assumes that (1) `[colors]` is at the
|
||||
# bottom of your config file. This method will
|
||||
# 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 colors used inside the web browser.
|
||||
#
|
||||
# This method changes the css variables used by the browser,
|
||||
# allowing custom styles to take advantage of this and serve
|
||||
# a variety of different color schemes effortlessly.
|
||||
#
|
||||
# This makes it trivial to have a browser that is completely
|
||||
# immersed in your color scheme. You can even theme the websites
|
||||
# that you use often enough so that it matches any color scheme
|
||||
# you switch to!
|
||||
def set_web(theme : Hash(YAML::Type, YAML::Type))
|
||||
css : String = ""
|
||||
theme.each do |key, value|
|
||||
css += add_css key, value
|
||||
end
|
||||
puts css
|
||||
end
|
||||
|
||||
# Sets the background color in termite.
|
||||
#
|
||||
# NOTE: This is particularly useful to avoid color flashing.
|
||||
# Color flashing occurs when the terminal background
|
||||
# gets changed to a different color by the shell
|
||||
# at startup.
|
||||
#
|
||||
# NOTE: You should avoid manually calling #set_back and
|
||||
# #set_shell. Instead, you should use #set_terminal
|
||||
# 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"
|
||||
_DNE(file) if !File.exists?(file)
|
||||
config : String = ""
|
||||
File.each_line(file) do |line|
|
||||
if line.includes?("background = ")
|
||||
line = "background = #" + theme["base00"].to_s
|
||||
end
|
||||
config += line + "\n"
|
||||
end
|
||||
File.write(file, config)
|
||||
end
|
||||
|
||||
# Sets the terminal colors in the current shell.
|
||||
#
|
||||
# NOTE: To make this apply for all terminals (shells) you
|
||||
# open, simply call this method in each new instance.
|
||||
# This is the traditional method used by base16-shell;
|
||||
# however, it makes more sense to just change the
|
||||
# config file directly and avoid having to call an
|
||||
# external script every time you start a new terminal.
|
||||
#
|
||||
# NOTE: When using padding with terminals such as termite, the
|
||||
# background color for the terminal will take priority
|
||||
# over the background color for the shell. Therefore,
|
||||
# if you're using termite with padding, you should avoid
|
||||
# this method and simply use #set_terminal instead.
|
||||
#
|
||||
# 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(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"]
|
||||
cursor_color 12, ";7"
|
||||
end
|
||||
|
||||
private def add_color(num : Int32, color : YAML::Type)
|
||||
print "\033]4;#{num};rgb:#{color.to_s}\033\\"
|
||||
end
|
||||
|
||||
private def fgbg_color(num : Int32, color : YAML::Type)
|
||||
print "\033]#{num};rgb:#{color.to_s}\033\\"
|
||||
end
|
||||
|
||||
private def cursor_color(num : Int32, color : String)
|
||||
print "\033]#{num}#{color}\033\\"
|
||||
end
|
||||
|
||||
private def test_color(color : Symbol, text : String)
|
||||
print text.colorize(color)
|
||||
print " "
|
||||
print "___________________________".colorize.fore(color).back(color)
|
||||
print "\n"
|
||||
end
|
||||
|
||||
private def preview_color(color : YAML::Type, text : String)
|
||||
r : Int32 = color.to_s[0,2].to_i(16)
|
||||
g : Int32 = color.to_s[2,2].to_i(16)
|
||||
b : Int32 = color.to_s[4,2].to_i(16)
|
||||
print Trucolor.format({r, g, b}, "#{text} \x1b[48;2;#{r};#{g};#{b}m____________________________\x1b[0m\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) : 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
|
||||
|
||||
private def add_zcolor(type : String, color : YAML::Type) : String
|
||||
return "set #{type.ljust(30)} \"##{color.to_s}\"\n"
|
||||
end
|
||||
|
||||
private def add_css(key : Yaml::Type, value : YAML::Type)
|
||||
return "$#{key}: ##{value}"
|
||||
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
|
||||
end
|
Loading…
Reference in New Issue
Block a user