Struct broot::syntactic::SyntacticView
source · pub struct SyntacticView {
pub path: PathBuf,
pub pattern: InputPattern,
/* private fields */
}
Fields§
§path: PathBuf
§pattern: InputPattern
Implementations§
source§impl SyntacticView
impl SyntacticView
sourcepub fn new(
path: &Path,
pattern: InputPattern,
dam: &mut Dam,
con: &AppContext,
no_style: bool
) -> Result<Option<Self>, ProgramError>
pub fn new(
path: &Path,
pattern: InputPattern,
dam: &mut Dam,
con: &AppContext,
no_style: bool
) -> Result<Option<Self>, ProgramError>
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
}
}
sourcepub fn line_counts(&self) -> (usize, usize)
pub fn line_counts(&self) -> (usize, usize)
Give the count of lines which can be seen when scrolling, total count including filtered ones
sourcepub fn get_selected_line(&self) -> Option<String>
pub fn get_selected_line(&self) -> Option<String>
sourcepub fn get_selected_line_number(&self) -> Option<LineNumber>
pub fn get_selected_line_number(&self) -> Option<LineNumber>
sourcepub fn try_select_y(&mut self, y: u16) -> bool
pub fn try_select_y(&mut self, y: u16) -> bool
sourcepub fn select_first(&mut self)
pub fn select_first(&mut self)
Examples found in repository?
More 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)
}
}
sourcepub fn select_last(&mut self)
pub fn select_last(&mut self)
sourcepub fn try_select_line_number(&mut self, number: LineNumber) -> bool
pub fn try_select_line_number(&mut self, number: LineNumber) -> bool
sourcepub fn move_selection(&mut self, dy: i32, cycle: bool)
pub fn move_selection(&mut self, dy: i32, cycle: bool)
sourcepub fn try_scroll(&mut self, cmd: ScrollCommand) -> bool
pub fn try_scroll(&mut self, cmd: ScrollCommand) -> bool
sourcepub fn display(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area,
con: &AppContext
) -> Result<(), ProgramError>
pub fn display(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area,
con: &AppContext
) -> Result<(), ProgramError>
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(())
}
}
}
sourcepub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
pub fn display_info(
&mut self,
w: &mut W,
_screen: Screen,
panel_skin: &PanelSkin,
area: &Area
) -> Result<(), ProgramError>
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(()),
}
}