Skip to content

Commit

Permalink
Add bookmarks, move bookmarksAsText to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
someodd committed Sep 12, 2020
1 parent 54fd2a5 commit 7aad1d3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
12 changes: 12 additions & 0 deletions data/bookmarks.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ host=floodgap.com
resource=/
type=1
port=70

[Mozz.us - Hand Crafted Experiences]
host=mozz.us
port=70
resource=/
type=1

[World of Solitaire]
host=worldofsolitaire.com
resource=/
port=70
type=1
45 changes: 45 additions & 0 deletions src/Bookmarks.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{-# LANGUAGE OverloadedStrings #-}

module Bookmarks ( bookmarksMenuText ) where

import qualified Data.Text as T
import qualified Data.Map as Map
import Data.List ( intercalate )

import qualified Data.ConfigFile as CF
import qualified Data.ConfigFile.Types as CFT

import Config.Bookmarks

-- | Converts the user's bookmarks.ini into a Gophermenu `Text` which can be parsed
-- into an actual `Menu`. This is to make it so the user's bookmarks can be represented
-- as a Gopher menu.
bookmarksMenuText :: IO T.Text
bookmarksMenuText = do
cp <- getUserBookmarks
-- It is noted in Data.ConfigFile to not do this
let sectionOptionsList = filter (\x -> fst x /= "DEFAULT") $ Map.toList $ CF.content cp -- this skips DEFAULT
menuLines = map entryToMenuItemText sectionOptionsList
pure $ T.intercalate "\n" menuLines
where
-- FIXME: do left/right error thing
entryToMenuItemText :: (CF.SectionSpec, CFT.CPOptions) -> T.Text
entryToMenuItemText (sectionString, options) =
let label = sectionString
host =
case Map.lookup "host" options of
Just h -> h
Nothing -> error "Parse bookmark error: no host"
resource =
case Map.lookup "resource" options of
Just r -> r
Nothing -> error "Parse bookmark error: no resource"
gophertype =
case Map.lookup "type" options of
Just gt -> gt
Nothing -> error "Parse bookmark error: no gophertype"
port =
case Map.lookup "port" options of
Just p -> p
Nothing -> error "Parse bookmark error: no port!"
in T.pack $ intercalate "\t" [gophertype ++ label, resource, host, port]
35 changes: 1 addition & 34 deletions src/BrickApp/ModeAction/Bookmarks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
module BrickApp.ModeAction.Bookmarks where

import Data.Maybe ( fromJust )
import qualified Data.Map as Map
import qualified Data.Text as T
import Data.List ( intercalate )

import Brick.Widgets.Edit as E
import Brick.Widgets.Core ( str )
import qualified Data.ConfigFile as CF
import qualified Data.ConfigFile.Types as CFT

import Bookmarks
import BrickApp.Types.Names
import BrickApp.Utils ( menuToMenuBuffer, renderModeToItemChar )
import Gopher
Expand All @@ -32,38 +31,6 @@ initAddBookmarkMode gbs = gbs
}
}

-- | Converts the user's bookmarks.ini into a Gophermenu `Text` which can be parsed
-- into an actual `Menu`.
bookmarksMenuText :: IO T.Text
bookmarksMenuText = do
cp <- getUserBookmarks
-- It is noted in Data.ConfigFile to not do this
let sectionOptionsList = filter (\x -> fst x /= "DEFAULT") $ Map.toList $ CF.content cp -- this skips DEFAULT
menuLines = map entryToMenuItemText sectionOptionsList
pure $ T.intercalate "\n" menuLines
where
-- FIXME: do left/right error thing
entryToMenuItemText :: (CF.SectionSpec, CFT.CPOptions) -> T.Text
entryToMenuItemText (sectionString, options) =
let label = sectionString
host =
case Map.lookup "host" options of
Just h -> h
Nothing -> error "Parse bookmark error: no host"
resource =
case Map.lookup "resource" options of
Just r -> r
Nothing -> error "Parse bookmark error: no resource"
gophertype =
case Map.lookup "type" options of
Just gt -> gt
Nothing -> error "Parse bookmark error: no gophertype"
port =
case Map.lookup "port" options of
Just p -> p
Nothing -> error "Parse bookmark error: no port!"
in T.pack $ intercalate "\t" [gophertype ++ label, resource, host, port]

-- | Remove the selected bookmark, writing to file, and then give back
-- a new version of the `GopherBrowserState` that has the bookmark
-- removed from the menu.
Expand Down
1 change: 1 addition & 0 deletions waffle.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ library
Gopher
Open
GopherNet
Bookmarks

Config
Config.ConfigOpen
Expand Down

0 comments on commit 7aad1d3

Please sign in to comment.