Translation of atom Flight Manual: 3.4 text processing package

Time:2020-5-22

Text processing package

After we’ve written the first package, let’s take a look at some examples of other packages that we can write. This section will guide you to create a simple command to replace the selected text with ASCII art. When you run our command with the word “cool” selected, it will be replaced with:

                                     o888
    ooooooo     ooooooo     ooooooo   888
  888     888 888     888 888     888 888
  888         888     888 888     888 888
    88ooo888    88ooo88     88ooo88  o888o

This example should show you how to do basic text manipulation in the current text buffer and how to handle selection.

The last bag is in https://github.com/atom/ascii-art View in.

Basic text insertion

Press firstcmd-shift-PTo pop up the command panel. Then enter “generate package” and select the “package generator: generate package” command, as we did in the “package generator” section. inputascii-artAs the name of the package.

Now let’s edit the files in the package to let our character painting package do something interesting. Since this package does not need any UI, we can remove all view related ones, so we can safely delete themlib/ascii-art-view.coffeespec/ascii-art-view-spec.coffeeandstyles/

Next, openlib/ascii-art.coffeeAnd remove all view code, so it looks like this:

{CompositeDisposable} = require 'atom'

module.exports =
  subscriptions: null

  activate: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace',
      'ascii-art:convert': => @convert()

  deactivate: ->
    @subscriptions.dispose()

  convert: ->
    console.log 'Convert text!'

Create command

Now let’s add a command. It is strongly recommended that you take a namespace for your command, followed by a package name:。 So you can see in the code, we call the commandascii-art:convert, and when it callsconvert()method.

So far, it’s only recorded in the console. Let’s start by inserting some characters into the text buffer.

convert: ->
  if editor = atom.workspace.getActiveTextEditor()
    editor.insertText('Hello, World!')

As in word count, we useatom.workspace.getActiveTextEditor()To get the object that represents the currently active editor. Ifconvert()Method is called when there is no editor to get focus, it simply returns a blank string, so we can skip the next line.

Next we useinsertText()Method to insert a string into the current text editor. No matter where the cursor is currently in the editor, text is inserted at the cursor. If any text is selected, the selected text is replaced with “Hello, world!” text.

Reload package

Before we can triggerascii-art:convertBefore, we need to reload the window to load the latest code of our package. From the command panel or pressctrl-alt-cmd-lTo run the window: reload command.

Trigger command

Now you can open the command panel and search for the ASCII Art: convert command. But not at all. To fix it, turn it onpackage.jsonAnd findactivationCommandsProperty. Active commands speed up atom startup by delaying the loading of commands when they are not in use. So remove the existing command andactivationCommandsAdd toascii-art:convert

"activationCommands": {
  "atom-workspace": "ascii-art:convert"
}

First, reload the window through the window: reload command in the command panel. Now you can execute the ASCII Art: convert command, which will output “Hello, world!”.

Add shortcut key

Now let’s add the trigger “ASCII”- art:convert ”Shortcut key for the command. openkeymaps/ascii-art.cson, add a key binding toctrl-alt-alink toascii-art:convertOn command. Since you don’t need default key bindings, you can delete them.

When it’s done, it should look like this:

'atom-text-editor':
  'ctrl-alt-a': 'ascii-art:convert'

Now reload the window and verify that the shortcut works.

Add character painting

Now we need to convert the selected characters into character paintings. In order to complete it, we use thefigletNode module. openpackage.json, add the latest version of figlet todependenciesMedium:

"dependencies": {
  "figlet": "1.0.8"
}

Run update package dependencies: update from the command panel after saving the file. This will automatically install the node module dependency of the package, in this case only figlet. Whenever you updatepackage.jsonIn the filedependenciesField, you need to run the “update package dependencies: update” command.

If it doesn’t work for some reason, you will see the message “failed to update package dependencies”, and you will find a new one in your directorynpm-debug.logDocuments. This document will tell you exactly where there is an error.

Now inlib/ascii-art.coffeeRequest infigletNode module, and converts the selected text to character drawing instead of inserting “Hello, world!”.

convert: ->
  if editor = atom.workspace.getActiveTextEditor()
    selection = editor.getSelectedText()

    figlet = require 'figlet'
    font = "o8"
    figlet selection, {font: font}, (error, art) ->
      if error
        console.error(error)
      else
        editor.insertText("\n#{art}\n")

Reload the editor, select some text in the editor window, and pressctrl-alt-aInstead, it will be replaced with a funny character drawing version.

In this case, we need to see something new quickly. First of alleditor.getSelectedText(), as you guessed, returns the currently selected text.

Then we call figlet’s code to convert it to something else and use theeditor.insertText()Replace the currently selected text with it.

Summary

In this section, we write a package without UI to get the selected text and replace it with the processed version. It may be helpful to create text prompts and check tools.