pub struct SyntacticView {
    pub path: PathBuf,
    pub pattern: InputPattern,
    /* private fields */
}

Fields§

§path: PathBuf§pattern: InputPattern

Implementations§

Return a prepared text view with syntax coloring if possible. May return Ok(None) only when a pattern is given and there was an event before the end of filtering.

Examples found in repository?
src/preview/preview.rs (line 67)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    pub fn with_mode(
        path: &Path,
        mode: PreviewMode,
        con: &AppContext,
    ) -> Result<Self, ProgramError> {
        match mode {
            PreviewMode::Hex => {
                Ok(HexView::new(path.to_path_buf()).map(Self::Hex)?)
            }
            PreviewMode::Image => {
                ImageView::new(path).map(Self::Image)
            }
            PreviewMode::Text => {
                Ok(
                    SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false)
                        .transpose()
                        .expect("syntactic view without pattern shouldn't be none")
                        .map(Self::Syntactic)?,
                )
            }
        }
    }
    /// build an image view, unless the file can't be interpreted
    /// as an image, in which case a hex view is used
    pub fn image(path: &Path) -> Self {
        ImageView::new(path)
            .ok()
            .map(Self::Image)
            .unwrap_or_else(|| Self::hex(path))

    }
    /// build a text preview (maybe with syntaxic coloring) if possible,
    /// a hex (binary) view if content isnt't UTF8, a ZeroLen file if there's
    /// no length (it's probably a linux pseudofile) or a IOError when
    /// there's a IO problem
    pub fn unfiltered_text(
        path: &Path,
        con: &AppContext,
    ) -> Self {
        match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, false) {
            Ok(Some(sv)) => Self::Syntactic(sv),
            Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => {
                debug!("zero len or unmappable file - check if system file");
                Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf()))
            }
            Err(ProgramError::SyntectCrashed { details }) => {
                warn!("syntect crashed with message : {details:?}");
                Self::unstyled_text(path, con)
            }
            // not previewable as UTF8 text
            // we'll try reading it as binary
            Err(ProgramError::UnprintableFile) => Self::hex(path),
            _ => Self::hex(path),
        }
    }
    /// build a text preview with no syntax highlighting, if possible
    pub fn unstyled_text(
        path: &Path,
        con: &AppContext,
    ) -> Self {
        match SyntacticView::new(path, InputPattern::none(), &mut Dam::unlimited(), con, true) {
            Ok(Some(sv)) => Self::Syntactic(sv),
            Err(ProgramError::ZeroLenFile | ProgramError::UnmappableFile) => {
                debug!("zero len or unmappable file - check if system file");
                Self::ZeroLen(ZeroLenFileView::new(path.to_path_buf()))
            }
            // not previewable as UTF8 text - we'll try reading it as binary
            Err(ProgramError::UnprintableFile) => Self::hex(path),
            _ => Self::hex(path),
        }
    }
    /// try to build a filtered text view. Will return None if
    /// the dam gets an event before it's built
    pub fn filtered(
        &self,
        path: &Path,
        pattern: InputPattern,
        dam: &mut Dam,
        con: &AppContext,
    ) -> Option<Self> {
        match self {
            Self::Syntactic(_) => {
                match SyntacticView::new(path, pattern, dam, con, false) {

                    // normal finished loading
                    Ok(Some(sv)) => Some(Self::Syntactic(sv)),

                    // interrupted search
                    Ok(None) => None,

                    // not previewable as UTF8 text
                    // we'll try reading it as binary
                    Err(_) => Some(Self::hex(path)), // FIXME try as unstyled if syntect crashed
                }
            }
            _ => None, // not filterable
        }
    }

Give the count of lines which can be seen when scrolling, total count including filtered ones

Examples found in repository?
src/preview/preview.rs (line 195)
193
194
195
196
197
198
    pub fn get_selected_line(&self) -> Option<String> {
        match self {
            Self::Syntactic(sv) => sv.get_selected_line(),
            _ => None,
        }
    }
Examples found in repository?
src/preview/preview.rs (line 201)
199
200
201
202
203
204
    pub fn get_selected_line_number(&self) -> Option<LineNumber> {
        match self {
            Self::Syntactic(sv) => sv.get_selected_line_number(),
            _ => None,
        }
    }
Examples found in repository?
src/preview/preview.rs (line 213)
211
212
213
214
215
    pub fn unselect(&mut self) {
        if let Self::Syntactic(sv) = self {
            sv.unselect();
        }
    }
Examples found in repository?
src/preview/preview.rs (line 218)
216
217
218
219
220
221
    pub fn try_select_y(&mut self, y: u16) -> bool {
        match self {
            Self::Syntactic(sv) => sv.try_select_y(y),
            _ => false,
        }
    }
Examples found in repository?
src/preview/preview.rs (line 233)
231
232
233
234
235
236
237
    pub fn select_first(&mut self) {
        match self {
            Self::Syntactic(sv) => sv.select_first(),
            Self::Hex(hv) => hv.select_first(),
            _ => {}
        }
    }
More examples
Hide additional examples
src/syntactic/syntactic_view.rs (line 94)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    pub fn new(
        path: &Path,
        pattern: InputPattern,
        dam: &mut Dam,
        con: &AppContext,
        no_style: bool,
    ) -> Result<Option<Self>, ProgramError> {
        let mut sv = Self {
            path: path.to_path_buf(),
            pattern,
            lines: Vec::new(),
            scroll: 0,
            page_height: 0,
            selection_idx: None,
            total_lines_count: 0,
        };
        if sv.read_lines(dam, con, no_style)? {
            sv.select_first();
            Ok(Some(sv))
        } else {
            Ok(None)
        }
    }
Examples found in repository?
src/preview/preview.rs (line 240)
238
239
240
241
242
243
244
    pub fn select_last(&mut self) {
        match self {
            Self::Syntactic(sv) => sv.select_last(),
            Self::Hex(hv) => hv.select_last(),
            _ => {}
        }
    }
Examples found in repository?
src/preview/preview.rs (line 207)
205
206
207
208
209
210
    pub fn try_select_line_number(&mut self, number: usize) -> bool {
        match self {
            Self::Syntactic(sv) => sv.try_select_line_number(number),
            _ => false,
        }
    }
Examples found in repository?
src/preview/preview.rs (line 224)
222
223
224
225
226
227
228
229
230
    pub fn move_selection(&mut self, dy: i32, cycle: bool) {
        match self {
            Self::Syntactic(sv) => sv.move_selection(dy, cycle),
            Self::Hex(hv) => {
                hv.try_scroll(ScrollCommand::Lines(dy));
            }
            _ => {}
        }
    }
Examples found in repository?
src/preview/preview.rs (line 184)
179
180
181
182
183
184
185
186
187
188
    pub fn try_scroll(
        &mut self,
        cmd: ScrollCommand,
    ) -> bool {
        match self {
            Self::Syntactic(sv) => sv.try_scroll(cmd),
            Self::Hex(hv) => hv.try_scroll(cmd),
            _ => false,
        }
    }
Examples found in repository?
src/preview/preview.rs (line 256)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    pub fn display(
        &mut self,
        w: &mut W,
        disc: &DisplayContext,
        area: &Area,
    ) -> Result<(), ProgramError> {
        let panel_skin = &disc.panel_skin;
        let screen = disc.screen;
        let con = &disc.con;
        match self {
            Self::Image(iv) => iv.display(w, disc, area),
            Self::Syntactic(sv) => sv.display(w, screen, panel_skin, area, con),
            Self::ZeroLen(zlv) => zlv.display(w, screen, panel_skin, area),
            Self::Hex(hv) => hv.display(w, screen, panel_skin, area),
            Self::IoError(err) => {
                let mut y = area.top;
                w.queue(cursor::MoveTo(area.left, y))?;
                let mut cw = CropWriter::new(w, area.width as usize);
                cw.queue_str(&panel_skin.styles.default, "An error prevents the preview:")?;
                cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
                y += 1;
                w.queue(cursor::MoveTo(area.left, y))?;
                let mut cw = CropWriter::new(w, area.width as usize);
                cw.queue_g_string(&panel_skin.styles.status_error, err.to_string())?;
                cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
                y += 1;
                while y < area.top + area.height {
                    w.queue(cursor::MoveTo(area.left, y))?;
                    let mut cw = CropWriter::new(w, area.width as usize);
                    cw.fill(&panel_skin.styles.default, &SPACE_FILLING)?;
                    y += 1;
                }
                Ok(())
            }
        }
    }
Examples found in repository?
src/preview/preview.rs (line 290)
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    pub fn display_info(
        &mut self,
        w: &mut W,
        screen: Screen,
        panel_skin: &PanelSkin,
        area: &Area,
    ) -> Result<(), ProgramError> {
        match self {
            Self::Image(iv) => iv.display_info(w, screen, panel_skin, area),
            Self::Syntactic(sv) => sv.display_info(w, screen, panel_skin, area),
            Self::Hex(hv) => hv.display_info(w, screen, panel_skin, area),
            _ => Ok(()),
        }
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.