Paste #67314: Tab-Complete

Date: 2020/04/10 21:03:31 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


# @ ███████████████████████████████████████████████████████████
# @ ██    Command Dependencies | Tab Completion
# % ██
# % ██  [ Usage ]   tab-completes all online players by their name, filters as you type:
# - ██  [  # 1  ] - determine <proc[Online_Player_Tabcomplete]>
# % ██  [       ]
# % ██  [       ]   tab completes all players online by their name for the SECOND arg
# - ██  [  # 2  ] - determine <proc[Online_Player_Tabcomplete].context[2]>
# % ██  [       ]
# % ██  [       ]   tab completes all players online by their name for the first arg, blacklisting yourself
# - ██  [  # 3  ] - determine <proc[Online_Player_Tabcomplete].context[1|<player>]>
# % ██  [       ]   tab completes all players online by their name for the first arg, blacklisting players flagged "Admin"
# % ██  [       ]   the Blacklist must be escaped if it is a list of players
# - ██  [ # 4.1 ] - determine <proc[Online_Player_Tabcomplete].context[1|<server.list_players_flagged[Admin].escaped>]>
# % ██  [       ]   OR you can inject the script directly as opposed to using the procedure tag:
# % ██  [       ]   tab completes all players by their name for the first arg, blacklisting online players flagged "Admin"
# - ██  [  #4.2 ] - define blacklist <server.list_online_players_flagged[Admin]>
# - ██  [       ] - inject Online_Player_Tabcomplete
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the second arg, and blacklisting online players flagged "Admin"
# - ██  [  # 5  ] - define iArg 2
# - ██  [       ] - define blacklist <server.list_online_players_flagged[Admin]>
# - ██  [       ] - inject Online_Player_Tabcomplete
Online_Player_Tabcomplete:
    type: procedure
    debug: false
    definitions: iArg|Blacklist
    script:
        - if !<[iArg].exists>:
            - define iArg 1
        - if <context.args.size> == <[iArg].sub[1]>:
            - determine <server.list_online_players.exclude[<[Blacklist].unescaped||null>].parse[name]>
        - else if <context.args.size> == <[iArg]> && !<context.raw_args.ends_with[<&sp>]>:
            - determine <server.list_online_players.exclude[<[Blacklist].unescaped||null>].parse[name].filter[starts_with[<context.args.get[<[iArg]>]>]]>



# % ██  [ Usage ]   tab-completes all players by their name, filters as you type:
# - ██  [  # 1  ] - determine <proc[All_Player_Tabcomplete]>
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the SECOND arg
# - ██  [  # 2  ] - determine <proc[All_Player_Tabcomplete].context[2]>
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the first arg, blacklisting yourself
# - ██  [  # 3  ] - determine <proc[All_Player_Tabcomplete].context[1|<player>]>
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the first arg, blacklisting players flagged "Admin"
# % ██  [       ]   the Blacklist must be escaped if it is a list of players
# - ██  [ # 4.1 ] - determine <proc[All_Player_Tabcomplete].context[1|<server.list_players_flagged[Admin].escaped>]>
# % ██  [       ]   OR you can inject the script directly as opposed to using the procedure tag:
# % ██  [       ]   tab completes all players by their name for the first arg, blacklisting players flagged "Admin"
# - ██  [  #4.2 ] - define blacklist <server.list_players_flagged[Admin]>
# - ██  [       ] - inject All_Player_Tabcomplete
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the second arg, and blacklisting players flagged "Admin"
# - ██  [  # 5  ] - define iArg 2
# - ██  [       ] - define blacklist <server.list_players_flagged[Admin]>
# - ██  [       ] - inject All_Player_Tabcomplete
All_Player_Tabcomplete:
    type: procedure
    debug: false
    definitions: iArg|Blacklist
    script:
        - if !<[iArg].exists>:
            - define iArg 1
        - if <context.args.size> == <[iArg].sub[1]>:
            - determine <server.list_players.exclude[<[Blacklist].unescaped||null>].parse[name]>
        - else if <context.args.size> == <[iArg]> && !<context.raw_args.ends_with[<&sp>]>:
            - determine <server.list_players.exclude[<[Blacklist].unescaped||null>].parse[name].filter[starts_with[<context.args.get[<[iArg]>]>]]>



# % ██  [ Usage ]   Tab-completes a list of a values from an escaped ListTag - List must be escaped
# - ██  [  #1   ] - determine <proc[OneArg_Command_Tabcomplete].context[1|<list[Option1|Option2|Option3].escaped>]>
# % ██  [       ]
# % ██  [       ]   tab completes all players by their name for the SECOND arg
# - ██  [  #2   ] - determine <proc[OneArg_Command_Tabcomplete].context[2]>
# % ██  [       ]
# % ██  [       ]   This can also be injected to avoid escaping the list:
# - ██  [       ] - define Arg <list[option1|option2|option3]>
# - ██  [       ] - inject OneArg_Command_Tabcomplete Instantly
OneArg_Command_Tabcomplete:
    type: procedure
    debug: false
    definitions: iArg|Args
    script:
        - if !<[iArg].exists>:
            - define iArg 1
        - if <context.args.size||0> == <[iArg].sub[1]>:
            - determine <[Args].unescaped>
        - else if <context.args.size> == <[iArg]> && !<context.raw_args.ends_with[<&sp>]>:
            - determine <[Args].unescaped.filter[starts_with[<context.args.get[<[iArg]>]>]]>



