<commit_before>// Import Artist List as this can be huge

var artists = getArtistsArray();

function Player() // {{{
{
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////// PRIVATE SECTION /////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    // Get the focus to the visible playlist first
    //window._SBShowMainLibrary();

    // interval (milliseconds)
    function seek(interval, direction)
    {
        let position = gMM.playbackControl ? gMM.playbackControl.position : 0;
        player.seekTo(position + (direction ? interval : -interval));
    }

    function focusTrack(mediaItem)
    {
        SBGetBrowser().mediaTab.mediaPage.highlightItem(_SBGetCurrentView().getIndexForItem(mediaItem));
    }

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// MAPPINGS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    mappings.add([modes.PLAYER],
        ["x"], "Play track",
        function () { player.play(); });

    mappings.add([modes.PLAYER],
        ["z"], "Previous track",
        function () { player.previous(); });

    mappings.add([modes.PLAYER],
        ["c"], "Pause/unpause track",
        function () { player.togglePlayPause(); });

    mappings.add([modes.PLAYER],
        ["b"], "Next track",
        function () { player.next(); });

    mappings.add([modes.PLAYER],
        ["v"], "Stop track",
        function () { player.stop(); });

    mappings.add([modes.PLAYER],
        ["f"], "Filter library",
        function () { commandline.open(":", "filter ", modes.EX); });

    mappings.add([modes.PLAYER],
        ["F"], "Loads current view filtered by the keywords",
        function () { commandline.open(":", "Filter ", modes.EX); });

    mappings.add([modes.PLAYER],
        ["i"], "Select current track",
        function () { gSongbirdWindowController.doCommand("cmd_find_current_track"); });

    mappings.add([modes.PLAYER],
        ["s"], "Toggle shuffle",
        function () { player.toggleShuffle(); });

    mappings.add([modes.PLAYER],
        ["r"], "Toggle repeat",
        function () { player.toggleRepeat(); });

    mappings.add([modes.PLAYER],
        ["h", "<Left>"], "Seek -10s",
        function (count) { player.seekBackward(Math.max(1, count) * 10000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["l", "<Right>"], "Seek +10s",
        function (count) { player.seekForward(Math.max(1, count) * 10000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["H"], "Seek -1m",
        function (count) { player.seekBackward(Math.max(1, count) * 60000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["L"], "Seek +1m",
        function (count) { player.seekForward(Math.max(1, count) * 60000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
         ["=", "+"], "Increase volume by 10%",
         function () { player.increaseVolume(); });

    mappings.add([modes.PLAYER],
         ["-"], "Decrease volume by 10%",
         function () { player.decreaseVolume(); });

    ////////////////// ///////////////////////////////////////////////////////////}}}
    ////////////////////// COMMANDS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    // TODO: presumably this will eventually just filter the library view like cmus? --djk
    commands.add(["f[ilter]"],
        "Filter and play tracks",
        function (args)
        {
            // Store the old view
            // let prev_view = gMM.status.view;
            let library = LibraryUtils.mainLibrary;
            let mainView = library.createView();
            let sqncr = gMM.sequencer;
            let customProps = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
                                  .createInstance(Ci.sbIMutablePropertyArray);

            // args
            switch (args.length)
            {
                case 3:
                    customProps.appendProperty(SBProperties.trackName, args[2].toString());
                case 2:
                    customProps.appendProperty(SBProperties.albumName, args[1].toString());
                case 1:
                    customProps.appendProperty(SBProperties.artistName, args[0].toString());
                    break;
                default:
                    break;
            }

            sqncr.playView(mainView, mainView.getIndexForItem(library.getItemsByProperties(customProps).queryElementAt(0, Ci.sbIMediaItem)));
            player.focusPlayingTrack();
        },
        {
            argCount: "+",
            completer: function (context, args) completion.song(context, args)
        });

    commands.add(["F[ilter]"],
            "Filter tracks based on keywords {genre/artist/album/track}",
            function (args)
            {
                let library = LibraryUtils.mainLibrary;
                let myView = LibraryUtils.createStandardMediaListView(LibraryUtils.mainLibrary, args.string);
                if (myView.length == 0)
                    liberator.echoerr("No Tracks matching the keywords");
                else
                {
                     SBGetBrowser().loadMediaList(LibraryUtils.mainLibrary, null, null, myView,
                         "chrome://songbird/content/mediapages/filtersPage.xul");
                     // TODO: make this focusTrack work ?
                     focusTrack(myView.getItemByIndex(0));
                }
            },
            {
                argCount: "+",
                //completer: function (context, args) completion.tracks(context, args);
            });

    // TODO: better off as a single command (:player play) or cmus compatible (:player-play)? --djk
    commands.add(["playerp[lay]"],
        "Play track",
        function () { player.play(); });

    commands.add(["playerpa[use]"],
        "Pause/unpause track",
        function () { player.togglePlayPause(); });

    commands.add(["playern[ext]"],
        "Play next track",
        function () { player.next(); });

    commands.add(["playerpr[ev]"],
        "Play previous track",
        function () { player.previous(); });

    commands.add(["players[top]"],
        "Stop track",
        function () { player.stop(); });

    commands.add(["see[k]"],
        "Seek to a track position",
        function (args)<commit_msg>Add several player related autocommands.<commit_after>// Import Artist List as this can be huge

var artists = getArtistsArray();

function Player() // {{{
{
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////// PRIVATE SECTION /////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    // Get the focus to the visible playlist first
    //window._SBShowMainLibrary();

    // interval (milliseconds)
    function seek(interval, direction)
    {
        let position = gMM.playbackControl ? gMM.playbackControl.position : 0;
        player.seekTo(position + (direction ? interval : -interval));
    }

    function focusTrack(mediaItem)
    {
        SBGetBrowser().mediaTab.mediaPage.highlightItem(_SBGetCurrentView().getIndexForItem(mediaItem));
    }

    var mediaCoreListener = {
        onMediacoreEvent: function (event)
        {
            switch (event.type)
            {
                case Ci.sbIMediacoreEvent.BEFORE_TRACK_CHANGE:
                    liberator.log("Before track changed: " + event.data);
                    autocommands.trigger("TrackChangePre", { track: event.data });
                    break;
                case Ci.sbIMediacoreEvent.TRACK_CHANGE:
                    autocommands.trigger("TrackChange", { track: event.data });
                    break;
                case Ci.sbIMediacoreEvent.BEFORE_VIEW_CHANGE:
                    liberator.log("Before view changed: " + event.data);
                    autocommands.trigger("ViewChangePre", { view: event.data });
                    break;
                case Ci.sbIMediacoreEvent.VIEW_CHANGE:
                    liberator.log("View changed: " + event.data);
                    autocommands.trigger("ViewChange", { view: event.data });
                    break;
                case Ci.sbIMediacoreEvent.STREAM_START:
                    liberator.log("Track started: " + event.data);
                    autocommands.trigger("StreamStart", {});
                    break;
                case Ci.sbIMediacoreEvent.STREAM_PAUSE:
                    liberator.log("Track paused: " + event.data);
                    autocommands.trigger("StreamPause", {});
                    break;
                case Ci.sbIMediacoreEvent.STREAM_END:
                    liberator.log("Track ended: " + event.data);
                    autocommands.trigger("StreamEnd", {});
                    break;
                case Ci.sbIMediacoreEvent.STREAM_STOP:
                    liberator.log("Track stopped: " + event.data);
                    autocommands.trigger("StreamStop", {});
                    break;
            }
        }
    };

    gMM.addListener(mediaCoreListener);
    liberator.registerObserver("shutdown", function () {
        gMM.removeListener(mediaCoreListener);
    });

    /////////////////////////////////////////////////////////////////////////////}}}
    ////////////////////// MAPPINGS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    mappings.add([modes.PLAYER],
        ["x"], "Play track",
        function () { player.play(); });

    mappings.add([modes.PLAYER],
        ["z"], "Previous track",
        function () { player.previous(); });

    mappings.add([modes.PLAYER],
        ["c"], "Pause/unpause track",
        function () { player.togglePlayPause(); });

    mappings.add([modes.PLAYER],
        ["b"], "Next track",
        function () { player.next(); });

    mappings.add([modes.PLAYER],
        ["v"], "Stop track",
        function () { player.stop(); });

    mappings.add([modes.PLAYER],
        ["f"], "Filter library",
        function () { commandline.open(":", "filter ", modes.EX); });

    mappings.add([modes.PLAYER],
        ["F"], "Loads current view filtered by the keywords",
        function () { commandline.open(":", "Filter ", modes.EX); });

    mappings.add([modes.PLAYER],
        ["i"], "Select current track",
        function () { gSongbirdWindowController.doCommand("cmd_find_current_track"); });

    mappings.add([modes.PLAYER],
        ["s"], "Toggle shuffle",
        function () { player.toggleShuffle(); });

    mappings.add([modes.PLAYER],
        ["r"], "Toggle repeat",
        function () { player.toggleRepeat(); });

    mappings.add([modes.PLAYER],
        ["h", "<Left>"], "Seek -10s",
        function (count) { player.seekBackward(Math.max(1, count) * 10000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["l", "<Right>"], "Seek +10s",
        function (count) { player.seekForward(Math.max(1, count) * 10000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["H"], "Seek -1m",
        function (count) { player.seekBackward(Math.max(1, count) * 60000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
        ["L"], "Seek +1m",
        function (count) { player.seekForward(Math.max(1, count) * 60000); },
        { flags: Mappings.flags.COUNT });

    mappings.add([modes.PLAYER],
         ["=", "+"], "Increase volume by 10%",
         function () { player.increaseVolume(); });

    mappings.add([modes.PLAYER],
         ["-"], "Decrease volume by 10%",
         function () { player.decreaseVolume(); });

    ////////////////// ///////////////////////////////////////////////////////////}}}
    ////////////////////// COMMANDS ////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////{{{

    // TODO: presumably this will eventually just filter the library view like cmus? --djk
    commands.add(["f[ilter]"],
        "Filter and play tracks",
        function (args)
        {
            // Store the old view
            // let prev_view = gMM.status.view;
            let library = LibraryUtils.mainLibrary;
            let mainView = library.createView();
            let sqncr = gMM.sequencer;
            let customProps = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
                                  .createInstance(Ci.sbIMutablePropertyArray);

            // args
            switch (args.length)
            {
                case 3:
                    customProps.appendProperty(SBProperties.trackName, args[2].toString());
                case 2:
                    customProps.appendProperty(SBProperties.albumName, args[1].toString());
                case 1:
                    customProps.appendProperty(SBProperties.artistName, args[0].toString());
                    break;
                default:
                    break;
            }

            sqncr.playView(mainView, mainView.getIndexForItem(library.getItemsByProperties(customProps).queryElementAt(0, Ci.sbIMediaItem)));
            player.focusPlayingTrack();
        },
        {
            argCount: "+",
            completer: function (context, args) completion.song(context, args)
        });

    commands.add(["F[ilter]"],
            "Filter tracks based on keywords {genre/artist/album/track}",
            function (args)
            {
                let library = LibraryUtils.mainLibrary;
                let view = LibraryUtils.createStandardMediaListView(LibraryUtils.mainLibrary, args.literalArg);

                if (view.length == 0)
                    liberator.echoerr("No Tracks matching the keywords");
                else
                {
                     SBGetBrowser().loadMediaList(LibraryUtils.mainLibrary, null, null, view,
                                                     "chrome://songbird/content/mediapages/filtersPage.xul");
                     // TODO: make this focusTrack work ?
                     focusTrack(view.getItemByIndex(0));
                }
            },
            {
                argCount: "1",
                literal: 0
                //completer: function (context, args) completion.tracks(context, args);
            });

    // TODO: better off as a single command (:player play) or cmus compatible (:player-play)? --djk
    commands.add(["playerp[lay]"],
        "Play track",
        function () { player.play(); });

    commands.add(["playerpa[use]"],
        "Pause/unpause track",
        function () { player.togglePlayPause(); });

    commands.add(["playern[ext]"],
        "Play next track",
        function () { player.next(); });

    commands.add(["playerpr[ev]"],
        "Play previous track",
        function () { player.previous(); });

    commands.add(["players[top]"],
        "Stop track",
        function () { player.stop(); });

    commands.add(["see[k]"],
        "Seek to a track position",
        function (args)
{'fraction_non_alphanumeric': 0.21879626649501127, 'fraction_numerical': 0.005922111361441906, 'mean_word_length': 1.515136797798284, 'pattern_counts': {'":': 4, '<': 7, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5416013', 'n_tokens_mistral': 4127, 'n_tokens_neox': 3864, 'n_words': 750}