Skip to content

Instantly share code, notes, and snippets.

@oconnore
Created May 28, 2014 23:57
Show Gist options
  • Save oconnore/6577d8d3cc528cd601b0 to your computer and use it in GitHub Desktop.
Save oconnore/6577d8d3cc528cd601b0 to your computer and use it in GitHub Desktop.
Line / Whitespace selection tool
import System.IO
import System.Environment
import Text.Printf
import Data.Char
import Data.Set
import Data.List
import Text.Regex
import Text.Regex.Posix
printList :: [String] -> IO ()
printList [] = return ()
printList (a:rs) = do putStrLn a
printList rs
incr :: Int -> [Int]
incr x = (x : incr (x + 1))
filterIdx :: Set Int -> [a] -> [a]
filterIdx set lis = snd . unzip $ Prelude.filter (\x -> member (fst x) set)
(zip (incr 1) lis)
formatLines :: [[String]] -> [String]
formatLines lines = Prelude.map (\line ->
Prelude.foldl (\x y -> y ++ x) "" (intersperse " " (reverse line))) lines
printCols :: Set Int -> IO ()
printCols set = do
cont <- getContents
printList . formatLines $ Prelude.map ((filterIdx set) . words) (lines cont)
return ()
printLines :: Set Int -> IO ()
printLines set = do
cont <- getContents
printList (filterIdx set (lines cont))
type SelArguments = (Bool, [Int])
parseArgs' :: [String] -> SelArguments -> SelArguments
parseArgs' [] res = res
parseArgs' (h:hs) (col, ls) =
if h =~ "-c|--col(umns?)?" then parseArgs' hs (True, ls)
else if h =~ "[0-9]+" then parseArgs' hs (col, (read h) : ls)
else error "Cannot parse arguments"
parseArgs :: [String] -> SelArguments
parseArgs arr = parseArgs' arr (False, [])
main :: IO ()
main = do
args <- getArgs
let pargs = (parseArgs args) in
case pargs of
(col, list) -> let intSet = (fromList list) in
case col of
True -> printCols intSet
False -> printLines intSet
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment