Advertisement
banovski

Lines: numbering, replacing tabs with arrows

Oct 8th, 2023 (edited)
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 1.26 KB | Source Code | 0 0
  1. import Data.Text (Text, pack, replace, unpack)
  2. import System.Hclip (setClipboard, getClipboard)
  3. import System.Process (callCommand)
  4. import Text.Regex (Regex, mkRegexWithOpts, subRegex, matchRegex)
  5.  
  6. tab :: Text
  7. tab = pack "\t"
  8.  
  9. arrow :: Text
  10. arrow = pack " → "
  11.  
  12. regex :: Regex
  13. regex = mkRegexWithOpts "^[0-9]+\\. " True False
  14.  
  15. arrowsWithTabs :: Text -> String
  16. arrowsWithTabs = unpack . replace arrow tab
  17.  
  18. tabsWithArrows :: Text -> String
  19. tabsWithArrows = unpack . replace tab arrow
  20.  
  21. lineNumberer :: String -> String
  22. lineNumberer =
  23.   unlines .
  24.   map (\(number, string) -> show number ++ ". " ++ string) . zip [1 ..] . lines
  25.  
  26. lineDenumberer :: String -> String
  27. lineDenumberer =
  28.   unlines . map (\item -> Text.Regex.subRegex regex item "") . lines
  29.  
  30. inputCheck :: String -> Maybe [String]
  31. inputCheck = matchRegex regex
  32.  
  33. dispatcher :: String -> IO ()
  34. dispatcher input
  35.   | inputCheck input == Nothing = do
  36.       setClipboard . lineNumberer . tabsWithArrows $ pack input
  37.       callCommand "espeak -v whisper -a 25 -s 300 'Numbered!'"
  38.   | otherwise = do
  39.       setClipboard . lineDenumberer . arrowsWithTabs $ pack input
  40.       callCommand "espeak -v whisper -a 25 -s 300 'Denumbered!'"
  41.  
  42. main :: IO ()
  43. main = do
  44.   input <- getClipboard
  45.   dispatcher input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement