Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract language definitions into data file #699

Merged
merged 40 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8f71b69
Export languages as JSON
spenserblack Jul 4, 2022
094bfa7
Make language name the JSON keys
spenserblack Jul 4, 2022
2142e42
Alphabetize JSON
spenserblack Jul 4, 2022
d0eb796
Use Tokei value names as JSON keys
spenserblack Jul 4, 2022
01da2c9
Add ASCII filenames to JSON
spenserblack Jul 4, 2022
cee37f7
Clean JSON
spenserblack Jul 4, 2022
c8fe473
Prettify JSON more
spenserblack Jul 4, 2022
7eb1ead
Generate language data with build script
spenserblack Jul 4, 2022
390e2b1
Cleanup unneeded JS files
spenserblack Jul 5, 2022
7e34cea
Update CODEOWNERS
spenserblack Jul 5, 2022
8cd1fcf
Remove unneeded TODO
spenserblack Jul 5, 2022
92745e0
Fix clippy errors on generated tests
spenserblack Jul 5, 2022
f6353e0
Remove paste dependency
spenserblack Jul 5, 2022
c014c6b
Fix alphabetization of languages
spenserblack Jul 5, 2022
b76784e
Add back clap serialization macro
spenserblack Jul 5, 2022
49c1d1b
Add back language chips/circles
spenserblack Jul 5, 2022
372c0b6
Clarify TODO
spenserblack Jul 5, 2022
e1e09c2
Remove redundant `name:` fields
spenserblack Jul 5, 2022
c824643
Update `CONTRIBUTING.md`
spenserblack Jul 5, 2022
5d7bfc0
Add script to create YAML with embedded ASCII
spenserblack Jul 6, 2022
713e4f6
Embed ASCII in YAML
spenserblack Jul 6, 2022
fe3ce50
Remove temporary script
spenserblack Jul 6, 2022
9e15944
Update CONTRIBUTING.md
spenserblack Jul 6, 2022
8d6841b
Place template alongside source code
spenserblack Jul 7, 2022
4988dcb
Fix trailing space after template `if`
spenserblack Jul 7, 2022
c745b64
Add script to convert RGB to hex
spenserblack Jul 7, 2022
9f02ad7
Use hex values in `languages.yaml`
spenserblack Jul 7, 2022
5be782b
Rename `default` to `white`, remap to `Default` with filter
spenserblack Jul 8, 2022
cd75fb0
Fix ASCII formatting mangled by hex remapping
spenserblack Jul 8, 2022
7f47c79
Fix clippy warnings
spenserblack Jul 8, 2022
98b9f9a
Simplify remapping white to default
spenserblack Jul 8, 2022
2086a88
small cleanup of build.rs
Jul 8, 2022
3636cda
revert to env::var
Jul 8, 2022
331d1fe
cargo fmt
Jul 8, 2022
3e759a8
fix markdown logo
Jul 8, 2022
9ed7fc9
missing serialization attrs
Jul 8, 2022
8c0a34b
Update CONTRIBUTING.md
o2sh Jul 8, 2022
5e1787e
remove trailing empty lines
o2sh Jul 8, 2022
c8d19d4
Merge branch 'dev/json-languages' of github.com:spenserblack/onefetch…
o2sh Jul 8, 2022
927321e
remove table of contents from contributing.md
o2sh Jul 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/.github/ @spenserblack @o2sh
/src/ui/image_backends/ @CephalonRho @yoichi
/src/info/repo.rs @Byron
/src/info/langs/ @spenserblack
/languages.yaml @spenserblack @o2sh
/templates/language.rs @spenserblack
/src/info/deps/ @HallerPatrick @o2sh
/src/cli.rs @spenserblack @o2sh
*.ascii @o2sh
88 changes: 45 additions & 43 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
# How to Contribute

- [How to Contribute](#how-to-contribute)
- [Contributing via Pull Requests](#contributing-via-pull-requests)
- [Adding support for a new language](#adding-support-for-a-new-language)
- [Ascii logo](#ascii-logo)
spenserblack marked this conversation as resolved.
Show resolved Hide resolved
- [Adding support for a new package manager](#adding-support-for-a-new-package-manager)
- [Adding translation for README.md](#adding-translation-for-readmemd)

## Contributing via Pull Requests

### Adding support for a new language

Adding support for a new Language consists in adding a new entry to the `define_language!` macro in [language.rs](src/info/langs/language.rs) and filling it in with the right data.
Adding support for a new Language consists in adding a new entry to [language.yaml](./languages.yaml) and filling it in with the right data.

**Example**:

`{ TypeScript, Programming, "typescript.ascii", define_colors!([Cyan, White] : [Rgb(0, 122, 204), Rgb(255, 255, 255)]), Rgb(43, 116, 137) }`

The first item `TypeScript` corresponds to the name of the language as defined in [tokei](https://github.com/XAMPPRocky/tokei). The second item refers to the language type as specified by [linguist](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml), only four values are possible: Programming, Markup, Prose and Data. The third item `typescript.ascii` is the name of the file containing the ascii logo: this file has to be placed in the _./resources_ folder (more info below). Then we have the colors used to customize the look of the ascii logo when displayed to the screen. Finally, the circle color used in the language distribution whose hex value can be found in [linguist](https://github.com/github/linguist/blob/master/lib/linguist/languages.yml).

#### Ascii logo

```text
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTS{1}TSTSTSTSTSTSTS{0}TSTS{1}TSTSTS{0}TSTSTS
{0}TSTSTSTS{1}TSTSTSTSTSTSTS{0}TS{1}TSTSTSTSTS{0}TSTS
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTST{1}TSTST{0}TSTSTSTST
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTS{1}TSTST{0}STSTSTST
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTSTS{1}TSTST{0}STSTST
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTSTSTS{1}TSTST{0}TSTS
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTSTSTST{1}TSTST{0}TST
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTS{1}TSTSTSTSTS{0}TST
{0}TSTSTSTSTSTST{1}TSTS{0}TSTSTSTSTS{1}TSTSTS{0}STSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
{0}TSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTS
```
CSharp: # required, this will be the name of the enum variant for the language as specified by tokei (link 1)
type: programming # required, can be programming, data, markup, or prose as specified by linguist (link 2)

# required, this is the logo. If it's not within 25x40 bounds, you will get a compiler error. Use `{i}` to color the ascii with `i` the color index.
ascii: |
{0} ++++++
{0} ++++++++++++
{0} ++++++++++++++++++++
{0} ++++++++++++++++++++++++++
{0} ++++++++++++++++++++++++++++++++
{0} +++++++++++++{3}************{0}+++++++++++++
{0}+++++++++++{3}******************{0}++++++++{2};;;
{0}+++++++++{3}**********************{0}++{2};;;;;;;
{0}++++++++{3}*********{0}++++++{3}******{2};;;;;;;;;;;
{0}+++++++{3}********{0}++++++++++{3}**{2};;;{3}**{2};;;{3}**{2};;;
{0}+++++++{3}*******{0}+++++++++{2};;;;;;{3}*********{2}::
{0}+++++++{3}******{0}+++++++{2};;;;;;;;;;{3}**{2};;;{3}**{2};;;
{0}+++++++{3}*******{0}+++{1}:::::{2};;;;;;;{3}*********{2};;
{0}+++++++{3}********{1}::::::::::{3}**{2};;;{3}**{2};;;{3}**{2};;;
{0}++++++++{3}*********{1}::::::{3}******{2};;;;;;;;;;;
{0}++++++{1}:::{3}**********************{1}::{2};;;;;;;
{0}+++{1}::::::::{3}******************{1}::::::::{2};;;
{1} :::::::::::::{3}************{1}:::::::::::::
{1} ::::::::::::::::::::::::::::::::
{1} ::::::::::::::::::::::::::
{1} ::::::::::::::::::::
{1} ::::::::::::
{1} ::::::
colors:
ansi: # required, a list of the ANSI colors used to colorize the logo
- blue
- magenta
- magenta
- white
hex: # optional, alternative to basic colors for terminals that support true colour.
- '#9B4F97'
- '#67217A'
- '#803788'
- '#FFFFFF'
chip: '#178600' # required, this is used for the language breakdown bar, its value can be found in linguist (link 2).
serialization: c# # required only if the Enum name `CSharp` doesn't match the display name `C#`
```

Remarks:

- The ascii logo's dimensions must fall below `25*40` (height\*width); The CI will fail otherwise.
- Use `{i}` to color the ascii with `i` the color index from the `vec!` of colors defined in `define_language!`.
- Make sure to trim any unnecessary trailing whitespaces.
- Optionally, you may provide a `vec!` of colors in `rgb` format as an alternative to basic colors for terminals that support [true colour](https://gist.github.com/XVilka/8346728).
- link 1: https://github.com/XAMPPRocky/tokei#supported-languages
- link 2: https://github.com/github/linguist/blob/master/lib/linguist/languages.yml

### Adding support for a new package manager

Expand Down
10 changes: 2 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ ansi_term = "0.12.1"
base64 = "0.13.0"
libc = "0.2.126"

[dev-dependencies]
more-asserts = "0.3.0"
paste = "1.0.7"
[build-dependencies]
lazy_static = "1"
regex = "1"
serde_json = "1"
serde_yaml = "0.8"
tera = { version = "1", default-features = false }

[features]
fail-on-deprecated = []
74 changes: 74 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::fs::{self, File};
use std::path::Path;
use tera::{Context, Tera};

fn main() -> Result<(), Box<dyn Error>> {
let out_dir = env::var("OUT_DIR").expect("No OUT_DIR variable.");
let mut tera = Tera::default();
tera.register_filter("strip_color_tokens", strip_color_tokens_filter);
tera.register_filter("hex_to_rgb", hex_to_rgb_filter);

let lang_data: serde_json::Value = serde_yaml::from_reader(File::open("languages.yaml")?)?;

let output_path = Path::new(&out_dir).join("language.rs");

let rust_code = tera.render_str(
&std::fs::read_to_string("src/info/langs/language.tera.rs")?,
&Context::from_value(serde_json::json!({ "languages": lang_data, }))?,
)?;
fs::write(&output_path, rust_code)?;

Ok(())
}

/// Strips out `{n}` from the given string.
fn strip_color_tokens_filter(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
lazy_static! {
static ref COLOR_INDEX_REGEX: Regex = Regex::new(r"\{\d+\}").unwrap();
}
let s = match value {
tera::Value::String(s) => s,
_ => return Err(tera::Error::msg("expected string")),
};
return Ok(tera::Value::String(
COLOR_INDEX_REGEX.replace_all(s, "").to_string(),
));
}

fn hex_to_rgb_filter(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let hex_string = match value {
tera::Value::String(s) => s,
_ => return Err(tera::Error::msg("expected string")),
};
let hex_string = match hex_string.strip_prefix('#') {
Some(s) => s,
None => return Err(tera::Error::msg("expected hex string starting with `#`")),
};
if hex_string.len() != 6 {
return Err(tera::Error::msg("expected a 6 digit hex string"));
}
let channel_bytes = match u32::from_str_radix(hex_string, 16) {
Ok(n) => n,
Err(_) => return Err(tera::Error::msg("expected a valid hex string")),
};
let r = (channel_bytes >> 16) & 0xFF;
let g = (channel_bytes >> 8) & 0xFF;
let b = channel_bytes & 0xFF;

Ok(serde_json::json!({
"r": r,
"g": g,
"b": b,
}))
}
Loading