modify tab to complete word when there is only one completion
This commit is contained in:
@@ -78,19 +78,35 @@ printStatus state str =
|
|||||||
truncLen s = if length s > 20 then take 20 s ++ "..." else s
|
truncLen s = if length s > 20 then take 20 s ++ "..." else s
|
||||||
|
|
||||||
|
|
||||||
printCompletions :: State -> String -> IO ()
|
tabComplete :: String -> Int -> State -> IO (String, Int)
|
||||||
printCompletions state str =
|
tabComplete str pos state =
|
||||||
let tokens = words str
|
let tokens = words $ take pos str
|
||||||
completions = if tokens == []
|
completions = if tokens == []
|
||||||
then []
|
then []
|
||||||
else getCompletions (last tokens) state
|
else getCompletions (last tokens) state
|
||||||
compString = intercalate " " $ take 5 completions
|
in case completions of
|
||||||
toPrint = "\n\x1b[32m" -- color to green
|
cs@(_:_:_) -> do
|
||||||
|
printCompletions cs
|
||||||
|
putStr $ concat $ replicate (length promptStr + pos) "\x1b[C"
|
||||||
|
-- ^ shift cursor right to current position
|
||||||
|
return (str, pos)
|
||||||
|
x:_ -> let diff = drop (length $ last tokens) x
|
||||||
|
in do
|
||||||
|
putStr diff
|
||||||
|
return (take pos str ++ diff ++ drop pos str, pos + length diff)
|
||||||
|
_ -> return (str, pos)
|
||||||
|
|
||||||
|
|
||||||
|
printCompletions :: [String] -> IO ()
|
||||||
|
printCompletions cs =
|
||||||
|
let compString = intercalate " " $ take 5 cs -- only list 5 matches
|
||||||
|
toPrint = "\n"
|
||||||
|
++ "\x1b[2K\r" -- clear completion line
|
||||||
|
++ "\x1b[32m" -- color to green
|
||||||
++ replicate (length promptStr) ' '
|
++ replicate (length promptStr) ' '
|
||||||
++ compString
|
++ compString
|
||||||
++ "\x1b[0m\r\x1b[A" -- color to white, move to prev line
|
++ "\x1b[0m\r\x1b[A" -- color to white, move to prev line
|
||||||
in do putStr toPrint
|
in putStr toPrint
|
||||||
putStr $ concat $ replicate (length promptStr + length str) "\x1b[C"
|
|
||||||
|
|
||||||
|
|
||||||
cleanPrompt :: IO ()
|
cleanPrompt :: IO ()
|
||||||
@@ -153,8 +169,8 @@ readline state = read' "" 0
|
|||||||
(pos, nacc, newpstate) <- readSpecialKey pos acc pstate
|
(pos, nacc, newpstate) <- readSpecialKey pos acc pstate
|
||||||
read' nacc (clamp pos 0 (length acc)) newpstate
|
read' nacc (clamp pos 0 (length acc)) newpstate
|
||||||
'\t' -> do
|
'\t' -> do
|
||||||
printCompletions state acc
|
(newAcc, newPos) <- tabComplete acc pos state
|
||||||
read' acc pos pstate
|
read' newAcc newPos pstate
|
||||||
c ->
|
c ->
|
||||||
if isPrint c
|
if isPrint c
|
||||||
then do
|
then do
|
||||||
|
Reference in New Issue
Block a user