Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A tool for running commands based on the context from the previous command.

License

NotificationsYou must be signed in to change notification settings

Red5d/contextcommands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This tool allows for running context-sensitive commands that will look at the previous command you ran to determine how it should act. It was inspired by Google Now's context-sensitive capabilities.

Installation

This tool requires the importlib module which is included in Python 3.x, but needs to be installed in 2.x.

Clone this repo to your local system, then run theinstall.sh script. It will copy the files into ~/.local/bin/contextcommands and give you a line to put in your ~/.bashrc file to add the 'c' function to your environment.

Example usage:

sudo systemctl restart apache2c status

In this example, the 'c' command is a bash function incontext.bash that takes the argument of 'status' in this case, grabs the previous command from history, then feeds both to thecontext.py Python script to determine what kind of action to take.

Thesystemctl command is detected because of thesystemctl.py module undermodules/ and that module checks to see if the argument ('status') is one of several defined actions for thesystemctl command. Since 'status' is in the defined list of actions, it takes the previous command, replaces 'restart' with 'status', and sends the command text back to the 'c' function for display, adding to the bash history, and finally running the command. The 'systemctl' module also allows for changing the service name in the previous command as well. Any context command that doesn't match a defined systemctl action will be assumed to be a service name.

The other initial modules that I've added are for the 'less', 'cat', and 'docker' commands. The 'less' and 'cat' modules are basically the same. After running either of those, a context command likec nano orc vim will replaceless orcat with the command after the 'c'. This is for the common use case of needing to edit a file after viewing it withless orcat. The 'docker' module is similar to the 'systemctl' module and can change the action in commands likedocker start container1.

Creating Modules

I'll be adding more modules and refining/extending the ones that I have as I find more use cases for this. If you want to make your own modules, take a look at theless.py andsystemctl.py files in themodules folder as examples. Both are commented.

Modules will be auto-detected when the 'c' command is invoked.

The 3 requirements for a module are:

  1. It must have acontext() function like the examples that takes in the list item containing the previous command, and the context command word.
  2. Thecontext() function must return either a command string orNone.
  3. It should not be possible for the returned command string to contain an unintentionally destructive action. For example, if you make a module forrm (which I probably won't do), make sure that it won't replace the file name with something like '*'.

Contributing

I welcome useful module additions and enhancements. Please make sure to test your code and add comments where appropriate.

About

A tool for running commands based on the context from the previous command.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp