Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependencies to nightly build of rust compiler. #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove dependencies to nightly build of rust compiler.
  • Loading branch information
Bhavit Sharma committed May 24, 2020
commit e1bd72ca61803bb20ab5312e72eb10b0553ba690
22 changes: 2 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
extern crate termion;
extern crate rustc_version;

use rustc_version::{version_meta, Channel};

extern crate termion;
// use std::process::Command;


fn main() -> Result<(),()> {
// Bail out if compiler isn't a nightly
if let Ok(false) = version_meta().map(|m| m.channel == Channel::Nightly) {
eprint!("{}", termion::color::Fg(termion::color::Red));
eprint!("{}", termion::style::Bold);
eprint!("{}", termion::style::Underline);
eprintln!("NIHGTLY COMPILER required");
eprintln!("Please install a nighlty compiler to proceed: https://rustup.rs/");
eprint!("{}", termion::style::Reset);
eprintln!("rustup toolchain install nightly");
eprintln!("source ~/.cargo/env");

return Err(());
}

fn main() -> Result<(), ()> {
// crates.io doesn't allow question marks in file names
// So we just stuff that in an archive for distribution

70 changes: 43 additions & 27 deletions src/bookmarks.rs
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@ use termion::event::Key;

use std::collections::HashMap;

use crate::fail::{HResult, HError, ErrorLog};
use crate::widget::{Widget, WidgetCore};
use crate::coordinates::Coordinates;
use crate::fail::{ErrorLog, HError, HResult};
use crate::term;
use crate::widget::{Widget, WidgetCore};

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Bookmarks {
@@ -14,8 +14,12 @@ pub struct Bookmarks {

impl Bookmarks {
pub fn new() -> Bookmarks {
let mut bm = Bookmarks { mapping: HashMap::new() };
bm.load().or_else(|_| HError::log("Couldn't load bookmarks!")).ok();
let mut bm = Bookmarks {
mapping: HashMap::new(),
};
bm.load()
.or_else(|_| HError::log("Couldn't load bookmarks!"))
.ok();
bm
}
pub fn add(&mut self, key: char, path: &str) -> HResult<()> {
@@ -24,9 +28,10 @@ impl Bookmarks {
Ok(())
}
pub fn get(&self, key: char) -> HResult<&String> {
let path = self.mapping.get(&key)?;
let path = self.mapping.get(&key).ok_or_else(|| HError::NoneError)?;
Ok(path)
}

pub fn load(&mut self) -> HResult<()> {
let bm_file = crate::paths::bookmark_path()?;

@@ -35,8 +40,7 @@ impl Bookmarks {
}

let bm_content = std::fs::read_to_string(bm_file)?;
let mapping = bm_content.lines()
.fold(HashMap::new(), |mut bm, line| {
let mapping = bm_content.lines().fold(HashMap::new(), |mut bm, line| {
let parts = line.splitn(2, ":").collect::<Vec<&str>>();
if parts.len() == 2 {
if let Some(key) = parts[0].chars().next() {
@@ -62,17 +66,18 @@ impl Bookmarks {
}
pub fn save(&self) -> HResult<()> {
let bm_file = crate::paths::bookmark_path()?;
let bookmarks = self.mapping.iter().map(|(key, path)| {
format!("{}:{}\n", key, path)
}).collect::<String>();
let bookmarks = self
.mapping
.iter()
.map(|(key, path)| format!("{}:{}\n", key, path))
.collect::<String>();

std::fs::write(bm_file, bookmarks)?;

Ok(())
}
}


pub struct BMPopup {
core: WidgetCore,
bookmarks: Bookmarks,
@@ -86,7 +91,7 @@ impl BMPopup {
core: core.clone(),
bookmarks: Bookmarks::new(),
bookmark_path: None,
add_mode: false
add_mode: false,
};
bmpopup.set_coordinates(&core.coordinates).log();
bmpopup
@@ -96,16 +101,16 @@ impl BMPopup {
self.bookmark_path = Some(cwd);
self.refresh()?;
match self.popup() {
Ok(_) => {},
Err(HError::PopupFinnished) => {},
Ok(_) => {}
Err(HError::PopupFinnished) => {}
err @ Err(HError::TerminalResizedError) => err?,
err @ Err(HError::WidgetResizedError) => err?,
err @ Err(_) => err?,
}
self.get_core()?.clear()?;

let bookmark = self.bookmark_path.take();
Ok(bookmark?)
Ok(bookmark.ok_or_else(|| HError::NoneError)?)
}

pub fn add(&mut self, path: &str) -> HResult<()> {
@@ -132,11 +137,11 @@ impl BMPopup {
crate::term::reset(),
key,
path,
padding = padding as usize)
padding = padding as usize
)
}
}


impl Widget for BMPopup {
fn get_core(&self) -> HResult<&WidgetCore> {
Ok(&self.core)
@@ -155,9 +160,11 @@ impl Widget for BMPopup {
fn set_coordinates(&mut self, _: &Coordinates) -> HResult<()> {
let (xsize, ysize) = crate::term::size()?;
let len = self.bookmarks.mapping.len();
let ysize = ysize.saturating_sub( len + 1 );
let ysize = ysize.saturating_sub(len + 1);

self.core.coordinates.set_size_u(xsize.saturating_sub(1), len);
self.core
.coordinates
.set_size_u(xsize.saturating_sub(1), len);
self.core.coordinates.set_position_u(1, ysize);

Ok(())
@@ -169,14 +176,23 @@ impl Widget for BMPopup {
let mut drawlist = String::new();

if !self.add_mode {
let cwd = self.bookmark_path.as_ref()?;
let cwd = self
.bookmark_path
.as_ref()
.ok_or_else(|| HError::NoneError)?;
drawlist += &self.render_line(ypos, &'`', cwd);
}

let bm_list = self.bookmarks.mapping.iter().enumerate().map(|(i, (key, path))| {
let line = i as u16 + ypos + 1;
self.render_line(line, key, path)
}).collect::<String>();
let bm_list = self
.bookmarks
.mapping
.iter()
.enumerate()
.map(|(i, (key, path))| {
let line = i as u16 + ypos + 1;
self.render_line(line, key, path)
})
.collect::<String>();

drawlist += &bm_list;

@@ -186,12 +202,12 @@ impl Widget for BMPopup {
match key {
Key::Ctrl('c') | Key::Esc => {
self.bookmark_path = None;
return HError::popup_finnished()
},
return HError::popup_finnished();
}
Key::Char('`') => return HError::popup_finnished(),
Key::Char(key) => {
if self.add_mode {
let path = self.bookmark_path.take()?;
let path = self.bookmark_path.take().ok_or_else(|| HError::NoneError)?;
self.bookmarks.add(key, &path)?;
self.add_mode = false;
self.bookmarks.save().log();
Loading