Skip to content

Extension

Takuro Fujino edited this page May 6, 2024 · 8 revisions

Introduction

You can append a new supporting file type of AFTViewer on your own.

To Do that,

  1. Create a new lib-file in ~/.config/aftviewer/additional_types directory.
  2. Add a proper setting in the setting.json file.

The details are in the following.

1. Create a new lib-file

This file is a module dynamically loaded when the file type is specified.
This file should include a main function and add_args. You also need to create a show_help function to display the file type specific help message.

main function

This function defines the behavior of the file type. You can write this function as you like if the arguments are defined correctly, but you can use some functions that are used in the default libraries. See API reference page for available functions.

Parameters

  • path: pathlib.Path
    The path to the target file.
  • args: argparse.Namespace
    The arguments given by the command line. Attributes of this variable depends on the file type. See the following add_args function section for more information and args for default-used arguments.

Return

  • None

add_args function

This function defines the optional arguments used in this file type. Note that available attributes of args passed in the main function should be specified in this function except file and type.
To add default attributes, add_args_* functions are provided. See API reference page for details.

Parameters

  • parser: argparse.ArgumentParser
    The parser which optional arguments will be added.

Return

  • None

show_help function

This function shows the file type specific help message. help_template function in API reference page provides a help template.

Parameters

  • None

Return

  • None

Interactive mode

I append some explanation to use --interactive and/or --interactive_cui mode. To use these modes, you need to define additional functions, called show_func and get_contents. These functions are passed as arguments of interactive_view and/or interactive_cui functions. See API reference for details.

show_func

This function defines how to show the item in the file.

Parameters

  • cpath: str
    A path to the item (not the file).
  • **kwargs: dict
    A keyword argument. Possible keywords are the following.
    • "cui": Bool
      True if this function is called from interactive_cui mode.
    • "system": Bool
      True if this function is called from interactive_cui mode with the system-open key.
    • "stdscr": curses window
      The main curses window of AFTViewer interactive CUI mode. This is the same as the first argument of the curses.wrapper() function.

Return

  • ReturnMessage
    This is a class of returned a message. This class has two attributes: message (str) and error (bool).
    ReturnMessage.message is the message shown in the terminal when the item is selected. ReturnMessage.error is the flag that is an error message or not. If ReturnMessage.error is True, this is treated and shown as an error message.

get_contents

This function returns the contents at the given path in that file. This function returns two lists of strings. The first one is the list of directories. The second one is the list of files. In this context, the "directory" means an item that contains other items, and the "file" means otherwise.

Parameters

  • cpath: pathlib.PurePath

Return

  • None

2. Add a setting

To recognize the new lib-file as the library of the new file type, you need to add the "additional_types" configuration in the setting.json file. See Customization page for the location of the setting.json file.

The "additional_types" setting is a dictionary; the key is the name of the lib-file without extension, and the value is the extension of the supporting file type with separated by space. If the lib-file "png_sample.py" is to show an image file, you should set

{
    "additional_types": {
        "show_image": "png jpeg jpg"
    }
}

in the setting.json.