Esempio n. 1
0
    async def grep(self, ctx, data, patterns, *opts, display=True):
        """Print the lines of `data` that match one or more of the specified patterns.
        Additionally, a subset of `grep` arguments can be supplied as `opts`."""
        data, patterns = str(data), str(patterns)

        opts = [str(option) for option in opts]
        validate_options(opts, self.__GREP_ARGS)
        # Filter out empty lines that produce all-matching patterns.
        patterns = "\n".join(p for p in patterns.split("\n") if p)

        output = trim_double_newline(await execute_system_cmd("grep",
                                                              "--color=never",
                                                              "-e",
                                                              patterns,
                                                              *opts,
                                                              "--",
                                                              "-",
                                                              stdin=data))

        if display:
            format_str = "```\n{}\n```"
            chunks = split_message(output,
                                   MAX_MESSAGE_LENGTH - len(format_str))
            for chunk in chunks:
                await ctx.send(format_str.format(chunk))

        return output
Esempio n. 2
0
 async def concat(self, ctx, *arguments, display=True):
     """Joins its arguments together into a single string."""
     content = "".join(str(arg) for arg in arguments)
     if display:
         for chunk in split_message(content, MAX_MESSAGE_LENGTH):
             await ctx.send(chunk)
     return content
Esempio n. 3
0
 async def print(self, ctx, content, file_format="", *, display=True):
     """Print the input data with highlighting specified by 'file_format'."""
     content = str(content)
     if display:
         format_str = f"```{file_format}\n{{}}\n```"
         chunks = split_message(content,
                                MAX_MESSAGE_LENGTH - len(format_str))
         for chunk in chunks:
             await ctx.send(format_str.format(chunk))
     return f"```{file_format}\n{content}\n```"
Esempio n. 4
0
 async def queue(self, ctx, *, display=True):
     """Displays the queue contents."""
     with self.__get_player(ctx) as player:
         head, tail, split = player.queue_data()
         if display:
             queue_info = format_entry_lists(
                 display_entry,
                 enumerate(head),
                 enumerate(tail, start=split),
                 init="\U0001F3BC Current queue:",
             )
             for chunk in split_message(queue_info, MAX_MESSAGE_LENGTH):
                 await ctx.send(chunk)
         return format_entry_lists(export_entry, head, tail)
Esempio n. 5
0
    async def eval(self, ctx, *_, display):
        """Extracts the contents of the file."""
        async for msg in ctx.history(limit=1000):
            for elem in msg.attachments:
                if elem.filename == self.name:
                    content = str(await elem.read(), errors="replace")
                    if display:
                        fmt = "```\n{}\n```"
                        chunks = split_message(content,
                                               MAX_MESSAGE_LENGTH - len(fmt))
                        for chunk in chunks:
                            await ctx.send(fmt.format(chunk))
                    return content

        raise commands.CommandError("No such file!")
Esempio n. 6
0
    async def head(self, ctx, data, line_count=10, display=True):
        """Take the first `line_count` lines of the input."""
        data, line_count = str(data), int(line_count)

        output = trim_double_newline(await execute_system_cmd("head",
                                                              "-n",
                                                              str(line_count),
                                                              "-",
                                                              stdin=data))

        if display:
            format_str = "```\n{}\n```"
            chunks = split_message(output,
                                   MAX_MESSAGE_LENGTH - len(format_str))
            for chunk in chunks:
                await ctx.send(format_str.format(chunk))

        return output