Copyright | Copyright 2015 Peter Harpending |
---|---|
License | Apache-2.0 |
Maintainer | Peter Harpending <peter@harpending.org> |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
You know when you run git commit
, and a little editor pops up?
This is a Haskell library that does that.
- bracketConduit :: Template -> Producer (ResourceT IO) ByteString -> Consumer ByteString (ResourceT IO) b -> ResourceT IO (ExitCode, b)
- type Template = String
- mkTemplate :: String -> Template
- htmlTemplate :: Template
- jsonTemplate :: Template
- markdownTemplate :: Template
- oldMarkdownTemplate :: Template
- plainTemplate :: Template
- xmlTemplate :: Template
- txtTemplate :: Template
- yamlTemplate :: Template
- runSpecificEditor :: String -> Template -> ByteString -> IO ByteString
- userEditor :: IO (Maybe String)
- userEditorDefault :: String -> IO String
- _default_editor :: String
- _editor :: String
- _ftempl :: String
- wrapStr :: ByteString -> String
- runUserEditorDWIM :: Template -> ByteString -> IO ByteString
- runUserEditorDWIMFile :: Template -> FilePath -> IO ByteString
- runUserEditor :: IO ByteString
- runUserEditorWithTemplate :: Template -> IO ByteString
Documentation
bracketConduit :: Template -> Producer (ResourceT IO) ByteString -> Consumer ByteString (ResourceT IO) b -> ResourceT IO (ExitCode, b) Source
This is the function you should use.
bracketConduit
takes a Producer
, to produce the contents of the
original file, and a Consumer
to consume them, and returns the
result of the consumer, along with the exit code from the editor.
If you don't know how to use conduits, see the documentation for the conduit package.
If you really don't want to use conduits, you can use the strict I/O functions at the bottom of the module. Be warned that those functions are not very performant.
mkTemplate :: String -> Template Source
Make a template
mkTemplate ext = _ftempl <> "." <> ext
>>>
mkTemplate "blah"
tmp.blah
htmlTemplate :: Template Source
File-type template for HTML
htmlTemplate = mkTemplate "html"
jsonTemplate :: Template Source
File-type template for JSON
jsonTemplate = mkTemplate "json"
markdownTemplate :: Template Source
File-type template for Markdown
markdownTemplate = mkTemplate "md"
oldMarkdownTemplate :: Template Source
Older file-type template for Markdown
markdownTemplate = mkTemplate "markdown"
plainTemplate :: Template Source
File-type template for plain text
plainTemplate = mkTemplate "txt"
xmlTemplate :: Template Source
File-type template for XML
xmlTemplate = mkTemplate "xml"
txtTemplate :: Template Source
Same as plainTemplate
yamlTemplate :: Template Source
File-type template for YAML
yamlTemplate = mkTemplate "yaml"
:: String | Name of the editor. |
-> Template | Template for the file name. |
-> ByteString | Initial contents of the file. |
-> IO ByteString | Resulting ByteString. |
Open an editor. You probably don't want to use this function.
runSpecific editorName templ initialContents = withSystemTempFile templ $ filePath hdl -> do hSetBinaryMode hdl True hSetBuffering hdl NoBuffering B.hPut hdl initialContents callProcess editorName [filePath] B.hGetContents hdl
userEditor :: IO (Maybe String) Source
This uses getEnv
from System.Posix to attempt to
get the user's $EDITOR
variable.
userEditor = getEnv _editor
Wrapper around userEditor
that includes a fallback option if the
$EDITOR
variable doesn't exist.
userEditorDefault def = userEditor >>= case
Just e -> pure e
Nothing -> pure def
_default_editor :: String Source
The default editor if no other editor is found
_default_editor = "nano"
The variable we should search when finding the user's editor.
_editor = EDITOR
wrapStr :: ByteString -> String Source
If you don't want to use ByteString, use this function.
>>>
:t runUserEditorDWIM plainTemplate mempty
ByteString>>>
:t wrapStr <$> runUserEditorDWIM plainTemplate mempty
String
:: Template | Template for the file name |
-> ByteString | Initial contents |
-> IO ByteString | Resulting ByteString |
This is most likely the function you want to use. It takes a file type template as an argument, along with what you want displayed when the user opens the editor. It then runs the editor, and returns the version of the text that the user modified.
runUserEditorDWIM templ initialContents = userEditorDefault _default_editor >>= theEditor -> runSpecificEditor theEditor templ initialContents
Examples:
>>>
:set -XOverloadedStrings
>>>
runUserEditorDWIM jsonTemplate "{\n\n}\n"
This will open up the user's $EDITOR
configured to edit JSON, and
with the initial text:
{ }
There are a bunch of templates. See the "File-type extensions" section. It's also trivially easy to make your own templates. Say you want one for, I dunno, Python:
pythonTemplate = mkTemplate "py"
The argument to mkTemplate
should be the file extension you
want. In that case, I used "py"
, because Python's file extension
is .py
.
:: Template | Template for the file name |
-> FilePath | File containing initial contents |
-> IO ByteString | Resulting ByteString |
This is the same as above, it just takes a file as an argument instead of the ByteString
runUserEditorDWIMFile templ fp = B.readFile fp >>= runUserEditorDWIM templ
runUserEditor :: IO ByteString Source
This is likely the simplest function here. It opens up the user's editor, and fetches a ByteString from it
runUserEditor = userEditorDefault _default_editor >>= theEditor -> runSpecificEditor theEditor plainTemplate mempty
runUserEditorWithTemplate Source
:: Template | Template for the file name |
-> IO ByteString | Resulting ByteString |
This is probably the second-simplest function.