summaryrefslogtreecommitdiff
path: root/examples/gio-cat.js
blob: 72d9c2d81b6b7458ccb956d6451cdf8748495648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2010 litl, LLC

import GLib from 'gi://GLib';
import Gio from 'gi://Gio';

let loop = GLib.MainLoop.new(null, false);
const decoder = new TextDecoder();

function cat(filename) {
    let f = Gio.file_new_for_path(filename);
    f.load_contents_async(null, (obj, res) => {
        let contents;
        try {
            contents = obj.load_contents_finish(res)[1];
        } catch (err) {
            logError(err);
            loop.quit();
            return;
        }
        print(decoder.decode(contents));
        loop.quit();
    });

    loop.run();
}

if (ARGV.length !== 1)
    printerr('Usage: gio-cat.js filename');
else
    cat(ARGV[0]);