[PYTHON] Think about the selective interface on the command line

Introduction

The selective interface here is an Anything-like tool in Emacs. It will be easier to understand this point by looking at the image.

Such tools have a variety of applications.

The selective interface allows you to select the output result by connecting it with a pipe. And the selected one will be finally output.

You can use this output to mediate various tools and get values.

percol

A selective interface tool written in python.

https://github.com/mooz/percol

installation of percol

python


$ sudo pip install percol

Or

python


$ git clone https://github.com/mooz/percol

$ cd !$:t

$ sudo python setup.py install

Basic usage of percol

python


$ ls | percol

Matches a specific string from the beginning.

python


$ ls ~ | percol --query="s"

Customize the left side of the prompt.

python


$ ls | percol --right-prompt=prompt1

percol key binding

With percol, you can also set key bindings by creating a configuration file.

python


$ mkdir ~/.percol.d

$ vim ~/.percol.d/rc.py

python:~/.percol.d/rc.py


# https://gist.github.com/mitukiii/4234173
import sys, commands
from percol.command import SelectorCommand
from percol.key import SPECIAL_KEYS
from percol.finder import FinderMultiQueryMigemo, FinderMultiQueryRegex

## prompt
# Case Insensitive /Prompt according to Match Method
def dynamic_prompt():
    prompt = ur""
    if percol.model.finder.__class__ == FinderMultiQueryMigemo:
        prompt += "[Migemo]"
    elif percol.model.finder.__class__ == FinderMultiQueryRegex:
        prompt += "[Regexp]"
    else:
        prompt += "[String]"
    if percol.model.finder.case_insensitive:
        prompt += "[a]"
    else:
        prompt += "[A]"
    prompt += "> %q"
    return prompt

percol.view.__class__.PROMPT = property(lambda self: dynamic_prompt())

## migemo
#Change dictionary path on Mac and Ubuntu
if sys.platform == "darwin":
    FinderMultiQueryMigemo.dictionary_path = "/usr/local/Cellar/cmigemo/20110227/share/migemo/utf-8/migemo-dict"
else:
    FinderMultiQueryMigemo.dictionary_path = "/usr/local/share/migemo/utf-8/migemo-dict"

## kill
#Share kill (yank) with clipboard on Mac
if sys.platform == "darwin":
    def copy_end_of_line_as_kill(self):
        commands.getoutput("echo " + self.model.query[self.model.caret:] + " | pbcopy")
        self.model.query  = self.model.query[:self.model.caret]

    def paste_as_yank(self):
        self.model.insert_string(commands.getoutput("pbpaste"))

    SelectorCommand.kill_end_of_line = copy_end_of_line_as_kill
    SelectorCommand.yank = paste_as_yank

## keymap
#Make delete (backspace) work on Mac
SPECIAL_KEYS.update({
    127: '<backspace>'
})
percol.import_keymap({
    "C-a" : lambda percol: percol.command.beginning_of_line(),
    "C-e" : lambda percol: percol.command.end_of_line(),
    "C-b" : lambda percol: percol.command.backward_char(),
    "C-f" : lambda percol: percol.command.forward_char(),
    "C-d" : lambda percol: percol.command.delete_forward_char(),
    "C-h" : lambda percol: percol.command.delete_backward_char(),
    "C-k" : lambda percol: percol.command.kill_end_of_line(),
    "C-y" : lambda percol: percol.command.yank(),
    "C-n" : lambda percol: percol.command.select_next(),
    "C-p" : lambda percol: percol.command.select_previous(),
    "C-v" : lambda percol: percol.command.select_next_page(),
    "M-v" : lambda percol: percol.command.select_previous_page(),
    "M-<" : lambda percol: percol.command.select_top(),
    "M->" : lambda percol: percol.command.select_bottom(),
    "C-m" : lambda percol: percol.finish(),
    "C-j" : lambda percol: percol.finish(),
    "C-g" : lambda percol: percol.cancel(),
    "M-c" : lambda percol: percol.command.toggle_case_sensitive(),
    "M-m" : lambda percol: percol.command.toggle_finder(FinderMultiQueryMigemo),
    "M-r" : lambda percol: percol.command.toggle_finder(FinderMultiQueryRegex)
})

reference: Super convenient if you put percol and combine it with zsh

I tried the terminal version of anything-like percol instead of zaw

Applied usage of percol

Select multiple lines with percol

To select multiple lines, use percol.command.toggle_mark_and_next (). By default, it seems to be set to C-S.

python


$ ps ax | percol | awk '{ print $1 }' | xargs kill

In my case, I am writing the following configuration file. Multi-line mark and mark cancellation.

python:~/.percol.d/rc.py


    "C-n" : lambda percol: percol.command.toggle_mark_and_next(),
    "C-p" : lambda percol: percol.command.unmark_all(),

You can also set the delimiter with the following command.

python


$ ls | percol | tr '\n' ';'

percol zsh completion

When cloning from GitHub, it seems that it is prepared from the beginning.

https://github.com/mooz/percol/blob/master/tools/zsh/_percol

python


$ curl -o ~/.zsh/functions/_percol https://raw.githubusercontent.com/mooz/percol/master/tools/zsh/_percol

$ exec $SHELL

cd with percol

If cdr is created, it seems that you can easily move by the following method.

~/.zshrc


# http://piyopiyoducky.net/blog/2013/08/17/cdr-with-percol/
### search a destination from cdr list
function percol-get-destination-from-cdr() {
    cdr -l | \
        sed -e 's/^[[:digit:]]*[[:blank:]]*//' | \
        percol --match-method migemo --query "$LBUFFER"
}

### search a destination from cdr list and cd the destination
function percol-cdr() {
    local destination="$(percol-get-destination-from-cdr)"
    if [ -n "$destination" ]; then
        BUFFER="cd $destination"
        zle accept-line
    else
        zle reset-prompt
    fi
}
zle -N percol-cdr
bindkey '^xb' percol-cdr

Other than that, click here.

~/.zshrc


# https://github.com/shibayu36/config-file/blob/master/.zsh/percol-sources/cdr.zsh
function percol-cdr () {
    local selected_dir=$(cdr -l | awk '{ print $2 }' | percol)
    if [ -n "$selected_dir" ]; then
        BUFFER="cd ${selected_dir}"
        zle accept-line
    fi
    zle clear-screen
}
zle -N percol-cdr
bindkey '^@' percol-cdr

reference:

I wrote a cd to the directory I went to recently with percol

Move to Anything-like directory recently moved by Zsh cdr and percol --PiyoPiyoDucky

Migrate from autojump to percol-based directory jump | Web scratch

percol and ghq

Makes your directory structure easier to use.

python


$ go get github.com/motemen/ghq

Details are introduced in the following articles.

ghq + percol + docc | SOTA

Unified and efficient management of local repositories using ghq --delirious thoughts

The zsh completion file of the ghq command has been modified, so I will explain the process --Qiita

percol and git

Percol is also very useful for making git commands easier to use.

Details are introduced in the following articles.

git-issue + git-flow + percol

Settings that make it easier to branch redmine tickets using percol and git-issue | Technology-Gym

Use percol to copy the git commit hash value to the clipboard-Qiita

Git --auto-complete pull request and check out --Qiita

peco

A selective interface tool written in golang based on percol.

https://github.com/lestrrat/peco

The configuration file can be edited as follows.

golang:~/.peco/config.json


{
    "Keymap": {
        "C-p": "peco.SelectPrevious",
        "C-n": "peco.SelectNext",
        "C-c": "peco.Cancel"
    }
}

Details are introduced in the following articles.

Command "peco" that makes the Windows command prompt 10 times more convenient

Introduced peco and used it for zsh history-Goodbye Internet

Recommended Posts

Think about the selective interface on the command line
Keep getting RSS on the command line
You search commandlinefu on the command line
Arduino development on the command line: vim + platformio
About the service command
Syntax highlighting on the command line using Pygments
Convert XLSX to CSV on the command line
Operate Route53 on the command line using AWS-CLI.
Think about how to program Python on the iPad
Search for large files on Linux from the command line
Roughly think about the loss function
Python standard module that can be used on the command line
Get, test, and submit test cases on the command line in the AtCoder contest
Roughly think about the gradient descent method
Look up your Mac's Wi-Fi network name (SSID) on the command line
Introduction of SoftLayer Command Line Interface environment
Correspondence memo when the direction key cannot be used on the python command line
Frequently used (personally) notes on the tar command
My thoughts on python2.6 command line app template
[Python] Seriously think about the M-1 winning method.
About the test
How to pass arguments when invoking python script from blender on the command line
About Go Interface
About the queue
Convert pdf to Text on the command line. No knowledge of Python required. About pdf2txt.py attached to pdfminer and adjustment parameters.
How to create an article from the command line
Sort in Python. Next, let's think about the algorithm.
Think about the next generation of Rack and WSGI
Execute the command on the web server and display the result
Miscellaneous notes about deploying the django app on Heroku
Multiply PDF by OCR on command line on Linux (Ubuntu)
(Remember quickly) How to use the LINUX command line
I tried to notify the honeypot report on LINE