Diff
checker
文本
文本
图像
文档
Excel
文件夹
Legal
Enterprise
桌面版
定价
登录
下载 Diffchecker 桌面版
比较文本
查找两个文本文件之间的差异
工具
历史
实时编辑器
折叠未更改行
关闭换行
视图
拆分
统一
比对精度
智能
单词
字符
语法高亮
选择语法
忽略
文本转换
转到第一个差异
编辑输入
Diffchecker Desktop
运行Diffchecker最安全的方式。获取Diffchecker桌面应用:您的差异永远不会离开您的电脑!
获取桌面版
Untitled diff
创建于
11年前
差异永不过期
清除
导出
分享
解释
41 删除
行
总计
删除
字符
总计
删除
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
120 行
全部复制
26 添加
行
总计
添加
字符
总计
添加
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
104 行
全部复制
复制
已复制
复制
已复制
import Control.Concurrent
import Control.Concurrent
import Control.Concurrent.STM
import Control.Concurrent.STM
import Control.Concurrent.STM.TVar
import Control.Concurrent.STM.TVar
import Control.Monad
import Control.Monad
import System.Directory
import System.Directory
import System.Environment (getArgs)
import System.Environment (getArgs)
import System.Exit
import System.Exit
import System.INotify
import System.INotify
import System.IO
import System.IO
import System.Process
import System.Process
data Config =
data Config =
Config
Config
复制
已复制
复制
已复制
{
confOutputFile :: FilePath
{
confExecFile
:: FilePath
, confCabalFile
:: FilePath
, confWorking :: Bool
, confWorking :: Bool
} deriving (Show,Read)
} deriving (Show,Read)
main = do
main = do
args <- getArgs
args <- getArgs
let dir = head args
let dir = head args
复制
已复制
复制
已复制
let
cabalOutput
= args !! 1
let
execFile
= args !! 1
putStrLn $ "Watching directory: " ++ dir
putStrLn $ "Watching directory: " ++ dir
复制
已复制
复制
已复制
putStrLn $ "
Cabal output
file: " ++
cabalOutput
putStrLn $ "
Exec
file: " ++
execFile
contents
<-
getDirectoryContents dir
fileExists
<-
doesFileExist execFile
let contents' = filter filterCabal contents
if fileExists
case contents' of
then
do
(x:_) ->
do
let config = Config
execFile
False
let config = Config
cabalOutput x
False
runThread config dir
runThread config dir
复制
已复制
复制
已复制
[] ->
do
else
do
putStrLn
"No cabal
file
found
!"
putStrLn
$ "Exec
file
does not exist
!"
putStrLn "Exiting"
putStrLn "Exiting"
runThread config dir = do
runThread config dir = do
config <- newTVarIO config
config <- newTVarIO config
n <- initINotify
n <- initINotify
putStrLn "Press <Enter> to exit"
putStrLn "Press <Enter> to exit"
print n
print n
wd <- addWatch n
wd <- addWatch n
[ Modify, CloseWrite, Create, Delete, MoveIn, MoveOut ]
[ Modify, CloseWrite, Create, Delete, MoveIn, MoveOut ]
dir
dir
(eventHandler config)
(eventHandler config)
print wd
print wd
getLine
getLine
removeWatch wd
removeWatch wd
killINotify n
killINotify n
eventHandler :: TVar Config -> Event -> IO ()
eventHandler :: TVar Config -> Event -> IO ()
eventHandler conf x@(Modified _ (Just fp)) = handleFilteredFile conf x fp
eventHandler conf x@(Modified _ (Just fp)) = handleFilteredFile conf x fp
eventHandler conf x@(MovedIn _ fp _) = handleFilteredFile conf x fp
eventHandler conf x@(MovedIn _ fp _) = handleFilteredFile conf x fp
eventHandler conf x@(MovedOut _ fp _) = handleFilteredFile conf x fp
eventHandler conf x@(MovedOut _ fp _) = handleFilteredFile conf x fp
eventHandler conf x@(Created _ fp) = handleFilteredFile conf x fp
eventHandler conf x@(Created _ fp) = handleFilteredFile conf x fp
eventHandler conf x@(Deleted _ fp) = handleFilteredFile conf x fp
eventHandler conf x@(Deleted _ fp) = handleFilteredFile conf x fp
eventHandler _ _ = return ()
eventHandler _ _ = return ()
handleFilteredFile conf evt fp =
handleFilteredFile conf evt fp =
when (filterHS fp) $ print evt >> doWork conf
when (filterHS fp) $ print evt >> doWork conf
filterHS fp = fileExt fp == "hs"
filterHS fp = fileExt fp == "hs"
复制
已复制
复制
已复制
filterCabal fp = fileExt fp == "cabal"
fileExt = reverse
fileExt = reverse
. takeWhile (/= '.')
. takeWhile (/= '.')
. reverse
. reverse
doWork :: TVar Config -> IO ()
doWork :: TVar Config -> IO ()
doWork conf = do
doWork conf = do
config <- readTVarIO conf
config <- readTVarIO conf
if confWorking config
if confWorking config
then do
then do
print "Already working!"
print "Already working!"
return ()
return ()
else do
else do
print "New work available!"
print "New work available!"
atomically $ writeTVar conf (config { confWorking = True })
atomically $ writeTVar conf (config { confWorking = True })
_ <- forkIO $ runCI conf
_ <- forkIO $ runCI conf
return ()
return ()
runCI :: TVar Config -> IO ()
runCI :: TVar Config -> IO ()
runCI conf = do
runCI conf = do
复制
已复制
复制
已复制
runCIChain conf
config <- readTVarIO conf
config <- readTVarIO conf
复制
已复制
复制
已复制
contents <- readFile $ confExecFile config
execContents $ lines contents
atomically $ writeTVar conf (config { confWorking = False })
atomically $ writeTVar conf (config { confWorking = False })
return ()
return ()
复制
已复制
复制
已复制
runCIChain :: TVar Config -> IO ()
execContents ::
[String] -> IO Bool
runCIChain conf = do
execContents [] = return True
cabalBuild <- runCabal conf ["build"]
execContents (x:xs)
= do
print $ "*** cabal build result: " ++ show cabalBuild
let p = shell x
case cabalBuild of
(_,_,_,h
) <-
c
rea
te
Process
p
False -> return ()
e
xitCode
<- waitForProcess h
True -> do
case
exitCode
of
cabalTest <- runCabal conf ["test"]
ExitSuccess ->
execContents xs
print $ "*** cabal test result: " ++ show cabalTest
runCabal :: TVar Config ->
[String] -> IO Bool
runCabal conf args
= do
(code, out, err
) <-
rea
d
Process
WithE
xitCode
"cabal" args ""
config <- readTVarIO conf
let outputFile = confOutputFile config
_ <- when (out /= []) $ appendFile outputFile out
_ <- when (err /= []) $ appendFile outputFile err
case
code
of
ExitSuccess ->
return True
ExitFailure _ -> return False
ExitFailure _ -> return False
复制
已复制
复制
已复制
已保存差异
原始文本
打开文件
import Control.Concurrent import Control.Concurrent.STM import Control.Concurrent.STM.TVar import Control.Monad import System.Directory import System.Environment (getArgs) import System.Exit import System.INotify import System.IO import System.Process data Config = Config { confOutputFile :: FilePath , confCabalFile :: FilePath , confWorking :: Bool } deriving (Show,Read) main = do args <- getArgs let dir = head args let cabalOutput = args !! 1 putStrLn $ "Watching directory: " ++ dir putStrLn $ "Cabal output file: " ++ cabalOutput contents <- getDirectoryContents dir let contents' = filter filterCabal contents case contents' of (x:_) -> do let config = Config cabalOutput x False runThread config dir [] -> do putStrLn "No cabal file found!" putStrLn "Exiting" runThread config dir = do config <- newTVarIO config n <- initINotify putStrLn "Press <Enter> to exit" print n wd <- addWatch n [ Modify, CloseWrite, Create, Delete, MoveIn, MoveOut ] dir (eventHandler config) print wd getLine removeWatch wd killINotify n eventHandler :: TVar Config -> Event -> IO () eventHandler conf x@(Modified _ (Just fp)) = handleFilteredFile conf x fp eventHandler conf x@(MovedIn _ fp _) = handleFilteredFile conf x fp eventHandler conf x@(MovedOut _ fp _) = handleFilteredFile conf x fp eventHandler conf x@(Created _ fp) = handleFilteredFile conf x fp eventHandler conf x@(Deleted _ fp) = handleFilteredFile conf x fp eventHandler _ _ = return () handleFilteredFile conf evt fp = when (filterHS fp) $ print evt >> doWork conf filterHS fp = fileExt fp == "hs" filterCabal fp = fileExt fp == "cabal" fileExt = reverse . takeWhile (/= '.') . reverse doWork :: TVar Config -> IO () doWork conf = do config <- readTVarIO conf if confWorking config then do print "Already working!" return () else do print "New work available!" atomically $ writeTVar conf (config { confWorking = True }) _ <- forkIO $ runCI conf return () runCI :: TVar Config -> IO () runCI conf = do runCIChain conf config <- readTVarIO conf atomically $ writeTVar conf (config { confWorking = False }) return () runCIChain :: TVar Config -> IO () runCIChain conf = do cabalBuild <- runCabal conf ["build"] print $ "*** cabal build result: " ++ show cabalBuild case cabalBuild of False -> return () True -> do cabalTest <- runCabal conf ["test"] print $ "*** cabal test result: " ++ show cabalTest runCabal :: TVar Config -> [String] -> IO Bool runCabal conf args = do (code, out, err) <- readProcessWithExitCode "cabal" args "" config <- readTVarIO conf let outputFile = confOutputFile config _ <- when (out /= []) $ appendFile outputFile out _ <- when (err /= []) $ appendFile outputFile err case code of ExitSuccess -> return True ExitFailure _ -> return False
更改后文本
打开文件
import Control.Concurrent import Control.Concurrent.STM import Control.Concurrent.STM.TVar import Control.Monad import System.Directory import System.Environment (getArgs) import System.Exit import System.INotify import System.IO import System.Process data Config = Config { confExecFile :: FilePath , confWorking :: Bool } deriving (Show,Read) main = do args <- getArgs let dir = head args let execFile = args !! 1 putStrLn $ "Watching directory: " ++ dir putStrLn $ "Exec file: " ++ execFile fileExists <- doesFileExist execFile if fileExists then do let config = Config execFile False runThread config dir else do putStrLn $ "Exec file does not exist!" putStrLn "Exiting" runThread config dir = do config <- newTVarIO config n <- initINotify putStrLn "Press <Enter> to exit" print n wd <- addWatch n [ Modify, CloseWrite, Create, Delete, MoveIn, MoveOut ] dir (eventHandler config) print wd getLine removeWatch wd killINotify n eventHandler :: TVar Config -> Event -> IO () eventHandler conf x@(Modified _ (Just fp)) = handleFilteredFile conf x fp eventHandler conf x@(MovedIn _ fp _) = handleFilteredFile conf x fp eventHandler conf x@(MovedOut _ fp _) = handleFilteredFile conf x fp eventHandler conf x@(Created _ fp) = handleFilteredFile conf x fp eventHandler conf x@(Deleted _ fp) = handleFilteredFile conf x fp eventHandler _ _ = return () handleFilteredFile conf evt fp = when (filterHS fp) $ print evt >> doWork conf filterHS fp = fileExt fp == "hs" fileExt = reverse . takeWhile (/= '.') . reverse doWork :: TVar Config -> IO () doWork conf = do config <- readTVarIO conf if confWorking config then do print "Already working!" return () else do print "New work available!" atomically $ writeTVar conf (config { confWorking = True }) _ <- forkIO $ runCI conf return () runCI :: TVar Config -> IO () runCI conf = do config <- readTVarIO conf contents <- readFile $ confExecFile config execContents $ lines contents atomically $ writeTVar conf (config { confWorking = False }) return () execContents :: [String] -> IO Bool execContents [] = return True execContents (x:xs) = do let p = shell x (_,_,_,h) <- createProcess p exitCode <- waitForProcess h case exitCode of ExitSuccess -> execContents xs ExitFailure _ -> return False
查找差异