packages feed

Hish (empty) → 0.0.1

raw patch · 8 files changed

+330/−0 lines, 8 filesdep +MissingHdep +basedep +processsetup-changed

Dependencies added: MissingH, base, process

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for Hish++## 0.0.1  -- 2015-10-16++* First version. Released on an unsuspecting world.
+ Hish.cabal view
@@ -0,0 +1,42 @@+Name:                   Hish+Version:                0.0.1+Author:                 Yun-Yan Chi+Maintainer:             jaiyalas@gmail.com+License:                BSD3+License-File:           LICENSE+-- Synopsis:+Description:            Prompt program for Fish shell written in Haskell+Homepage:               https://github.com/jaiyalas/Hish+Bug-reports:            https://github.com/jaiyalas/Hish/issues+Cabal-Version:          >= 1.12+Build-Type:             Simple+Extra-Source-Files:     README.md, ChangeLog.md+category:               Command Line, Console, Shell+--++library+  buildable: True+  exposed:   True+  default-language: Haskell2010+  hs-source-dirs: src+  ghc-options: -w+  build-depends: base      >= 4 && < 5+               , MissingH  >= 1.2+               , process   >= 1.2+  exposed-modules:+        Hish.ANSICode+        Hish.SysInfo++--+executable hish+  default-language: Haskell2010+  hs-source-dirs:   src+  main-is:          hish.hs+  ghc-options:      -w+  build-depends: base      >= 4 && < 5+               , MissingH  >= 1.2+               , process   >= 1.2+--+Source-Repository head+  Type:                 git+  location:             https://github.com/jaiyalas/Hish.git
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Yun-Yan Chi++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 Yun-Yan Chi 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.
+ README.md view
@@ -0,0 +1,33 @@+# Hish++[![Build Status](https://api.travis-ci.org/jaiyalas/Hish.png?branch=stable)](http://travis-ci.org/jaiyalas/Hish)+[![MIT](http://b.repl.ca/v1/license-BSD3-blue.png)](https://en.wikipedia.org/wiki/BSD_licenses)+[![Haskell](http://b.repl.ca/v1/language-haskell-orange.png)](http://haskell.org)++Prompt program for Fish shell written in Haskell++## Features++### Done+++ show **git status**+  + "#" for *clean*+  + "\*" for *dirty*++ show **git branch**++ show **working directory**+  + be shortened if too long++### Todo+++ [ ] distinguish the cases of *clean* and *up-to-date*++ [ ] show *time*++ [ ] implement *color theme*++ [ ] show *SSH* info++ [ ] use *config file*++ [ ] load *environment variables*++## Installation++```+> cabal install Hish+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Hish/ANSICode.hs view
@@ -0,0 +1,125 @@+module Hish.ANSICode+  ( ANSICode (..)+  , esc+  , (<>)+  , applyANSI+  --+  , fgBlack+  , fgRed+  , fgGreen+  , fgYellow+  , fgBlue+  , fgMagenta+  , fgCyan+  , fgWhite+  --+  , fgBlackL+  , fgRedL+  , fgGreenL+  , fgYellowL+  , fgBlueL+  , fgMagentaL+  , fgCyanL+  , fgWhiteL+  --+  , bgBlack+  , bgRed+  , bgGreen+  , bgYellow+  , bgBlue+  , bgMagenta+  , bgCyan+  , bgWhite+  --+  , bgBlackL+  , bgRedL+  , bgGreenL+  , bgYellowL+  , bgBlueL+  , bgMagentaL+  , bgCyanL+  , bgWhiteL+  ) where++-- | Encoding ANSI-code+data ANSICode = ESC_Bold+              | ESC_Underline+              | ESC_Reverse+              | ESC_Reset+              | ESC_Fg        {fg :: Int}+              | ESC_Bg        {bg :: Int}+              | ESC_Setup+                { -- | (foreground, background, other) +                body :: (Int,Int,Int)+                }+              deriving Show+++instance Monoid ANSICode where+  mempty  = ESC_Setup (0,0,0)+  mappend ESC_Bold      (ESC_Setup (f,b,i)) = ESC_Setup (f,b,1)+  mappend ESC_Underline (ESC_Setup (f,b,i)) = ESC_Setup (f,b,4)+  mappend ESC_Reverse   (ESC_Setup (f,b,i)) = ESC_Setup (b,f,i)+  mappend ESC_Reset     (ESC_Setup (f,b,i)) = ESC_Setup (0,0,0)+  mappend (ESC_Fg   fc) (ESC_Setup (f,b,i)) = ESC_Setup (fc,b,0)+  mappend (ESC_Bg   bc) (ESC_Setup (f,b,i)) = ESC_Setup (f,bc,0)++-- | alias of mempty+esc :: ANSICode+esc = mempty++-- | alias of mappend+(<>) :: ANSICode -> ANSICode -> ANSICode+x <> y = mappend x y+infixr 6 <>++-- | apply ANSI setting onto a given string+applyANSI :: String   -- ^ content+          -> ANSICode -- ^ ANSI setting+          -> String+applyANSI s (ESC_Setup (f,b,i)) | f == 0, b == 0, i == 0 = "\ESC[0m\STX"++s+                            | f == 0, b == 0, i == 1 = "\ESC[1m\STX"++s++"\ESC[0m\STX"+                            | f == 0, b == 0, i == 4 = "\ESC[4m\STX"++s++"\ESC[0m\STX"+                            | i == 0 = "\ESC["++(showC f)++(showC b)++"m\STX"++s++"\ESC[0m\STX"+                            | i == 1 = "\ESC["++(showC f)++(showC b)++"1m\STX"++s++"\ESC[0m\STX"+                            | i == 4 = "\ESC["++(showC f)++(showC b)++"4m\STX"++s++"\ESC[0m\STX"++showC :: Int -> String+showC 0 = ""+showC i = show i++";"++fgBlack   = ESC_Fg 30+fgRed     = ESC_Fg 31+fgGreen   = ESC_Fg 32+fgYellow  = ESC_Fg 33+fgBlue    = ESC_Fg 34+fgMagenta = ESC_Fg 35+fgCyan    = ESC_Fg 36+fgWhite   = ESC_Fg 37++fgBlackL   = ESC_Fg 90+fgRedL     = ESC_Fg 91+fgGreenL   = ESC_Fg 92+fgYellowL  = ESC_Fg 93+fgBlueL    = ESC_Fg 94+fgMagentaL = ESC_Fg 95+fgCyanL    = ESC_Fg 96+fgWhiteL   = ESC_Fg 97++bgBlack   = ESC_Bg 40+bgRed     = ESC_Bg 41+bgGreen   = ESC_Bg 42+bgYellow  = ESC_Bg 43+bgBlue    = ESC_Bg 44+bgMagenta = ESC_Bg 45+bgCyan    = ESC_Bg 46+bgWhite   = ESC_Bg 47++bgBlackL   = ESC_Bg 100+bgRedL     = ESC_Bg 101+bgGreenL   = ESC_Bg 102+bgYellowL  = ESC_Bg 103+bgBlueL    = ESC_Bg 104+bgMagentaL = ESC_Bg 105+bgCyanL    = ESC_Bg 106+bgWhiteL   = ESC_Bg 107
+ src/Hish/SysInfo.hs view
@@ -0,0 +1,65 @@+module Hish.SysInfo+  ( status+  , branch+  , pwd+  , uid+  )where++import System.Process+import System.Exit+import Data.List (lines,unlines)+import Data.Char (isSpace)+import qualified Data.String.Utils as S (split,replace,join)++-- | Obtain current git-status.+-- Returning "#" for clean working directory.+-- Returning "*" for dirty working directory.+status :: IO String+status = do+  (code,out,_) <- readProcessWithExitCode "git" ["status","--porcelain"] ""+  case code of+    ExitFailure _ -> return ""+    ExitSuccess   -> return $+      ((\b->if b then "#" else "*")+      .null+      .map head.lines+      ) out+-- | Obtain current name of git-branch+branch :: IO String+branch = do+  (code,out,_) <- readProcessWithExitCode "git" ["branch"] ""+  case code of+    ExitFailure _ -> return ""+    ExitSuccess   -> return $+      (drop 1+      .head.takeWhile (('*'==).head) -- take current br+      .lines.filter (/=' ') -- rm ' ';split by '\n'+      ) out+-- | Obtain current working directory+pwd :: Int -- ^ threshold of shortening+    -> IO String+pwd width = do+  (code,out,_) <- readProcessWithExitCode "pwd" [] ""+  name <- uid+  case code of+    ExitFailure _ -> return ""+    ExitSuccess   -> return $+      ( (\str -> if (head str) == '~' then str else '/':str )+      . (\str -> if (length str) > width+                    then pwdShorten $ S.split "/" str+                    else str)+      . S.replace ("/Users/"++name) "~"+      . filter (/='\n')+      ) $ out+--+pwdShorten :: [String] -> String+pwdShorten [] = ""+pwdShorten [l] = l+pwdShorten (x:xs) = (head x) : '/' : pwdShorten xs+-- | Obtain username+uid :: IO String+uid = do+  (code,name,_) <- readProcessWithExitCode "whoami" [] ""+  case code of+    ExitFailure _ -> return ""+    ExitSuccess   -> return $ filter (/='\n') $ name
+ src/hish.hs view
@@ -0,0 +1,28 @@+module Main where++import Hish.ANSICode+import Hish.SysInfo++--+_prompt_symbol = ">"+_pwdWidth = 60++main :: IO ()+main = do+  st <- status+  br <- branch+  wd <- pwd _pwdWidth+  -- show GIT BRANCH+  putStr $ applyANSI wd $ fgGreen <> esc+  case br of+    "" -> putStr ""+    _  -> do+      putStr " "+      putStr $ applyANSI br $ fgBlueL <> esc+  -- show GIT STATUS+  case st of+    "*" -> putStr $ applyANSI st $ ESC_Bold <> fgRedL <> esc+    "#" -> putStr $ applyANSI st $ ESC_Bold <> fgGreen <> esc+    _   -> putStr ""+  -- show PROMPT SYMBOL+  putStr $ applyANSI (_prompt_symbol++" ") $ fgWhiteL <> esc