Last active
February 1, 2024 12:54
-
-
Save hholst80/0f39580088857b7b0d9173181174bf2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
#include <unistd.h> // Necessary for the C.isatty function | |
fn main() { | |
// Define ANSI color codes | |
// mut ansi_codes := map[string]string | |
mut ansi_codes := { | |
'reset': '\x1b[0m' | |
'red': '\x1b[31m' | |
'green': '\x1b[32m' | |
'yellow': '\x1b[33m' | |
'blue': '\x1b[34m' | |
'magenta': '\x1b[35m' | |
'cyan': '\x1b[36m' | |
'white': '\x1b[37m' | |
'bold': '\x1b[1m' | |
'italics': '\x1b[3m' | |
'underline': '\x1b[4m' | |
'blink': '\x1b[5m' | |
} | |
result := os.execute('fc-list :spacing=mono') | |
if C.isatty(1) == 0 { | |
for key in ansi_codes.keys() { | |
ansi_codes[key] = '' | |
} | |
} | |
if result.exit_code != 0 { | |
eprintln('Command execution failed with exit code: ${result.exit_code}') | |
eprintln('Error Output:\n${result.output}') | |
exit(1) | |
} | |
lines := result.output.split('\n') | |
mut fonts := map[string]map[string]bool{} | |
for line in lines { | |
if line == '' { | |
continue | |
} | |
_, opts := line.split_once(': ') or { | |
eprintln('error: unexpected output from fc-list: ${line}') | |
exit(1) | |
} | |
names, styles := opts.split_once(':style=') or { | |
// eprintln('error: unexpected output from fc-list: missing "style" field: ${line}') | |
// exit(1) | |
// Just skip these | |
continue | |
} | |
main_name, _ := names.split_once(',') or { names, '' } | |
assert main_name != '' | |
for style in styles.split(',') { | |
fonts[main_name][style] = true | |
} | |
} | |
mut font_names := fonts.keys() | |
font_names.sort_with_compare(fn (a &string, b &string) int { | |
return a.to_lower().compare(b.to_lower()) | |
}) | |
for font_name in font_names { | |
println('${ansi_codes['bold']}${ansi_codes['green']}${font_name}${ansi_codes['reset']}') | |
mut styles := fonts[font_name].keys() | |
styles.sort() | |
for style in styles { | |
print(ansi_codes['italics']) | |
println(' ${font_name} ${style}${ansi_codes['reset']}') | |
} | |
println('') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment