Use AI and Go to program a command-line predictor

Hand-Picked Commands

The main program in Listing 6 then just needs to pass the path to the ~/.myhist.log file to history() (Listing 4), which includes the commands typed so far, including the timestamps and directories. The returned entries then get passed on to the predict() predictor (Listing 5). The result is a prioritized list, which the promptui package (which I discovered on GitHub) shows us in a graphically appealing way on the command line.

The Run() package function makes the command-line user interface interact with the user, lets the user select an item with the arrow keys or with vi mappings, cleans up the menu display nicely when done, and returns the chosen command as the result variable. If everything works without error (i.e., the user did not press Ctrl+C to escape), line 25 outputs the selected command to Stderr, where the g shell function from Listing 1 picks it up, writes it, and executes it.

Conclusions

My DIY command-line predictor significantly reduces typing work during development. Of course, there are no limits to your imagination in DIY projects like this: The algorithm in predict() is still very simple and just cries out to be pimped out using AI tools like Markov chains. Let your creativity run wild.

Infos

  1. "Programming Snapshot – Run statistics on typed shell commands" by Mike Schilli, Linux Magazine, issue 243, February 2021, https://www.linux-magazine.com/Issues/2021/243/Making-History
  2. "Programming Snapshot – Go program stores directory paths" by Mike Schilli, Linux Magazine, issue 228, November 2019, https://www.linux-magazine.com/Issues/2019/228/Pathfinder/(offset)/3
  3. Bash preexec: https://github.com/rcaloras/bash-preexec

The Author

Mike Schilli works as a software engineer in the San Francisco Bay Area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you email him at mailto:mschilli@perlmeister.com he will gladly answer any questions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Making History

    In the history log, the Bash shell records all commands typed by the user. Mike Schilli extracts this data with Go for a statistical analysis of his typing behavior.

  • File Inspector

    Spotify, the Internet music service, collects data about its users and their taste in music. Mike Schilli requested a copy of his files to investigate them with Go.

  • Atuin

    Atuin adds some handy queries to the shell history function, while letting you synchronize your command history across the network.

  • PowerShell in Linux

    Microsoft released its PowerShell under a free license in August 2016 and ported the tool to Linux and Mac OS. Is PowerShell for Linux a mere marketing ploy or a real contender that can compete with native Linux shells?

  • Perl: Portfolio Watch

    We'll show you a Perl script that helps you draw area graphs to keep track of your portfolio's performance.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News