Skip to content

Instantly share code, notes, and snippets.

@duboisf
Created February 23, 2024 02:11
Show Gist options
  • Save duboisf/7136e531af70d8730c73603b967b2cba to your computer and use it in GitHub Desktop.
Save duboisf/7136e531af70d8730c73603b967b2cba to your computer and use it in GitHub Desktop.
Fix nerdfont moved codepoints

Small app to detect and show fixed Nerfont moved codepoints

I wrote versions in golang and rust.

With golang it was a walk in the park but I've writen a lot of go.

I just started learning rust and oh boy did I struggle 😅

nushell script to watch for changes

While writting the golang version, I was using this nu snippet to build and run the app automatially:

watch . { |event|
    if $event == Create {
        go build
        let cwd = pwd
        do {
            cd ~/.dotfiles
            git ls-files
                | lines
                | each {ls $in}
                | flatten
                | where type == file
                | each { ^($cwd | path join codepoints) $in.name }
        }
    }
}

Screenshot of running the golang version

image

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func Main(filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
// make sure filename is a file
if fileInfo, err := file.Stat(); err != nil {
return fmt.Errorf("failed to get file info: %w", err)
} else if fileInfo.IsDir() {
return fmt.Errorf("file is a directory")
}
fileScanner := bufio.NewScanner(file)
fileScanner.Split(bufio.ScanLines)
lineCount := 0
for fileScanner.Scan() {
line := fileScanner.Text()
lineCount++
charCount := 0
for _, c := range line {
charCount++
if c >= 0xf500 && c <= 0xfd46 {
fixedLine := strings.ReplaceAll(line, string(c), string(c+0xe0b01))
fmt.Printf("%s: found moved codepoint %#U at line %d, char %d:\n", filename, c, lineCount, charCount)
fmt.Printf("%s (original line)\n", line)
fmt.Printf("%s󰹺\n", strings.Repeat(" ", charCount-1))
fmt.Printf("%s (fixed line)\n\n", fixedLine)
}
}
}
return nil
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "USAGE: %s <filename>\n", os.Args[0])
os.Exit(1)
}
if err := Main(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
use std::{
env::args,
fs::File,
io::{BufRead, BufReader},
};
fn main() {
if args().len() != 2 {
eprintln!("Usage: {} <file>", args().next().unwrap());
std::process::exit(1);
}
let filename = args().nth(1).unwrap();
if std::path::Path::new(&filename).is_file() == false {
eprintln!("{}: not a file", filename);
std::process::exit(1);
}
let file = match File::open(&filename) {
Ok(file) => file,
Err(err) => {
eprintln!("Error opening file: {}", err);
std::process::exit(1);
}
};
let buffer = BufReader::new(file);
buffer.lines().enumerate().for_each(|(i, line)| match line {
Ok(line) => {
line.chars().enumerate().for_each(|(j, c)| match c {
'\u{f500}'..='\u{fd46}' => {
print!(
"{}: codepoint 0x{:x} at line {}, char {}, (new codepoint: 0x{:x}):\n",
filename,
c as i32,
i + 1,
j + 1,
c as i32 + 0xe0b01
);
print!("{}\n{}🡅 \n", line, " ".repeat(j));
print!(
"replaced string:\n{}🡇 \n{}\n",
" ".repeat(j),
line.replace(
c.to_string().as_str(),
std::char::from_u32(c as u32 + 0xe0b01)
.unwrap()
.to_string()
.as_str()
),
);
}
_ => (),
});
}
Err(err) => eprintln!("{}: error reading line: {}", filename, err),
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment