Paepoi

Paepoi » Gjs Tips » Gjs 組み込み機能

Gjs 組み込み機能

# 最終更新日 2020.08.02

標準出力
標準入力は無い、GIOChannel を使おう
#!/usr/bin/gjs

// print で標準出力に出力される
print('標準出力');

// printerr は標準エラー出力に出力される
printerr('標準エラー');

// log も標準エラー出力、「Gjs-Message: 時間情報: JS_LOG:」の接頭子付き
log('ログ');
//=> Gjs-Message: 06:45:52.677: JS LOG: ログ

system
他に breakpoint, dumpHeap, gc, clearDataCaches がある
#!/usr/bin/gjs

const System = imports.system;
const GObject = imports.gi.GObject;
  
// 自身のパス、C 言語の argv[0] と同じ
print(System.programInvocationName);

// gjs --version コマンド同様、区切りは無い
print(System.version);

// object のアドレス
let obj = {}
print(System.addressOf(obj));

// GObject 派生な場合のアドレス、Gtk.Window 等もコレ
let gobj = new GObject.Object();
print(System.addressOfGObject(gobj));

// 参照カウンタ数
let x = gobj.ref();
print(System.refcount(x));
gobj.unref();
print(System.refcount(x));

// 終了
System.exit(0);
print('一行前で終了したのでコレは表示されない');

byteArray
UTF-8 バイト文字列と JavaScript 文字列の相互変換
#!/usr/bin/gjs

const ByteArray = imports.byteArray;

// Uint8Array に変換
let uint8array = ByteArray.fromString('SUZUKI刀');
for (let s of uint8array) print(String.fromCharCode(s));

// JavaScript 文字列に変換
let jsStr = ByteArray.toString(uint8array);
print(jsStr);

mainloop
GLib.MainLoop を単純表記できるようにしたもの
#!/usr/bin/gjs

const Mainloop = imports.mainloop;

let count = 0;

Mainloop.timeout_add(100, ()=> {
    if (count === 5) {
        print('__end__');
        Mainloop.quit();
        return false;
    }
    print(count);
    count++;
    return true;
});
Mainloop.run();

print('__done__');

Copyright(C) sasakima-nao All rights reserved 2002 --- 2023.