How to get font-names from bash output to GJS

I am trying to get font-names from bash output with in GJS.

When I run this command from terminal, I am getting font-names as output

fc-scan --format "%{family}\n" /usr/share/fonts

How can I run this command within GJS (from gnome-extension) and store in an array.

I am trying like below from gdm-extension(GitHub - PRATAP-KUMAR/gdm-extension: GDM Extension - Tweak few things of GDM Login Screen from the login screen itself)

import {spawnCommandLine} from 'resource:///org/gnome/shell/misc/util.js';

spawnCommandLine('fc-scan --format "%{family}\n" /usr/share/fonts >> ./fonts.txt');

Or is there any better way to get the list of font-names from GJS?

Thanks

Hi,

Indeed :slight_smile:

Usually we don’t query fonts from fontconfig directly, but from higher-level APIs like Pango.

I don’t know GJS specifically, but here a small example in Python:

import gi
gi.require_version('Pango', '1.0')
gi.require_version('Gtk', '4.0')
from gi.repository import Pango, Gtk

# Here I create a random widget, but preferably use an existing one
# (the font families should be the same for all widgets)
widget = Gtk.Label()

context = widget.get_pango_context()
for family in context.list_families():
    print(family.get_name())
    for face in family.list_faces():
        print("\t", face.describe().to_string())

You can import Pango in GJS the same way you import Gtk or GLib.

Thank you @gwillems. I will try to see if I can implement with Pango.

Hi @gwillems
I tried for some time and found it difficult for me to play with widgets since this extension purely relay on St.

I am trying to read file names with Gio.File and made it to work but there is a blocker with recursive file operation.

I am following this code File Operations | GNOME JavaScript and trying to modify it to read file names instead of deleting operation.

Just to test the original code in the link, I created a false directory structure to delete files recursively but getting error.

Unhandled promise rejection. To suppress this warning, add an error handler to your promise chain with .catch() or a try-catch block around your await expression. Stack trace of the failed promise:
_getFonts@file:///usr/local/share/gnome-shell/extensions/gdm-extension@pratap.fastmail.fm/gdmExtension.js:158:24
_subMenuFonts@file:///usr/local/share/gnome-shell/extensions/gdm-extension@pratap.fastmail.fm/gdmExtension.js:155:18
_init@file:///usr/local/share/gnome-shell/extensions/gdm-extension@pratap.fastmail.fm/gdmExtension.js:79:18

Looks like it complains that there is no code to manage potential file operation errors…

Sadly I don’t know GJS, can’t really help here… Maybe create another discussion to specifically ask about file operations in GJS, for more visibility.

Thank you. I created the post. How to recursively get all file names from a given folder using Gio.File?