Skip to content

Instantly share code, notes, and snippets.

@mshock
Created May 15, 2012 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/2702575 to your computer and use it in GitHub Desktop.
Save mshock/2702575 to your computer and use it in GitHub Desktop.
shell tab emulation
#! /usr/bin/perl -w
use Term::ReadKey;
# make STDOUT hot
$| = 1;
$string = '';
$done = 0;
# get your dir listing
opendir($dh, '.');
@dir = sort readdir($dh);
closedir $dh;
# input loop
while (!$done) {
# read a key
while (defined ($key = ReadKey(-1))) {
# if it's a tab, get the next thing in the directory
if ($key =~ /\t/) {
# treat dir as circular list
$cur = shift(@dir);
# 'clear' the input
print "\b" x (length($string));
print " " x (length($string));
print "\b" x (length($string));
$string = $cur;
push(@dir, $cur);
}
# return means we're done
elsif ($key =~ /\r/) {
$done = 1;
last;
}
# otherwise add to input string
else {
$string .= $key;
# don't forget to clear to keep things looking normal
print "\b" x (length($string) - 1);
}
print $string;
}
}
print "\nyou have selected: $string\n";
@mshock
Copy link
Author

mshock commented May 15, 2012

Script for getting user input while emulating the shell functionality of directory/file listing using tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment