<commit_before>require "gtk3"
require "json"
require "optparse"

class NSD
    def main
        options = parse_options(ARGV)

        io = GLib::IOChannel.new($stdin)
        watch_id = io.add_watch(GLib::IOChannel::IN) do |io, cond|
            begin
                line = io.readline().strip()
            rescue EOFError
                io.close()
                GLib::Source.remove(watch_id)
                return
            end
            if line == ""
                next true
            end
            if options.json
                begin
                    line = JSON::load(line)
                rescue JSON::ParserError => e
                    warn e
                    next true
                end
            end
            attrs = nil
            if options.markup
                begin
                    attrs, line = Pango::parse_markup(line)
                rescue GLib::Error => e
                    warn e
                    next true
                end
            end
            y = 50 + rand * (Gdk::default_root_window.screen.height - 100)
            Comment.new(
                line, attrs=attrs, font=options.font, duration=options.duration
            ).start(y)
        end
<commit_msg>quit if stdin was closed and all comments are disappeared<commit_after>require "gtk3"
require "json"
require "optparse"

class NSD
    def main
        options = parse_options(ARGV)

        comment_count = 0
        io_closed = false

        io = GLib::IOChannel.new($stdin)
        watch_id = io.add_watch(GLib::IOChannel::IN|GLib::IOChannel::HUP) do |io, cond|
            begin
                line = io.readline().strip()
            rescue EOFError
                io.close()
                GLib::Source.remove(watch_id)
                io_closed = true
                next true
            end
            if line == ""
                next true
            end
            if options.json
                begin
                    line = JSON::load(line)
                rescue JSON::ParserError => e
                    warn e
                    next true
                end
            end
            attrs = nil
            if options.markup
                begin
                    attrs, line = Pango::parse_markup(line)
                rescue GLib::Error => e
                    warn e
                    next true
                end
            end
            y = 50 + rand * (Gdk::default_root_window.screen.height - 100)
            comment = Comment.new(
                line, attrs=attrs, font=options.font, duration=options.duration
            )
            comment_count += 1

            comment.signal_connect('destroy') do |comment, event|
                comment_count -= 1

                if comment_count == 0 and io_closed
                    Gtk::main_quit()
                end
            end
            comment.start(y)
        end

{'fraction_non_alphanumeric': 0.07449364915894267, 'fraction_numerical': 0.0054926192928252664, 'mean_word_length': 1.0535588442565187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3410149', 'n_tokens_mistral': 767, 'n_tokens_neox': 710, 'n_words': 185}