From 8b1755425c963740b37d12bd5c16ca4de94a828f Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Tue, 29 Aug 2023 00:08:02 +0200 Subject: [PATCH 1/2] Implement utility functions to deal with the user - getTerminalSize -> returns the dimensions of the terminal - getUserCommand -> returns input from the user. We currently support - ``NextPage`` and `Quit`. Implement getTerminalSize via 'tput' --- src/HCat/Internal.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/HCat/Internal.hs b/src/HCat/Internal.hs index d28a0ff..483049b 100644 --- a/src/HCat/Internal.hs +++ b/src/HCat/Internal.hs @@ -4,6 +4,10 @@ module HCat.Internal where import Data.Text (Text) import Data.Text qualified as T +import System.IO +import System.Info qualified as SysInfo +import System.Process qualified as P + -- | @parseArgs@ takes a list of strings and returns a single FilePath if there was exactly one element. -- -- >>> parseArgs ["foo"] @@ -67,3 +71,29 @@ paginate (ScreenDimensions rows cols) = type Pages = [Page] type Page = Text + +getTerminalSize :: IO ScreenDimensions +getTerminalSize = case SysInfo.os of + "linux" -> tputScreenDimensions + "darwin" -> tputScreenDimensions + _ -> pure $ defaultScreenDimensions{screenRows = 25, screenColumns = 80} + +defaultScreenDimensions :: ScreenDimensions +defaultScreenDimensions = ScreenDimensions{screenRows = 25, screenColumns = 80} + +tputScreenDimensions :: IO ScreenDimensions +tputScreenDimensions = ScreenDimensions <$> tput "lines" <*> tput "cols" + where + tput cmd = read <$> P.readProcess "tput" [cmd] "" + +data UserCommand = NextPage | Quit deriving (Show) + +getUserCommand :: IO UserCommand +getUserCommand = do + hSetBuffering stdin NoBuffering + hSetEcho stdin False + input <- getChar + case input of + ' ' -> pure NextPage + 'q' -> pure Quit + _ -> getUserCommand From 3cd99e3f0b763cb2eefa503a195738ea25be68ed Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Tue, 29 Aug 2023 21:07:33 +0200 Subject: [PATCH 2/2] Add 'doctest' as a cabal test We provide a simple wrapper that executes `doctest` on our `src` directory. This allows us to run all doctests during a regular `cabal test` run. --- flake.nix | 2 +- hcat.cabal | 19 +++++++++++++++++++ package.yaml | 13 +++++++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index 0d630c6..a665510 100644 --- a/flake.nix +++ b/flake.nix @@ -70,12 +70,12 @@ haskellPackages.haskell-language-server haskellPackages.fourmolu haskellPackages.hspec-discover + haskellPackages.doctest cabal-install ghcid nixfmt hpack hlint - haskellPackages.doctest ]; }; }); diff --git a/hcat.cabal b/hcat.cabal index f84a895..9c3766b 100644 --- a/hcat.cabal +++ b/hcat.cabal @@ -50,6 +50,25 @@ executable hcat , hcat default-language: GHC2021 +test-suite doctest + type: exitcode-stdio-1.0 + main-is: Doctest.hs + other-modules: + Paths_hcat + hs-source-dirs: + test/doctest + default-extensions: + BlockArguments + OverloadedStrings + ImportQualifiedPost + ghc-options: -Wall -fdefer-typed-holes + build-tool-depends: + doctest:doctest + build-depends: + base >=4.13 && <5 + , process + default-language: Haskell2010 + test-suite spec type: exitcode-stdio-1.0 main-is: Spec.hs diff --git a/package.yaml b/package.yaml index b2d6beb..d15b892 100644 --- a/package.yaml +++ b/package.yaml @@ -56,9 +56,10 @@ tests: build-tools: hspec-discover verbatim: default-language: GHC2021 - # doctest: - # main: Doctest.hs - # source-dirs: - # - test/doctest - # dependencies: - # - process + doctest: + main: Doctest.hs + source-dirs: + - test/doctest + dependencies: + - process + build-tools: doctest