Skip to content

Commit

Permalink
simplify retrieving colors from palette in AlacrittyTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 27, 2024
1 parent 1e46e42 commit 32b19b3
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,12 @@ impl<'a> AlacrittyTheme<'a> {
}
}

fn color(&self, name: &str) -> &'_ str {
self.palette[name].gui.normal()
}

fn write_header_comment(&self, w: &mut impl Write) -> io::Result<()> {
write!(
writeln!(
w,
r#"# Alacritty theme for spring-night colorscheme
#
Expand All @@ -903,34 +907,33 @@ impl<'a> AlacrittyTheme<'a> {
# Copyright (c) 2016 rhysd
#
# PLEASE DO NOT MODIFY THIS FILE DIRECTLY!
# Generated by script vim-color-spring-night/gen/{}
"#,
file!(),
# Generated by script vim-color-spring-night/gen/{file}"#,
file = file!(),
)
}

#[rustfmt::skip]
fn write_primary_section(&self, w: &mut impl Write) -> io::Result<()> {
writeln!(w)?;
writeln!(w, "[colors.primary]")?;
writeln!(w, "background = \"{}\"", &self.palette[self.background].gui.normal())?;
writeln!(w, "foreground = \"{}\"", &self.palette[self.normal.foreground].gui.normal())?;
writeln!(w, "dim_foreground = \"{}\"", &self.palette[self.dim.foreground].gui.normal())?;
writeln!(w, "bright_foreground = \"{}\"", &self.palette[self.bright.foreground].gui.normal())
writeln!(w, "background = \"{}\"", &self.color(self.background))?;
writeln!(w, "foreground = \"{}\"", &self.color(self.normal.foreground))?;
writeln!(w, "dim_foreground = \"{}\"", &self.color(self.dim.foreground))?;
writeln!(w, "bright_foreground = \"{}\"", &self.color(self.bright.foreground))
}

#[rustfmt::skip]
fn write_colors_section(&self, w: &mut impl Write, colors: &AlacrittyFgColors<'a>) -> io::Result<()> {
writeln!(w)?;
writeln!(w, "[colors.{}]", colors.name)?;
writeln!(w, "black = \"{}\"", &self.palette[colors.black].gui.normal())?;
writeln!(w, "red = \"{}\"", &self.palette[colors.red].gui.normal())?;
writeln!(w, "green = \"{}\"", &self.palette[colors.green].gui.normal())?;
writeln!(w, "yellow = \"{}\"", &self.palette[colors.yellow].gui.normal())?;
writeln!(w, "blue = \"{}\"", &self.palette[colors.blue].gui.normal())?;
writeln!(w, "magenta = \"{}\"", &self.palette[colors.magenta].gui.normal())?;
writeln!(w, "cyan = \"{}\"", &self.palette[colors.cyan].gui.normal())?;
writeln!(w, "white = \"{}\"", &self.palette[colors.white].gui.normal())
writeln!(w, "[colors.{}]", colors.name)?;
writeln!(w, "black = \"{}\"", &self.color(colors.black))?;
writeln!(w, "red = \"{}\"", &self.color(colors.red))?;
writeln!(w, "green = \"{}\"", &self.color(colors.green))?;
writeln!(w, "yellow = \"{}\"", &self.color(colors.yellow))?;
writeln!(w, "blue = \"{}\"", &self.color(colors.blue))?;
writeln!(w, "magenta = \"{}\"", &self.color(colors.magenta))?;
writeln!(w, "cyan = \"{}\"", &self.color(colors.cyan))?;
writeln!(w, "white = \"{}\"", &self.color(colors.white))
}

fn write_search_section(&self, w: &mut impl Write) -> io::Result<()> {
Expand All @@ -939,18 +942,17 @@ impl<'a> AlacrittyTheme<'a> {
writeln!(
w,
r#"matches = {{ foreground = "{fg}", background = "{bg}" }}"#,
fg = &self.palette[self.normal.foreground].gui.normal(),
bg = &self.palette[self.search_background].gui.normal(),
fg = &self.color(self.normal.foreground),
bg = &self.color(self.search_background),
)?;
writeln!(
w,
r#"focused_match = {{ foreground = "{fg}", background = "{bg}" }}"#,
fg = &self.palette[self.bright.foreground].gui.normal(),
bg = &self.palette[self.search_focus_background].gui.normal(),
fg = &self.color(self.bright.foreground),
bg = &self.color(self.search_focus_background),
)
}

#[rustfmt::skip]
fn write_to(&self, w: &mut impl Write) -> io::Result<()> {
self.write_header_comment(w)?;
self.write_primary_section(w)?;
Expand All @@ -965,9 +967,9 @@ fn write_to_files(dir: &str) -> Result<()> {
let palette = Palette::default();

fn join(entries: &[&str]) -> PathBuf {
let mut it = entries.iter();
let mut path = PathBuf::from(it.next().unwrap());
for entry in it {
let mut entries = entries.iter();
let mut path = PathBuf::from(entries.next().unwrap());
for entry in entries {
path.push(entry);
}
path
Expand All @@ -978,21 +980,21 @@ fn write_to_files(dir: &str) -> Result<()> {
.with_context(|| format!("Could not create colorscheme file: {:?}", &path))?;
Colorscheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("While generate colorscheme file {:?}", &path))?;
.with_context(|| format!("Could not write to colorscheme file {:?}", &path))?;

let path = join(&[dir, "autoload", "airline", "themes", "spring_night.vim"]);
let file = File::create(&path)
.with_context(|| format!("Could not create airline theme file {:?}", &path))?;
AirlineTheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("Could not generate airline theme file {:?}", &path))?;
.with_context(|| format!("Could not write to airline theme file {:?}", &path))?;

let path = join(&[dir, "alacritty", "spring_night.toml"]);
let file = File::create(&path)
.with_context(|| format!("Could not create alacritty theme file {:?}", &path))?;
AlacrittyTheme::new(&palette)
.write_to(&mut BufWriter::new(file))
.with_context(|| format!("Could not generate alacritty theme file {:?}", &path))
.with_context(|| format!("Could not write to alacritty theme file {:?}", &path))
}

fn write_to_stdout() -> Result<()> {
Expand All @@ -1001,15 +1003,15 @@ fn write_to_stdout() -> Result<()> {

Colorscheme::new(&palette)
.write_to(&mut stdout)
.context("While writing colorscheme to stdout")?;
.context("Could not write colorscheme to stdout")?;
writeln!(stdout)?;
AirlineTheme::new(&palette)
.write_to(&mut stdout)
.context("While writing airline theme to stdout")?;
.context("Could not write airline theme to stdout")?;
writeln!(stdout)?;
AlacrittyTheme::new(&palette)
.write_to(&mut stdout)
.context("While writing alacritty theme to stdout")
.context("Could not write alacritty theme to stdout")
}

fn main() -> Result<()> {
Expand All @@ -1025,7 +1027,7 @@ fn main() -> Result<()> {

let matches = opts
.parse(args)
.context("Please use --help option for more detail")?;
.context("Please try --help for more detail")?;

if matches.opt_present("h") {
let brief = &format!("Usage: {} [options]", program);
Expand Down

0 comments on commit 32b19b3

Please sign in to comment.