translate-cli 0.1.0.0 → 1.0.0.0
raw patch · 6 files changed
+82/−19 lines, 6 files
Files
- README.md +41/−2
- app/Main.hs +1/−3
- src/Lib.hs +20/−8
- src/Model.hs +16/−2
- test/Spec.hs +2/−2
- translate-cli.cabal +2/−2
README.md view
@@ -1,12 +1,51 @@ # translate-cli -Command Line Interface to translate words+Command Line Interface to translate words.+The current translation supports german and english words. Input will be translated in both directions. +```sh+➜ ~ translate golden+[en] golden+[en] gold+[en] or+[en] aureate+[en] gilded+[de] golden+[de] goldgelb+[de] goldig+[de] Goldfarbe+[de] goldene Farbe+```++## Initial setup++`stack setup`+ ## Build `stack build` ## Run -* `stack exec translate` to show help+### NPM++* `npm i -g @andys8/translate-cli`+* `translate <text-to-translate>`++### Binary++* Download compressed binary+ * [Linux](https://github.com/andys8/translate-cli/raw/master/bin/x86_64-linux/translate.tgz)+ * [OSX](https://github.com/andys8/translate-cli/raw/master/bin/x86_64-osx/translate.tgz)+* Add manually to `PATH`+* `translate <text-to-translate>`++### Build from sources++* Setup stack * `stack exec translate -- <text-to-translate>` to translate+++## Credits++[Glosbe API](https://de.glosbe.com/a-api) is used for translation.
app/Main.hs view
@@ -19,9 +19,7 @@ parser = parseVersion <|> parseMain main :: IO ()-main = do- cmd <- options "Translation tool for the commandline" parser- cmd+main = join (options "Translation tool for the commandline using glosbe api" parser) printVersion :: IO() printVersion = putStrLn $ showVersion version
src/Lib.hs view
@@ -10,25 +10,37 @@ import Model +data Language = DeEn | EnDe+ instance FromJSON Phrase instance FromJSON Translation instance FromJSON RestResponse translateText :: String -> IO() translateText phrase = do- r <- getWith (getOptions phrase) $ apiBase- let body = r ^. responseBody- decoded = eitherDecode body :: Either String RestResponse- printPhrases $ fmap (take 5 . toPhrases) decoded+ doRequest $ getLangOptions DeEn phrase+ doRequest $ getLangOptions EnDe phrase apiBase :: String apiBase = "https://glosbe.com/gapi_v0_1/translate" -getOptions :: String -> Options-getOptions phrase = defaults+doRequest :: Options -> IO()+doRequest options = do+ r <- getWith options apiBase+ let body = r ^. responseBody+ decoded = eitherDecode body :: Either String RestResponse+ printPhrases $ fmap (take 5 . toPhrases) decoded++getLangOptions :: Language -> String -> Options+getLangOptions lang phrase =+ let (from, dest) =+ case lang of+ DeEn -> ("deu", "eng")+ EnDe -> ("eng", "deu")+ in defaults & param "phrase" .~ [pack phrase]- & param "from" .~ ["de"]- & param "dest" .~ ["en"]+ & param "from" .~ [from]+ & param "dest" .~ [dest] & param "format" .~ ["json"] printPhrases :: Either String [String] -> IO()
src/Model.hs view
@@ -10,7 +10,7 @@ , language :: String } deriving (Generic, Show) -data Translation = Translation { phrase :: Maybe Phrase+newtype Translation = Translation { phrase :: Maybe Phrase } deriving (Generic, Show) data RestResponse = RestResponse { result :: String@@ -24,4 +24,18 @@ getPhrases = catMaybes . fmap phrase formatPhrase :: Phrase -> String-formatPhrase (Phrase text lang) = "[" ++ lang ++ "] " ++ text+formatPhrase (Phrase text lang) =+ "\x1b["+ ++ show (colorCode lang)+ ++ "m["+ ++ lang+ ++ "]\x1b[0m "+ ++ text+++colorCode lang =+ case lang of+ "en" -> 32+ "de" -> 33+ _ -> 34+
test/Spec.hs view
@@ -12,7 +12,7 @@ testToPhrasesWithResponse :: Test testToPhrasesWithResponse = TestCase $ assertEqual "Should return empty list if no input"- ["[en] cat", "[en] dog"]+ ["\ESC[32m[en]\ESC[0m cat","\ESC[32m[en]\ESC[0m dog"] (toPhrases response2) main :: IO Counts@@ -24,7 +24,7 @@ enPhrase2 = Phrase "dog" "en" response1 = RestResponse "ok" ([]::[Translation])-response2 = RestResponse "ok" ([translation1, translation2])+response2 = RestResponse "ok" [translation1, translation2] translation1 = Translation (Just enPhrase1) translation2 = Translation (Just enPhrase2)
translate-cli.cabal view
@@ -1,5 +1,5 @@ name: translate-cli-version: 0.1.0.0+version: 1.0.0.0 synopsis: Translation cli tool description: Translation cli tool homepage: https://github.com/andys8/translate-cli#readme@@ -28,7 +28,7 @@ executable translate hs-source-dirs: app main-is: Main.hs- ghc-options: -threaded -rtsopts -with-rtsopts=-N+ ghc-options: -threaded -dynamic -rtsopts -with-rtsopts=-N build-depends: base , translate-cli , turtle