<commit_before>use alloc::boxed::Box;

use core::cmp;

use common::string::{String, ToString};

use graphics::display::Display;

use schemes::{KScheme, Resource, ResourceSeek, URL};

pub struct DisplayScheme;

// Should there only be one display per session?
pub struct DisplayResource {
	pub display: Box<Display>,
	pub seek: usize,
}

impl Resource for DisplayResource {
	// can't think of when you would wish to duplicate a display
	fn dup(&self) -> Option<Box<Resource>> {
		None
	}

	/// Return the URL for display resource
	fn url(&self) -> URL {
		return URL::from_string(&("display://".to_string()));
	}

	// not sure what to return here
	fn read(&mut self, buf: &mut [u8]) -> Option<usize> {
		None
	}


	fn write(&mut self, buf: &[u8]) -> Option<usize> {
		let display = &mut self.display;

		let size = cmp::min(display.size - self.seek, buf.len());
		unsafe {
			Display::copy_run(buf.as_ptr() as usize,
			                  display.offscreen + self.seek,
			                  size);
		}
		self.seek += size;
		return Some(size);
	}

	fn seek(&mut self, pos: ResourceSeek) -> Option<usize> {
		let end = self.display.size;

		self.seek = match pos {
			ResourceSeek::Start(offset) => cmp::min(end, cmp::max(0, offset)),
			ResourceSeek::Current(offset) => cmp::min(end, cmp::max(0, self.seek as isize + offset) as usize),
			ResourceSeek::End(offset) => cmp::min(end, cmp::max(0, end as isize + offset) as usize),
		};

		return Some(self.seek);
	}

	fn sync(&mut self) -> bool {
		self.display.flip();
		return true;
	}
}

impl KScheme for DisplayScheme {
	fn scheme(&self) -> String {
		return "display".to_string();
	}

	fn open(&mut self, url: &URL) -> Option<Box<Resource>> {
		// TODO: ponder these things:
		// - should display:// be the only only valid url
		//      for this scheme?
		// - maybe "read" should support displays at some other location
		//      like built in screen sharing capability or something
		unsafe {
			return Some(box DisplayResource {
			            display: Display::root(),
			           seek: 0,
			      });
		}
	}
}<commit_msg>Convert tabs to spaces in `kernel::schemes::display`<commit_after>use alloc::boxed::Box;

use core::cmp;

use common::string::{String, ToString};

use graphics::display::Display;

use schemes::{KScheme, Resource, ResourceSeek, URL};

pub struct DisplayScheme;

// Should there only be one display per session?
pub struct DisplayResource {
    pub display: Box<Display>,
    pub seek: usize,
}

impl Resource for DisplayResource {
    // can't think of when you would wish to duplicate a display
    fn dup(&self) -> Option<Box<Resource>> {
        None
    }

    /// Return the URL for display resource
    fn url(&self) -> URL {
        return URL::from_string(&("display://".to_string()));
    }

    // not sure what to return here
    fn read(&mut self, buf: &mut [u8]) -> Option<usize> {
        None
    }


    fn write(&mut self, buf: &[u8]) -> Option<usize> {
        let display = &mut self.display;

        let size = cmp::min(display.size - self.seek, buf.len());
        unsafe {
            Display::copy_run(buf.as_ptr() as usize,
                              display.offscreen + self.seek,
                              size);
        }
        self.seek += size;
        return Some(size);
    }

    fn seek(&mut self, pos: ResourceSeek) -> Option<usize> {
        let end = self.display.size;

        self.seek = match pos {
            ResourceSeek::Start(offset) => cmp::min(end, cmp::max(0, offset)),
            ResourceSeek::Current(offset) => cmp::min(end, cmp::max(0, self.seek as isize + offset) as usize),
            ResourceSeek::End(offset) => cmp::min(end, cmp::max(0, end as isize + offset) as usize),
        };

        return Some(self.seek);
    }

    fn sync(&mut self) -> bool {
        self.display.flip();
        return true;
    }
}

impl KScheme for DisplayScheme {
    fn scheme(&self) -> String {
        return "display".to_string();
    }

    fn open(&mut self, url: &URL) -> Option<Box<Resource>> {
        // TODO: ponder these things:
        // - should display:// be the only only valid url
        //      for this scheme?
        // - maybe "read" should support displays at some other location
        //      like built in screen sharing capability or something
        unsafe {
            return Some(box DisplayResource {
                        display: Display::root(),
                       seek: 0,
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.13966979027219992, 'fraction_numerical': 0.002677376171352075, 'mean_word_length': 2.456437933693138, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1276428', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1361, 'n_words': 409}