-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark.cpp
More file actions
181 lines (166 loc) · 7.18 KB
/
mark.cpp
File metadata and controls
181 lines (166 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) 2025 Michael Shebanow and Sunil William Savkar
*
* Original work:
* Copyright (c) 1991 Sunil William Savkar. All rights reserved.
*/
#include "mark_db.hpp"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <unistd.h>
#include <string>
// Main function
int main(int argc, char* argv[]) {
MarkDatabaseManager manager;
if (!manager.initialize()) {
std::cerr << "mark: Must set environment var $MARK_PATH or $MARK_DIR" << std::endl;
return 1;
}
MarkDatabase* db = manager.getDefaultDatabase();
if (!db) {
std::cerr << "mark: No default database available" << std::endl;
return 1;
}
// Get current directory
char* pwd = std::getenv("PWD");
if (!pwd) {
char cwd[1024];
if (!getcwd(cwd, sizeof(cwd))) {
std::cerr << "mark: Unable to get current directory" << std::endl;
return 1;
}
pwd = cwd;
}
// Handle /tmp_mnt prefix (legacy support)
std::string currentDir = pwd;
if (currentDir.substr(0, 8) == "/tmp_mnt") {
currentDir = currentDir.substr(8);
}
// Parse arguments
if (argc == 1) {
// List marks from all databases
for (const auto& entry : manager.getDatabases()) {
if (!entry.alias.empty()) {
std::cout << "\n[" << entry.alias << "]" << std::endl;
} else {
std::cout << "\n[" << entry.path << "]" << std::endl;
}
entry.db->listMarks();
}
return 0;
}
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-h" || arg == "-help") {
std::cout << "Mark Directory\nusage:\tmark <options>\n\n"
<< "option\t\t\tdescription\n\n"
<< "<cr>\n"
<< "-l<ist>\t\t\tLists current marks and their directories\n"
<< "[mark] or [db]:[mark]\tAliases current directory to mark name\n"
<< "\t\t\t\tUse 'db:mark' to specify which database\n"
<< "-rm [mark]\n"
<< "-remove [mark]\t\tRemoves specified mark\n"
<< "-v<ersion>\t\tPrints current version of the program\n"
<< "-h<elp>\t\t\tThis help message\n"
<< "-reset\t\t\tClears all marks in the current environment (no confirmation)\n"
<< "-clear\t\t\tClears all marks with confirmation prompt\n"
<< "-r<efresh>\t\tRefreshes all marks in the current environment\n"
<< "-c [mark]\t\tMake mark cloud-based (backward compat, maps to cloud:mark)\n"
<< "\nexamples:\tmark xxx, mark cloud:xxx, mark -list, mark -reset, mark -clear, mark -rm xxx" << std::endl;
return 0;
} else if (arg == "-v" || arg == "-ver" || arg == "-version") {
std::cout << "mark-setd version 2.0" << std::endl;
return 0;
} else if (arg == "-l" || arg == "-list") {
for (const auto& entry : manager.getDatabases()) {
if (!entry.alias.empty()) {
std::cout << "\n[" << entry.alias << "]" << std::endl;
} else {
std::cout << "\n[" << entry.path << "]" << std::endl;
}
entry.db->listMarks();
}
} else if (arg == "-rm" || arg == "-remove") {
if (i + 1 < argc) {
db->removeMark(argv[++i]);
} else {
std::cerr << "mark: -rm requires a mark name" << std::endl;
}
} else if (arg == "-reset") {
db->resetMarks();
} else if (arg == "-clear") {
// Clear all marks with confirmation
std::cout << "This will remove ALL marks from the database." << std::endl;
std::cout << "Are you sure? (yes/no): ";
std::string confirmation;
std::getline(std::cin, confirmation);
// Convert to lowercase for comparison
std::string lowerConfirmation = confirmation;
for (char& c : lowerConfirmation) {
c = std::tolower(c);
}
if (lowerConfirmation == "yes" || lowerConfirmation == "y") {
if (db->resetMarks()) {
std::cout << "All marks cleared." << std::endl;
} else {
std::cerr << "mark: Failed to clear marks" << std::endl;
return 1;
}
} else {
std::cout << "Operation cancelled." << std::endl;
}
return 0;
} else if (arg == "-r" || arg == "-refresh" || arg == "-ref") {
db->refreshMarks();
} else if (arg == "-c") {
// Cloud mark option (backward compatibility - maps to cloud:mark)
if (i + 1 < argc) {
std::string markName = argv[++i];
MarkDatabase* cloudDb = manager.findDatabase("cloud");
if (!cloudDb) {
std::cerr << "mark: -c requires cloud database (set MARK_PATH or MARK_REMOTE_DIR)" << std::endl;
return 1;
}
std::string existingPath = cloudDb->getMarkPath(markName);
if (!existingPath.empty()) {
std::cout << "mark: Mark \"" << markName << "\" already exists at: " << existingPath << std::endl;
std::cout << "mark: Update to current directory? (y/n): ";
std::string response;
std::getline(std::cin, response);
if (response == "y" || response == "Y" || response == "yes" || response == "Yes") {
cloudDb->addMark(markName, currentDir);
} else {
std::cerr << "mark: Operation cancelled" << std::endl;
return 0;
}
} else {
cloudDb->addMark(markName, currentDir);
}
} else {
std::cerr << "mark: -c requires a mark name" << std::endl;
}
} else if (arg[0] != '-') {
// Check for <db>:<alias> syntax
size_t colonPos = arg.find(':');
if (colonPos != std::string::npos && colonPos > 0 && colonPos < arg.length() - 1) {
// New syntax: mark <db>:<alias>
std::string dbSpec = arg.substr(0, colonPos);
std::string alias = arg.substr(colonPos + 1);
MarkDatabase* targetDb = manager.findDatabase(dbSpec);
if (!targetDb) {
std::cerr << "mark: Failed to create or access database \"" << dbSpec << "\"" << std::endl;
return 1;
}
targetDb->addMark(alias, currentDir);
} else {
// Regular mark (default database)
db->addMark(arg, currentDir);
}
} else {
std::cerr << "mark: unrecognized option: " << arg << std::endl;
}
}
return 0;
}