5 Commits
Author SHA1 Message Date
paul a37643b5cd Simplify nix setup 2025-02-22 11:44:49 +01:00
paul 61355405d5 Implement search 2025-02-22 11:26:25 +01:00
paul d698ebce72 Bump nixpkgs 2023-12-27 22:16:49 +01:00
paul b0451300a5 Bump version in cabal file too 2023-12-27 13:55:48 +01:00
paul 7afe93727c Update dependencies, remove Nix overlay 2023-12-26 22:30:12 +01:00
10 changed files with 66 additions and 203 deletions
+4
View File
@@ -32,3 +32,7 @@ $ nix develop
$ nix run .#stack
$ nix run .#ghc
```
## TODO
- [ ] Benchmark, profile and optimize search
+40 -11
View File
@@ -66,13 +66,14 @@ type API =
:<|> "utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel
:<|> "search" :> Capture "search" Text :> Get '[PlainText, HTML] SearchModel
server :: Server API
server =
rootR :<|> utf8R :<|> codepointsR :<|> textR
rootR :<|> utf8R :<|> codepointsR :<|> textR :<|> searchR
where
rootR host = do
pure $ RootModel $ fromMaybe "" host
rootR host' = do
pure $ RootModel $ fromMaybe "" host'
utf8R bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
@@ -85,6 +86,9 @@ server =
textR textP = do
pure $ TextModel textP
searchR searchP = do
pure $ mkSearchModel searchP
-- /
newtype RootModel = RootModel
@@ -224,28 +228,53 @@ newtype TextModel = TextModel
}
instance MimeRender HTML TextModel where
mimeRender _ model = renderHtml $ documentWithBody $ do
H.table $ for_ (Text.unpack model.text) $ \c -> do
mimeRender _ model = charTableHtml $ Text.unpack model.text
instance MimeRender PlainText TextModel where
mimeRender _ model = charTableText $ Text.unpack model.text
-- /search/<search>
newtype SearchModel = SearchModel
{ results :: [Char]
}
mkSearchModel :: Text -> SearchModel
mkSearchModel search = SearchModel $ searchAllChars search
searchAllChars :: Text -> [Char]
searchAllChars search = [c | c <- [minBound..maxBound], any (\name -> Text.toLower search `Text.isInfixOf` Text.toLower (Text.pack name)) (allNames c)]
instance MimeRender HTML SearchModel where
mimeRender _ model = charTableHtml model.results
instance MimeRender PlainText SearchModel where
mimeRender _ model = charTableText model.results
-- Utilities
charTableHtml :: [Char] -> BL.ByteString
charTableHtml chars =
renderHtml $ documentWithBody $ do
H.table $ for_ chars $ \c -> do
H.tr $ do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
instance MimeRender PlainText TextModel where
mimeRender _ model = renderText $ Table.render " "
charTableText :: [Char] -> BL.ByteString
charTableText chars =
renderText $ Table.render " "
[ map Table.cl
[ Text.pack [c]
, Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ allNames c
, Text.pack $ fromMaybe "" $ blockName c
]
| c <- Text.unpack model.text
| c <- chars
]
-- Utilities
renderText :: Text -> BL.ByteString
renderText = BL.fromStrict . Encoding.encodeUtf8
Generated
+4 -4
View File
@@ -2,16 +2,16 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1680273054,
"narHash": "sha256-Bs6/5LpvYp379qVqGt9mXxxx9GSE789k3oFc+OAL07M=",
"lastModified": 1703693486,
"narHash": "sha256-tuzNTOs+1zR2BEVKKrRRGdpR/n095AXIcT8Me1px2bI=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "3364b5b117f65fe1ce65a3cdd5612a078a3b31e3",
"rev": "671c2d3e1506a7ee1583515ca80cb3474fdc9c95",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixpkgs-unstable",
"ref": "release-23.11",
"repo": "nixpkgs",
"type": "github"
}
+15 -69
View File
@@ -1,90 +1,36 @@
{
description = "Unicode toy";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs.url = "github:nixos/nixpkgs/release-23.11";
outputs = { self, nixpkgs }:
let
overlay = import ./nix/overlay.nix;
settings = import ./nix/settings.nix;
haskellDeps = import ./nix/haskell-deps.nix;
pkgs = nixpkgs.legacyPackages.x86_64-linux;
pkgs = nixpkgs.legacyPackages.x86_64-linux.extend overlay;
haskellPackages = pkgs.haskell.packages."${settings.ghc}";
ghc = haskellPackages.ghcWithPackages haskellDeps;
# Wrap stack to disable its slow Nix integration.
# Instead, make it use the GHC defined above.
stack = pkgs.stdenv.mkDerivation {
name = "stack";
# The build is simply a call to makeWrapper, so we don't have to
# do any of the typical build steps.
phases = [ "installPhase" ];
nativeBuildInputs = [ pkgs.makeWrapper ];
# makeBinaryWrapper creates a stack executable for us that uses
# the GHC defined in this file.
installPhase = ''
makeWrapper ${pkgs.stack}/bin/stack $out/bin/stack \
--prefix PATH : ${ghc}/bin \
--add-flags '--no-nix --system-ghc --no-install-ghc'
'';
utoy = pkgs.haskellPackages.developPackage {
root = ./.;
};
utoy = pkgs.haskell.lib.justStaticExecutables (haskellPackages.callPackage
({ mkDerivation }:
mkDerivation {
# Keep this in sync with package.yaml
version = "0.6";
pname = "utoy";
license = pkgs.lib.licenses.mit;
src =
# We only need these files for building:
let
whitelist = [
./LICENSE
./utoy.cabal
./Setup.hs
./app
./src
./static
./test
];
in
pkgs.lib.sources.cleanSourceWith {
src = ./.;
filter = path: _type: pkgs.lib.any (prefix: pkgs.lib.hasPrefix (toString prefix) path) whitelist;
};
libraryHaskellDepends = haskellDeps haskellPackages;
}) {});
in {
packages.x86_64-linux = {
inherit ghc;
inherit stack;
packages.x86_64-linux = rec {
docker =
pkgs.dockerTools.buildImage {
name = "git.pbrinkmeier.de/paul/utoy";
tag = utoy.version;
config.Cmd = [ "${utoy}/bin/utoy" ];
config.Cmd = [ "${pkgs.haskell.lib.justStaticExecutables utoy}/bin/utoy" ];
};
default = utoy;
};
devShells.x86_64-linux.default = pkgs.mkShell {
packages = [
stack
ghc
haskellPackages.haskell-language-server
haskellPackages.implicit-hie
devShells.x86_64-linux.default =
(pkgs.haskellPackages.developPackage {
root = ./.;
modifier = drv:
pkgs.haskell.lib.addBuildTools drv [
pkgs.cabal-install
pkgs.haskellPackages.implicit-hie
pkgs.haskell-language-server
];
shellHook = ''
PS1+="(utoy) ";
'';
};
}).env;
};
}
-13
View File
@@ -1,13 +0,0 @@
haskellPackages: with haskellPackages; [
attoparsec
blaze-html
bytestring
file-embed
http-media
servant-server
text
unicode-data
unicode-data-names
wai
warp
]
-23
View File
@@ -1,23 +0,0 @@
let
settings = import ./settings.nix;
overlay = final: prev: {
haskell = prev.haskell // {
packages = prev.haskell.packages // {
"${settings.ghc}" = prev.haskell.packages."${settings.ghc}".override {
overrides = haskellOverlay prev;
};
};
};
};
haskellOverlay = pkgs: final: prev: {
attoparsec-iso8601 = prev.attoparsec-iso8601_1_1_0_0;
http-api-data = prev.http-api-data_0_5;
servant = pkgs.haskell.lib.doJailbreak prev.servant;
servant-server = pkgs.haskell.lib.doJailbreak prev.servant-server;
unicode-data = prev.unicode-data_0_4_0_1;
unicode-data-names = pkgs.haskell.lib.markUnbroken prev.unicode-data-names;
};
in
overlay
-5
View File
@@ -1,5 +0,0 @@
{
# GHC version to use with Nix.
# Should match the one in stack.yaml.
ghc = "ghc944";
}
-70
View File
@@ -1,70 +0,0 @@
# Adapted from new-template.hsfiles
name: utoy
# Keep this in sync with the version in flake.nix.
version: 0.6
git: "https://git.pbrinkmeier.de/paul/utoy"
license: MIT
author: "Paul Brinkmeier"
maintainer: "hallo@pbrinkmeier.de"
copyright: "2023 Paul Brinkmeier"
extra-source-files:
- README.md
- static/utoy.css
dependencies:
- base >= 4.7 && < 5
- attoparsec
- text
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
library:
source-dirs: src
executables:
utoy:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- utoy
- blaze-html
- bytestring
- file-embed
- http-media
- servant-server
- text
- unicode-data
- unicode-data-names
- wai
- warp
# Fix "Multiple files use the same module name", see
# https://stackoverflow.com/questions/67519851/multiple-files-use-the-same-module-name
when:
- condition: false
other-modules: Paths_utoy
tests:
utoy-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- utoy
-5
View File
@@ -1,5 +0,0 @@
# You can get a working environment using nix develop.
# Keep this in sync with nix/settings.nix
resolver: ghc-9.4.4
packages:
- .
+1 -1
View File
@@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: utoy
version: 0.6
version: 0.6.1
author: Paul Brinkmeier
maintainer: hallo@pbrinkmeier.de
copyright: 2023 Paul Brinkmeier