Created
May 15, 2012 15:18
-
-
Save mshock/2702575 to your computer and use it in GitHub Desktop.
shell tab emulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script for getting user input while emulating the shell functionality of directory/file listing using tab.