Now that you’ve built a complete application that handles IO and does some real work, take a some time to work through these exercises. The concepts you learned in this chapter will be critical as you work through the rest of the book, so it will be beneficial to ensure that you have a solid understanding of the concepts here.
If you run into an exercise that you are having trouble completing, try going back and reviewing this chapter, as well as some previous chapters. If you are still having difficulty, move on to the next chapter but plan to come back and revisit the exercise again later.
Handling Terminal Size Edge Cases
In our getTerminalSize function, there were several potential bugs that could have occurred. Try addressing these edge cases:
- tput is missing
- tput doesn’t return a number
- tput output doesn’t contain a trailing newline
Do-ing Some Refactoring
Throughout most of this chapter we used bind syntax to implement our IO actions. Look through some of the code you’ve written for opportunities to refactor this application to use do notation where appropriate.
Refactoring to Use Text and ByteString
Many of the earlier exercises in this book used String instead of ByteString or Text . Refactor some of your existing code to use these more efficient types instead.
Viewing Multiple Files
Expand your application to allow the user to pass more than one file in on the command line, and view them in order. Make sure to update the status line when you go from showing one file to another.
Scrolling Backwards
Instead of just scrolling forward, update your application to allow the user to scroll backwards as well.
Add a Help Screen
Allow the user to view help text on how to use the program by entering ? while viewing a file. The program should clear the screen and display a help message. Once the user scrolls past the end of the help text or presses q , the program should return them to where they were in the document.
Terminal Resizing
Sometimes users will want to resize their terminals while viewing a document. This is a problem for our pager since we calculate the size of the terminal exactly once at the start of the program. Update your program to allow the user to resize the terminal while retaining their current place in the document (the first word on the screen before resizing should still be the first word on the screen after resizing, regardless of how much the screen space has increased or decreased).
Note that detecting changes to the size of the terminal automatically will be unreasonably difficult with the knowledge that you have learned so far in this book, so you should handle terminal resizing by allowing the user to press a key to reflow the text.
Footnotes
[13]
http://hackage.haskell.org/package/text
[14]
http://hackage.haskell.org/package/bytestring
[15]
https://hackage.haskell.org/package/process
[16]
https://hackage.haskell.org/package/base-4.16.0.0/docs/Text-Printf.html#v:printf
[17]
https://hackage.haskell.org/package/time-1.12.1/docs/Data-Time-Format.html
Copyright © 2023, The Pragmatic Bookshelf.