-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
3,006 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/target | ||
*.log | ||
*.txt | ||
*.heldbak | ||
*.tmp | ||
Cargo.lock | ||
install | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "held" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
dragonos = [] | ||
|
||
[dependencies] | ||
# 控制term | ||
crossterm = "0.27" | ||
lazy_static = "1.4" | ||
|
||
# 命令解析 | ||
clap = { version = "4.4.7",features = ["derive"] } | ||
|
||
# 日志 | ||
simplelog = "^0.12.1" | ||
log = "0.4" | ||
|
||
# 解析配置文件 | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_yaml = "0.9" | ||
|
||
# 定义标志位 | ||
bitflags = "2.4.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# TOOLCHAIN="+nightly-2023-08-15-x86_64-unknown-linux-gnu" | ||
# RUSTFLAGS+="-C target-feature=+crt-static -C link-arg=-no-pie" | ||
|
||
ifdef DADK_CURRENT_BUILD_DIR | ||
# 如果是在dadk中编译,那么安装到dadk的安装目录中 | ||
INSTALL_DIR = $(DADK_CURRENT_BUILD_DIR) | ||
else | ||
# 如果是在本地编译,那么安装到当前目录下的install目录中 | ||
INSTALL_DIR = ./install | ||
endif | ||
|
||
ifeq ($(ARCH), x86_64) | ||
export RUST_TARGET=x86_64-unknown-linux-musl | ||
else ifeq ($(ARCH), riscv64) | ||
export RUST_TARGET=riscv64gc-unknown-linux-gnu | ||
else | ||
# 默认为x86_86,用于本地编译 | ||
export RUST_TARGET=x86_64-unknown-linux-musl | ||
endif | ||
|
||
run: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) | ||
|
||
build-linux: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) | ||
|
||
build-dragonos: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --features dragonos | ||
|
||
clean: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) | ||
|
||
test: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) | ||
|
||
doc: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc --target $(RUST_TARGET) | ||
|
||
fmt: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt | ||
|
||
fmt-check: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt --check | ||
|
||
run-release: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) --release | ||
|
||
build-release: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --release | ||
|
||
clean-release: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) --release | ||
|
||
test-release: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) --release | ||
|
||
.PHONY: install-dragonos | ||
install-dragonos: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --features dragonos --path . --no-track --root $(INSTALL_DIR) --force | ||
|
||
install-linux: | ||
RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --path . --no-track --root $(INSTALL_DIR) --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
line: | ||
number: | ||
enable: | ||
true | ||
highlight: | ||
enable: | ||
true | ||
color: | ||
0x00ffff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::{io, sync::Arc}; | ||
|
||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; | ||
|
||
use crate::{ | ||
config::appconfig::AppSetting, | ||
utils::{file::FileManager, ui::uicore::Ui}, | ||
}; | ||
|
||
pub struct Application { | ||
file_manager: FileManager, | ||
bak: bool, | ||
ui: Arc<Ui>, | ||
} | ||
|
||
impl Application { | ||
pub fn new(file_path: Option<String>, setting: AppSetting) -> io::Result<Self> { | ||
let bak; | ||
let mut file = if file_path.is_some() { | ||
bak = true; | ||
FileManager::new(file_path.unwrap())? | ||
} else { | ||
bak = false; | ||
FileManager::new("held.tmp".to_string())? | ||
}; | ||
|
||
// 将文件数据读入buf | ||
let buf = file.init(bak)?; | ||
|
||
Ok(Self { | ||
file_manager: file, | ||
bak, | ||
ui: Ui::new(Arc::new(buf), setting), | ||
}) | ||
} | ||
|
||
fn init(&mut self) -> io::Result<()> { | ||
Ui::init_ui()?; | ||
|
||
if !self.bak { | ||
self.ui.start_page_ui()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn run(&mut self) -> io::Result<()> { | ||
enable_raw_mode()?; | ||
self.init()?; | ||
match self.ui.ui_loop() { | ||
Ok(store) => { | ||
if store { | ||
let buffer = &self.ui.core.lock().unwrap().buffer; | ||
self.file_manager.store(buffer)? | ||
} | ||
} | ||
Err(_) => { | ||
// 补救措施:恢复备份文件 | ||
todo!() | ||
} | ||
} | ||
disable_raw_mode()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use crossterm::style::Color; | ||
|
||
#[derive(Debug, serde::Deserialize, Default)] | ||
pub struct DeserializeAppOption { | ||
pub line: Option<DeserializeLineOption>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AppSetting { | ||
pub line: LineSetting, | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, Clone, Copy)] | ||
pub struct DeserializeLineOption { | ||
pub number: Option<DeserializeLineNumber>, | ||
pub highlight: Option<DeserializeHighLightSetting>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct LineSetting { | ||
pub line_num: LineNumberSetting, | ||
pub highlight: HighLightSetting, | ||
|
||
pub prefix_width: usize, | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, Clone, Copy)] | ||
pub struct DeserializeLineNumber { | ||
pub enable: bool, | ||
pub background: Option<u32>, | ||
pub frontground: Option<u32>, | ||
} | ||
|
||
impl DeserializeLineNumber { | ||
pub fn to_line_number_setting(setting: Option<Self>) -> LineNumberSetting { | ||
let mut ret = LineNumberSetting::default(); | ||
if setting.is_none() { | ||
return ret; | ||
} else { | ||
let setting = setting.unwrap(); | ||
if setting.background.is_some() { | ||
let color = setting.background.unwrap(); | ||
let r = (color & 0xff0000) >> 16; | ||
let g = (color & 0x00ff00) >> 8; | ||
let b = color & 0x0000ff; | ||
|
||
ret.background = Color::Rgb { | ||
r: r as u8, | ||
g: g as u8, | ||
b: b as u8, | ||
} | ||
} | ||
|
||
if setting.frontground.is_some() { | ||
let color = setting.frontground.unwrap(); | ||
let r = (color & 0xff0000) >> 16; | ||
let g = (color & 0x00ff00) >> 8; | ||
let b = color & 0x0000ff; | ||
|
||
ret.frontground = Color::Rgb { | ||
r: r as u8, | ||
g: g as u8, | ||
b: b as u8, | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct LineNumberSetting { | ||
pub enable: bool, | ||
pub background: Color, | ||
pub frontground: Color, | ||
} | ||
|
||
impl Default for LineNumberSetting { | ||
fn default() -> Self { | ||
Self { | ||
enable: true, | ||
background: Color::DarkGreen, | ||
frontground: Color::White, | ||
} | ||
} | ||
} | ||
|
||
impl Default for LineSetting { | ||
fn default() -> Self { | ||
Self { | ||
line_num: LineNumberSetting::default(), | ||
highlight: Default::default(), | ||
prefix_width: 0, | ||
} | ||
} | ||
} | ||
|
||
// 高亮选项 | ||
#[derive(Debug, serde::Deserialize, Clone, Copy)] | ||
pub struct DeserializeHighLightSetting { | ||
pub enable: bool, | ||
pub color: u32, | ||
} | ||
|
||
impl DeserializeHighLightSetting { | ||
pub fn to_highlight_setting(&self) -> HighLightSetting { | ||
let r = (self.color & 0xff0000) >> 16; | ||
let g = (self.color & 0x00ff00) >> 8; | ||
let b = self.color & 0x0000ff; | ||
|
||
HighLightSetting { | ||
enable: self.enable, | ||
color: Color::Rgb { | ||
r: r as u8, | ||
g: g as u8, | ||
b: b as u8, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
// 高亮选项 | ||
#[derive(Debug, Clone, Copy)] | ||
pub struct HighLightSetting { | ||
pub enable: bool, | ||
pub color: Color, | ||
} | ||
|
||
impl Default for HighLightSetting { | ||
fn default() -> Self { | ||
Self { | ||
enable: true, | ||
color: Color::DarkYellow, | ||
} | ||
} | ||
} | ||
|
||
impl DeserializeAppOption { | ||
pub fn to_app_setting(&self) -> AppSetting { | ||
let line_setting = match self.line { | ||
Some(setting) => setting.to_line_setting(), | ||
None => LineSetting::default(), | ||
}; | ||
|
||
AppSetting { line: line_setting } | ||
} | ||
} | ||
|
||
impl DeserializeLineOption { | ||
pub fn to_line_setting(&self) -> LineSetting { | ||
let mut highlight = HighLightSetting::default(); | ||
if self.highlight.is_some() { | ||
let h = self.highlight.unwrap(); | ||
highlight = h.to_highlight_setting(); | ||
} | ||
LineSetting { | ||
line_num: DeserializeLineNumber::to_line_number_setting(self.number), | ||
highlight, | ||
prefix_width: 0, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use clap::Parser; | ||
use log::LevelFilter; | ||
|
||
#[derive(Parser)] | ||
#[command(name = "held")] | ||
#[command(author = "[email protected]")] | ||
#[command(version = "1.0")] | ||
#[command(about = "a termial editor", long_about = None)] | ||
pub struct CmdConfig { | ||
/// open file | ||
pub file: Option<String>, | ||
|
||
/// log level | ||
#[arg(value_enum, short, long, default_value = "warn")] | ||
pub level: LevelFilter, | ||
} |
Oops, something went wrong.