TUT/src/bookmark.h
m1ngsama a4c95a6527 feat: Add bookmark management
- Add BookmarkManager class for bookmark CRUD operations
- Store bookmarks in JSON format at ~/.config/tut/bookmarks.json
- Add keyboard shortcuts: B (add), D (remove)
- Add :bookmarks/:bm command to view bookmark list
- Bookmarks page shows clickable links
- Auto-save on add/remove, auto-load on startup
2025-12-27 15:29:44 +08:00

96 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <string>
#include <vector>
#include <ctime>
namespace tut {
/**
* 书签条目
*/
struct Bookmark {
std::string url;
std::string title;
std::time_t added_time;
Bookmark() : added_time(0) {}
Bookmark(const std::string& url, const std::string& title)
: url(url), title(title), added_time(std::time(nullptr)) {}
};
/**
* 书签管理器
*
* 书签存储在 ~/.config/tut/bookmarks.json
*/
class BookmarkManager {
public:
BookmarkManager();
~BookmarkManager();
/**
* 加载书签(从默认路径)
*/
bool load();
/**
* 保存书签(到默认路径)
*/
bool save() const;
/**
* 添加书签
* @return true 如果添加成功false 如果已存在
*/
bool add(const std::string& url, const std::string& title);
/**
* 删除书签
* @return true 如果删除成功
*/
bool remove(const std::string& url);
/**
* 删除书签(按索引)
*/
bool remove_at(size_t index);
/**
* 检查URL是否已收藏
*/
bool contains(const std::string& url) const;
/**
* 获取书签列表
*/
const std::vector<Bookmark>& get_all() const { return bookmarks_; }
/**
* 获取书签数量
*/
size_t count() const { return bookmarks_.size(); }
/**
* 清空所有书签
*/
void clear() { bookmarks_.clear(); }
/**
* 获取配置目录路径
*/
static std::string get_config_dir();
/**
* 获取书签文件路径
*/
static std::string get_bookmarks_path();
private:
std::vector<Bookmark> bookmarks_;
// 确保配置目录存在
static bool ensure_config_dir();
};
} // namespace tut