Module: OllamaChat::Pager
- Included in:
- Chat, MessageList
- Defined in:
- lib/ollama_chat/pager.rb
Overview
Module for handling pager functionality in the chat application.
Provides methods to display long-form content using pagers like ‘less’ or ‘more’, automatically handling terminal line capacity and improving the user experience when viewing large amounts of output.
This module is used throughout the application to paginate long messages, configuration outputs, and other text content that might not fit in the current terminal window.
Instance Method Summary collapse
-
#determine_pager_command ⇒ Object
private
The determine_pager_command method identifies an appropriate pager command for displaying content.
-
#use_pager {|the| ... } ⇒ Object
The use_pager method wraps the given block with a pager context.
Instance Method Details
#determine_pager_command ⇒ Object (private)
The determine_pager_command method identifies an appropriate pager command for displaying content. It first checks for a default pager specified by the PAGER environment variable. If no default is found, it attempts to locate ‘less’ or ‘more’ in the system PATH as fallback options. The method returns the selected pager command, ensuring it includes the ‘-r’ flag for proper handling of raw control characters when a fallback pager is used.
37 38 39 |
# File 'lib/ollama_chat/pager.rb', line 37 def determine_pager_command OC::PAGER? end |
#use_pager {|the| ... } ⇒ Object
The use_pager method wraps the given block with a pager context. If the output would exceed the terminal’s line capacity, it pipes the content through an appropriate pager command (like ‘less’ or ‘more’).
17 18 19 20 21 22 23 24 25 |
# File 'lib/ollama_chat/pager.rb', line 17 def use_pager command = determine_pager_command output_buffer = StringIO.new yield output_buffer buffer = output_buffer.string Kramdown::ANSI::Pager.pager(command:, lines: buffer.count(?\n)) do |output| output.puts buffer end end |