# % ██  [ Notes ] Tab-completes a list of options for a numbered list of args
# % ██  [       ] Note: definitions must be numbered "Arg1, Arg2, ..." - Skips if undefined
# % ██  [       ] Example usage for a command describing your pizza:
# - ██  [ Usage ] - define Arg1 <list[small|medium|large]>
# - ██  [       ] - define Arg2 <list[thin|handtossed|brooklyn]>
# - ██  [       ] - define Arg3 <list[redsauce|bbq|alfredo]>
# - ██  [       ] - define Arg4 <list[pepperonies|sausage|pineapples|none]>
# - ██  [       ] - inject MultiArg_Command_Tabcomplete Instantly
MultiArg_Command_Tabcomplete:
    type: task
    debug: false
    script:
        - if <context.args.size> == 0:
            - determine <[Arg1]>
        - foreach <context.args> as:Arg:
            - if <[Loop_Index]> == <context.args.size>:
                - if !<context.raw_args.ends_with[<&sp>]>:
                    - determine <[Arg<[Loop_Index]>].filter[starts_with[<context.args.get[<[Loop_Index]>]>]]>
                - else if <[Arg<[Loop_Index].add[1]>].exists>:
                    - determine <[Arg<[Loop_Index].add[1]>]>
            - else:
                - foreach next


# % ██  [ Notes ] Tab-completes a list of options for any list of args, including sub-args
# % ██  [       ] First arg is required, every arg afterwards is optional, including deciding between args, or sub-args from the previous arg
# % ██  [       ] Sub-Args are defined as Arg#<ARGNAME>Args - where the # is the arg number and <ARGNAME> is the name from Arg# you want to have sub-args
# - ██  [  # 1  ] - define Arg1 <list[Option1|Option2]>
# - ██  [  # 1  ] - define Arg2Option1Args <list[Option3|Option4]>
# - ██  [  # 1  ] - define Arg2Option2Args <list[Option5|Option6]>
# % ██  [       ] Example usage for a command describing your pizza from a shop that restricts the toppings based on your pizza size,
# % ██  [       ]   and only letting you add a secondary topping if you opted to get red-sauce:
# - ██  [ Usage ] - define Arg1 <list[small|medium|large]>
# - ██  [       ] - define Arg2SmallArgs <list[Peppernoies|Sausage]>
# - ██  [       ] - define Arg2MediumArgs <list[Pepperonies|Sausage|Ham|Chicken]>
# - ██  [       ] - define Arg2LargeArgs <list[Pepperonies|Sausage|Ham|Chicken|Pineapples|Bacon]>
# - ██  [       ] - define Arg3 <list[RedSauce|bbq|alfredo]>
# - ██  [       ] - define Arg4RedSauceArgs <list[Pepperonies|Sausage|Pineapples]>
# - ██  [       ] - inject MultiArg_With_MultiArgs_Command_Tabcomplete Instantly
# % ██  [ Notes ] Tab-completes a list of options for a numbered list of args with specific args per args
MultiArg_With_MultiArgs_Command_Tabcomplete:
    type: task
    debug: true
    script:
        - if <context.args.size> == 0:
            - determine <[Arg1]>
        - foreach <context.args> as:Arg:
            - if <[Loop_Index]> == <context.args.size>:
                - if !<context.raw_args.ends_with[<&sp>]>:
                    - if <[Arg<[Loop_Index]>].exists>:
                        - determine <[Arg<[Loop_Index]>].filter[starts_with[<context.args.get[<[Loop_Index]>]>]]>
                    - if  <[Arg<[Loop_Index].sub[1]>].contains[<context.args.get[<[Loop_Index].sub[1]>]>]>:
                        - define Option <context.args.get[<[Loop_Index].sub[1]>]>:
                        - if <[Arg<[loop_index]><[Option]>Args].exists>:
                            - determine <[Arg<[Loop_Index]><[Option]>Args].filter[starts_with[<context.args.last>]]>
                - else if <[Arg<[Loop_Index].add[1]>].exists>:
                    - determine <[Arg<[Loop_Index].add[1]>]>
                - else if <[Arg<[Loop_Index]>].contains[<context.args.last>]>:
                    - if <[Arg<[Loop_Index].add[1]><context.args.last>Args].exists>:
                        - determine <[Arg<[Loop_Index].add[1]><context.args.last>Args]>
            - else:
                - foreach next