shh-extras (empty) → 0.1.0.0
raw patch · 5 files changed
+118/−0 lines, 5 filesdep +basedep +hostnamedep +shhsetup-changed
Dependencies added: base, hostname, shh, time
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- shh-extras.cabal +28/−0
- src/Shh/Prompt.hs +53/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for shh-extras++## 0.1.0.0 -- 2019-03-07++* Provides a prompt formating function
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2019, Luke Clifton++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Luke Clifton nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ shh-extras.cabal view
@@ -0,0 +1,28 @@+cabal-version: >=1.10+name: shh-extras+version: 0.1.0.0+synopsis: Utility functions for using shh+description: Provides useful functions for setting up Shh as an+ interactive shell.+license: BSD3+license-file: LICENSE+author: Luke Clifton+maintainer: lukec@themk.net+copyright: (c) 2018, 2019 Luke Clifton+category: System+build-type: Simple+extra-source-files: CHANGELOG.md++source-repository head+ type: git+ location: https://github.com/luke-clifton/shh++library+ exposed-modules: Shh.Prompt+ build-depends:+ base >=4.11 && <4.13,+ shh >= 0.2.0.0,+ time,+ hostname+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Shh/Prompt.hs view
@@ -0,0 +1,53 @@+-- | This module provides utility functions for creating prompts. Useful+-- if you want to use Shh and GHCi as a shell.+module Shh.Prompt where++import Shh+import System.Environment+import Network.HostName+import Data.Time++-- | The type of GHCi prompt functions+type PromptFn = [String] -> Int -> IO String++-- | Format a prompt line suitable for use with Shh.+--+-- This also calls `initInteractive`, which is required for a good+-- user experience when using `shh` as a shell.+--+-- The format of the prompt uses the "%" character as an escape mechanism,+-- allowing for the substitution of various values into the prompt.+--+-- * @%% -> %@+-- * @%u -> current user@+-- * @%w -> current directory@+-- * @%h -> hostname@+-- * @%t -> HH:MM:SS@+--+-- Use it by importing @Shh.Prompt@ in your @.ghci@ or @$SHH_DIR/init.ghci@+-- files and @:set prompt-function formatPrompt "%u\@%h:%w$ "@+formatPrompt :: String -> PromptFn+formatPrompt fmt _ _ = do+ initInteractive+ format fmt+ where++ format :: String -> IO String+ format ('%' : '%' : rest) = ('%':) <$> format rest+ format ('%' : 'u' : rest) = insertEnv "USER" rest+ format ('%' : 'w' : rest) = insertEnv "PWD" rest+ format ('%' : 'h' : rest) = insertIO getHostName rest+ format ('%' : 't' : rest) = insertIO getTime rest+ format ( x : rest) = (x:) <$> format rest+ format [] = pure []++ insertEnv :: String -> String -> IO String+ insertEnv var rest = insertIO (getEnv var) rest++ insertIO :: IO String -> String -> IO String+ insertIO a rest = a >>= \s -> (s ++) <$> format rest++ getTime :: IO String+ getTime = formatTime defaultTimeLocale "%H:%M:%S" <$>+ (utcToLocalTime <$> getCurrentTimeZone <*> getCurrentTime)+