Display gophermap or text Returns a Command, which may be a Link containing a reference to part of the input data.
(
data: &'a str, // The text to display in pages
title: &str, // A short document title
gophermap: bool // True if data should consist of links
)
| 60 | /// Returns a Command, which may be a Link containing a reference |
| 61 | /// to part of the input data. |
| 62 | fn display_text<'a>( |
| 63 | data: &'a str, // The text to display in pages |
| 64 | title: &str, // A short document title |
| 65 | gophermap: bool // True if data should consist of links |
| 66 | ) -> Command<'a> { |
| 67 | let lines_per_page = 24; // Last line for status |
| 68 | let lines_paginate = 20; // How many lines to move each page |
| 69 | |
| 70 | // Split data into lines |
| 71 | let lines = data.lines().collect::<Vec<&str>>(); |
| 72 | let mut start_line = 0; // First line to display |
| 73 | loop { |
| 74 | let end_line = if start_line + lines_per_page > lines.len() {lines.len()} else { |
| 75 | start_line + lines_per_page |
| 76 | }; |
| 77 | |
| 78 | // Links shown on the page |
| 79 | let mut links: Vec<&str> = Vec::new(); |
| 80 | |
| 81 | // Draw the page |
| 82 | if gophermap { |
| 83 | // Indent lines, add numbers, type to links |
| 84 | for line in |
| 85 | (&lines[start_line..end_line]) |
| 86 | .iter() { |
| 87 | // First character determines type |
| 88 | match line.chars().nth(0) { |
| 89 | Some('i') => { |
| 90 | // Print text up to the first tab |
| 91 | println!(" {}", line[1..].split('\t').next().unwrap_or("")); |
| 92 | } |
| 93 | Some('0') => { |
| 94 | // Text file |
| 95 | print!("{}-TXT ", links.len()); |
| 96 | links.push(line); |
| 97 | println!("{}", line[1..].split('\t').next().unwrap_or("")); |
| 98 | } |
| 99 | Some('1') => { |
| 100 | // Gopher menu |
| 101 | print!("{}-DIR ", links.len()); |
| 102 | links.push(line); |
| 103 | println!("{}", line[1..].split('\t').next().unwrap_or("")); |
| 104 | } |
| 105 | Some(_) => { |
| 106 | println!("{:?}", line); |
| 107 | } |
| 108 | None => { |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } else { |
| 113 | // A text file, no links. Just print lines and line number at the end |
| 114 | for line in (&lines[start_line..end_line]).iter() { |
| 115 | println!("{}", line); |
| 116 | } |
| 117 | println!("Line {}-{}/{} ---- {} ----", start_line, end_line, lines.len(), title); |
| 118 | } |
| 119 |