├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── file.rs ├── history.rs ├── main.rs └── misc.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.idea 3 | /bin -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "autocfg" 7 | version = "1.1.0" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 10 | 11 | [[package]] 12 | name = "bitflags" 13 | version = "1.3.2" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 16 | 17 | [[package]] 18 | name = "bitflags" 19 | version = "2.4.2" 20 | source = "registry+https://github.com/rust-lang/crates.io-index" 21 | checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" 22 | 23 | [[package]] 24 | name = "cfg-if" 25 | version = "1.0.0" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 28 | 29 | [[package]] 30 | name = "crossterm" 31 | version = "0.27.0" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" 34 | dependencies = [ 35 | "bitflags 2.4.2", 36 | "crossterm_winapi", 37 | "futures-core", 38 | "libc", 39 | "mio", 40 | "parking_lot", 41 | "signal-hook", 42 | "signal-hook-mio", 43 | "winapi", 44 | ] 45 | 46 | [[package]] 47 | name = "crossterm_winapi" 48 | version = "0.9.1" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" 51 | dependencies = [ 52 | "winapi", 53 | ] 54 | 55 | [[package]] 56 | name = "futures-core" 57 | version = "0.3.30" 58 | source = "registry+https://github.com/rust-lang/crates.io-index" 59 | checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" 60 | 61 | [[package]] 62 | name = "libc" 63 | version = "0.2.152" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" 66 | 67 | [[package]] 68 | name = "lock_api" 69 | version = "0.4.11" 70 | source = "registry+https://github.com/rust-lang/crates.io-index" 71 | checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" 72 | dependencies = [ 73 | "autocfg", 74 | "scopeguard", 75 | ] 76 | 77 | [[package]] 78 | name = "log" 79 | version = "0.4.20" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" 82 | 83 | [[package]] 84 | name = "mio" 85 | version = "0.8.10" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" 88 | dependencies = [ 89 | "libc", 90 | "log", 91 | "wasi", 92 | "windows-sys", 93 | ] 94 | 95 | [[package]] 96 | name = "parking_lot" 97 | version = "0.12.1" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 100 | dependencies = [ 101 | "lock_api", 102 | "parking_lot_core", 103 | ] 104 | 105 | [[package]] 106 | name = "parking_lot_core" 107 | version = "0.9.9" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" 110 | dependencies = [ 111 | "cfg-if", 112 | "libc", 113 | "redox_syscall", 114 | "smallvec", 115 | "windows-targets", 116 | ] 117 | 118 | [[package]] 119 | name = "quec" 120 | version = "0.1.5" 121 | dependencies = [ 122 | "crossterm", 123 | "libc", 124 | "term_size", 125 | ] 126 | 127 | [[package]] 128 | name = "redox_syscall" 129 | version = "0.4.1" 130 | source = "registry+https://github.com/rust-lang/crates.io-index" 131 | checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" 132 | dependencies = [ 133 | "bitflags 1.3.2", 134 | ] 135 | 136 | [[package]] 137 | name = "scopeguard" 138 | version = "1.2.0" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 141 | 142 | [[package]] 143 | name = "signal-hook" 144 | version = "0.3.17" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" 147 | dependencies = [ 148 | "libc", 149 | "signal-hook-registry", 150 | ] 151 | 152 | [[package]] 153 | name = "signal-hook-mio" 154 | version = "0.2.3" 155 | source = "registry+https://github.com/rust-lang/crates.io-index" 156 | checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" 157 | dependencies = [ 158 | "libc", 159 | "mio", 160 | "signal-hook", 161 | ] 162 | 163 | [[package]] 164 | name = "signal-hook-registry" 165 | version = "1.4.1" 166 | source = "registry+https://github.com/rust-lang/crates.io-index" 167 | checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" 168 | dependencies = [ 169 | "libc", 170 | ] 171 | 172 | [[package]] 173 | name = "smallvec" 174 | version = "1.13.1" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" 177 | 178 | [[package]] 179 | name = "term_size" 180 | version = "0.3.2" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" 183 | dependencies = [ 184 | "libc", 185 | "winapi", 186 | ] 187 | 188 | [[package]] 189 | name = "wasi" 190 | version = "0.11.0+wasi-snapshot-preview1" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 193 | 194 | [[package]] 195 | name = "winapi" 196 | version = "0.3.9" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 199 | dependencies = [ 200 | "winapi-i686-pc-windows-gnu", 201 | "winapi-x86_64-pc-windows-gnu", 202 | ] 203 | 204 | [[package]] 205 | name = "winapi-i686-pc-windows-gnu" 206 | version = "0.4.0" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 209 | 210 | [[package]] 211 | name = "winapi-x86_64-pc-windows-gnu" 212 | version = "0.4.0" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 215 | 216 | [[package]] 217 | name = "windows-sys" 218 | version = "0.48.0" 219 | source = "registry+https://github.com/rust-lang/crates.io-index" 220 | checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" 221 | dependencies = [ 222 | "windows-targets", 223 | ] 224 | 225 | [[package]] 226 | name = "windows-targets" 227 | version = "0.48.5" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" 230 | dependencies = [ 231 | "windows_aarch64_gnullvm", 232 | "windows_aarch64_msvc", 233 | "windows_i686_gnu", 234 | "windows_i686_msvc", 235 | "windows_x86_64_gnu", 236 | "windows_x86_64_gnullvm", 237 | "windows_x86_64_msvc", 238 | ] 239 | 240 | [[package]] 241 | name = "windows_aarch64_gnullvm" 242 | version = "0.48.5" 243 | source = "registry+https://github.com/rust-lang/crates.io-index" 244 | checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" 245 | 246 | [[package]] 247 | name = "windows_aarch64_msvc" 248 | version = "0.48.5" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" 251 | 252 | [[package]] 253 | name = "windows_i686_gnu" 254 | version = "0.48.5" 255 | source = "registry+https://github.com/rust-lang/crates.io-index" 256 | checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" 257 | 258 | [[package]] 259 | name = "windows_i686_msvc" 260 | version = "0.48.5" 261 | source = "registry+https://github.com/rust-lang/crates.io-index" 262 | checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" 263 | 264 | [[package]] 265 | name = "windows_x86_64_gnu" 266 | version = "0.48.5" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" 269 | 270 | [[package]] 271 | name = "windows_x86_64_gnullvm" 272 | version = "0.48.5" 273 | source = "registry+https://github.com/rust-lang/crates.io-index" 274 | checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" 275 | 276 | [[package]] 277 | name = "windows_x86_64_msvc" 278 | version = "0.48.5" 279 | source = "registry+https://github.com/rust-lang/crates.io-index" 280 | checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" 281 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "quec" 3 | version = "0.1.5" 4 | edition = "2021" 5 | description = "a simple text editor" 6 | license = "GPL-3.0-only" 7 | authors = ["bgkillas "] 8 | readme = "README.md" 9 | keywords = ["cli","editor","text-editor"] 10 | categories = ["text-editors"] 11 | repository = "https://github.com/bgkillas/quec" 12 | 13 | [profile.release] 14 | codegen-units = 1 15 | lto = true 16 | strip = true 17 | 18 | [dependencies] 19 | 20 | [target.'cfg(not(unix))'.dependencies] 21 | term_size = "0.3.2" 22 | crossterm = {version="0.27.0", default-features=false,features = ["event-stream","windows"]} 23 | 24 | [target.'cfg(unix)'.dependencies] 25 | libc = "0.2.149" 26 | crossterm = {version="0.27.0", default-features=false,features = ["event-stream"]} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # quec 2 | [![crates.io](https://img.shields.io/crates/v/quec.svg)](https://crates.io/crates/quec) [![AUR](https://img.shields.io/aur/version/quec.svg)](https://aur.archlinux.org/packages/quec/) 3 | 4 | history files located ``$HOME/.quec/`` ``C:\\Users\\USERNAME\\AppData\\Roaming\\quec`` 5 | ## wont fix issues 6 | non-ascii characters that are not 1 char long messes stuff up 7 | 8 | 0 width ascii characters are deleted 9 | 10 | tab looks like space 11 | 12 | windows files are converted to linux 13 | 14 | requires modern terminal on windows, like windows terminal 15 | ## keybindings 16 | ``i`` edit mode 17 | 18 | ``esc`` exit all modes 19 | 20 | ``h`` left 21 | 22 | ``l`` right 23 | 24 | ``j`` down 25 | 26 | ``k`` up 27 | 28 | ``0`` move to beginning of line 29 | 30 | ``$`` move to end of line 31 | 32 | ``ins`` insert mode 33 | 34 | ``page down`` move 1 page down 35 | 36 | ``page up`` move 1 page up 37 | 38 | ``home`` to start of file 39 | 40 | ``end`` to end of file 41 | 42 | ``y`` copy line 43 | 44 | ``d`` cut line 45 | 46 | ``p`` print line 47 | 48 | ``w`` save 49 | 50 | ``s`` save as 51 | 52 | ``o`` open file 53 | 54 | `` ` `` go to next file 55 | 56 | ``~`` go to last file 57 | 58 | ``q`` quit 59 | 60 | ``u`` undo 61 | 62 | ``U`` redo 63 | 64 | ``R`` replace all 65 | 66 | ``g`` goto line # 67 | 68 | ``/`` search mode 69 | 70 | ``v`` greek mode -------------------------------------------------------------------------------- /src/file.rs: -------------------------------------------------------------------------------- 1 | use crate::{ 2 | history::History, 3 | misc::{get_file, read_single_char}, 4 | }; 5 | use crossterm::terminal; 6 | use std::{ 7 | fs::File, 8 | io::{BufRead, BufReader, Read, Stdout, Write}, 9 | }; 10 | pub struct Files { 11 | pub lines: Vec>, 12 | pub history: History, 13 | pub save_file_path: String, 14 | pub history_file: String, 15 | pub placement: usize, 16 | pub line: usize, 17 | pub start: usize, 18 | pub top: usize, 19 | pub cursor: usize, 20 | } 21 | pub fn save_file(files: &mut Files, history_dir: &str) -> String { 22 | files.lines = files 23 | .lines 24 | .iter() 25 | .map(|line| line.iter().collect::().trim_end().chars().collect()) 26 | .collect(); 27 | let mut result: Vec = files 28 | .lines 29 | .iter() 30 | .map(|line| line.iter().collect::().as_bytes().to_vec()) 31 | .flat_map(|mut line| { 32 | line.push(b'\n'); 33 | line.into_iter() 34 | }) 35 | .collect(); 36 | result.pop(); 37 | while let Some(last) = result.last() { 38 | if *last == b'\n' { 39 | result.pop(); 40 | files.lines.pop(); 41 | } else { 42 | break; 43 | } 44 | } 45 | result.push(b'\n'); 46 | let mut err = String::new(); 47 | match File::create(files.save_file_path.clone()) { 48 | Ok(mut file) => { 49 | file.write_all(&result).unwrap(); 50 | loop { 51 | if !files.history.list.is_empty() 52 | && ( 53 | files.history.list[0].add, 54 | files.history.list[0].split, 55 | files.history.list[0].line.clone(), 56 | ) == (true, true, None) 57 | && files.history.list[0].pos.0 >= files.lines.len() 58 | { 59 | files.history.list.remove(0); 60 | } else { 61 | break; 62 | } 63 | } 64 | if !files.history.list.is_empty() { 65 | if files.history_file.is_empty() { 66 | files.history_file = get_file(&files.save_file_path, history_dir); 67 | } 68 | File::create(files.history_file.clone()) 69 | .unwrap() 70 | .write_all(&files.history.to_bytes()) 71 | .unwrap(); 72 | } else { 73 | let _ = std::fs::remove_file(files.history_file.clone()); 74 | } 75 | } 76 | Err(e) => err = e.to_string(), 77 | } 78 | err 79 | } 80 | pub fn open_file(save_file_path: &str, history_dir: &str) -> Files { 81 | let mut history_file = String::new(); 82 | let (mut lines, history) = if File::open(save_file_path).is_err() { 83 | ( 84 | Vec::new(), 85 | History { 86 | pos: 0, 87 | list: Vec::new(), 88 | }, 89 | ) 90 | } else { 91 | let f = BufReader::new(File::open(save_file_path).unwrap()) 92 | .lines() 93 | .map(|l| match l { 94 | Ok(l) => l 95 | .chars() 96 | .filter(|c| { 97 | !c.is_ascii() 98 | || c.is_ascii_graphic() 99 | || c == &' ' 100 | || c == &'\t' 101 | || c == &'\n' 102 | }) 103 | .collect::>(), 104 | Err(e) => { 105 | println!("\x1b[?1049l{}", e); 106 | terminal::disable_raw_mode().unwrap(); 107 | std::process::exit(1); 108 | } 109 | }) 110 | .collect::>>(); 111 | history_file = get_file(save_file_path, history_dir); 112 | ( 113 | f, 114 | if let Ok(mut f) = File::open(history_file.clone()) { 115 | let mut read_bytes = Vec::new(); 116 | f.read_to_end(&mut read_bytes).unwrap(); 117 | History::from_bytes(&read_bytes) 118 | } else { 119 | History { 120 | pos: 0, 121 | list: Vec::new(), 122 | } 123 | }, 124 | ) 125 | }; 126 | if lines.is_empty() { 127 | lines.push(Vec::new()); 128 | } 129 | Files { 130 | lines, 131 | history, 132 | history_file, 133 | save_file_path: save_file_path.to_string(), 134 | placement: 0, 135 | line: 0, 136 | top: 0, 137 | start: 0, 138 | cursor: 0, 139 | } 140 | } 141 | pub fn get_word(stdout: &mut Stdout, height: usize) -> Result { 142 | let mut index = 0; 143 | let mut word = Vec::new(); 144 | print!("\x1b[H\x1b[{}B\x1b[K\x1b[G", height + 1,); 145 | stdout.flush().unwrap(); 146 | loop { 147 | let c = read_single_char(); 148 | match c { 149 | '\x1C' if index != word.len() => { 150 | //right 151 | index += 1; 152 | } 153 | '\x1B' if index != 0 => { 154 | //left 155 | index -= 1; 156 | } 157 | '\x08' if index != 0 => { 158 | word.remove(index - 1); 159 | index = index.saturating_sub(1); 160 | } 161 | '\x1A' => { 162 | return Err(()); 163 | } 164 | '\n' => break, 165 | '\0' => {} 166 | _ if !c.is_ascii() || c.is_ascii_graphic() || c == ' ' || c == '\t' || c == '\n' => { 167 | word.insert(index, c); 168 | index += 1; 169 | } 170 | _ => {} 171 | } 172 | print!( 173 | "\x1b[H\x1b[{}B\x1b[K{}\x1b[G{}", 174 | height + 1, 175 | word.iter().collect::(), 176 | if index == 0 { 177 | String::new() 178 | } else { 179 | "\x1b[".to_owned() + &index.to_string() + "C" 180 | }, 181 | ); 182 | stdout.flush().unwrap(); 183 | } 184 | Ok(word.iter().collect()) 185 | } 186 | -------------------------------------------------------------------------------- /src/history.rs: -------------------------------------------------------------------------------- 1 | #[derive(Clone)] 2 | pub struct History { 3 | pub pos: usize, 4 | pub list: Vec, 5 | } 6 | #[derive(Clone)] 7 | pub struct Point { 8 | pub add: bool, 9 | pub split: bool, 10 | pub pos: (usize, usize), 11 | pub char: char, 12 | pub line: Option>, 13 | } 14 | //stolen from chatgpt, probably is innefficient 15 | impl History { 16 | pub fn to_bytes(&self) -> Vec { 17 | let mut bytes = Vec::new(); 18 | bytes.extend(&self.pos.to_le_bytes()); 19 | bytes.extend(&self.list.len().to_le_bytes()); 20 | for point in &self.list { 21 | let point_bytes = point.to_bytes(); 22 | bytes.extend(&point_bytes.len().to_le_bytes()); 23 | bytes.extend(point_bytes); 24 | } 25 | bytes 26 | } 27 | pub fn from_bytes(bytes: &[u8]) -> History { 28 | let mut cursor = 0; 29 | let pos = byte(bytes, cursor); 30 | cursor += 8; 31 | let list_len = byte(bytes, cursor); 32 | cursor += 8; 33 | let mut list = Vec::with_capacity(list_len); 34 | for _ in 0..list_len { 35 | let point_size = byte(bytes, cursor); 36 | cursor += 8; 37 | let point_bytes = &bytes[cursor..cursor + point_size]; 38 | list.push(Point::from_bytes(point_bytes)); 39 | cursor += point_size; 40 | } 41 | History { pos, list } 42 | } 43 | } 44 | impl Point { 45 | fn to_bytes(&self) -> Vec { 46 | let mut bytes = Vec::new(); 47 | bytes.extend(&[self.add as u8, self.split as u8]); 48 | bytes.extend(&self.pos.0.to_le_bytes()); 49 | bytes.extend(&self.pos.1.to_le_bytes()); 50 | let bind = self.char.to_string(); 51 | let char_bytes = bind.as_bytes(); 52 | bytes.push(char_bytes.len() as u8); 53 | bytes.extend(char_bytes); 54 | match &self.line { 55 | Some(line) => { 56 | bytes.push(1); 57 | bytes.extend(&line.len().to_le_bytes()); 58 | bytes.extend(line.iter().collect::().as_bytes()); 59 | } 60 | None => { 61 | bytes.push(0); 62 | } 63 | } 64 | bytes 65 | } 66 | fn from_bytes(bytes: &[u8]) -> Point { 67 | let mut cursor = 0; 68 | let add = bytes[cursor] != 0; 69 | cursor += 1; 70 | let split = bytes[cursor] != 0; 71 | cursor += 1; 72 | let pos_0 = byte(bytes, cursor); 73 | cursor += 8; 74 | let pos_1 = byte(bytes, cursor); 75 | cursor += 8; 76 | let char_len = bytes[cursor] as usize; 77 | cursor += 1; 78 | let char = std::str::from_utf8(&bytes[cursor..cursor + char_len]) 79 | .unwrap() 80 | .chars() 81 | .next() 82 | .unwrap(); 83 | cursor += char_len; 84 | let line = if bytes[cursor] == 1 { 85 | cursor += 1; 86 | let len = byte(bytes, cursor); 87 | cursor += 8; 88 | let line_string = std::str::from_utf8(&bytes[cursor..cursor + len]) 89 | .unwrap_or("") 90 | .to_string(); 91 | Some(line_string.chars().collect()) 92 | } else { 93 | None 94 | }; 95 | Point { 96 | add, 97 | split, 98 | pos: (pos_0, pos_1), 99 | char, 100 | line, 101 | } 102 | } 103 | } 104 | fn byte(bytes: &[u8], cursor: usize) -> usize { 105 | usize::from_le_bytes([ 106 | bytes[cursor], 107 | bytes[cursor + 1], 108 | bytes[cursor + 2], 109 | bytes[cursor + 3], 110 | bytes[cursor + 4], 111 | bytes[cursor + 5], 112 | bytes[cursor + 6], 113 | bytes[cursor + 7], 114 | ]) 115 | } 116 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod file; 2 | mod history; 3 | mod misc; 4 | use crate::{ 5 | file::{get_word, open_file, save_file, Files}, 6 | history::{History, Point}, 7 | misc::{ 8 | clear, clear_line, exit, fix_history, fix_top, get_dimensions, help, print_line_number, 9 | read_single_char, 10 | }, 11 | Mode::{Default, Digraph, Edit, Insert, Search}, 12 | }; 13 | use crossterm::terminal; 14 | use std::{ 15 | cmp::min, 16 | env::{args, var}, 17 | fs::{canonicalize, create_dir}, 18 | io::{stdout, Write}, 19 | time::Instant, 20 | }; 21 | fn main() { 22 | let args = &args().collect::>()[1..]; 23 | if !args.is_empty() { 24 | match args[0].as_str() { 25 | "--help" | "-h" => { 26 | help(); 27 | std::process::exit(0); 28 | } 29 | "--version" | "-v" => { 30 | println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); 31 | std::process::exit(0); 32 | } 33 | _ => {} 34 | } 35 | } 36 | let mut stdout = stdout(); 37 | print!("\x1b[?1049h\x1b[H\x1b[J"); 38 | #[cfg(unix)] 39 | let history_dir = var("HOME").unwrap() + "/.quec/"; 40 | #[cfg(not(unix))] 41 | let history_dir = &format!( 42 | "C:\\Users\\{}\\AppData\\Roaming\\quec\\", 43 | var("USERNAME").unwrap() 44 | ); 45 | let _ = create_dir(history_dir.clone()); 46 | let (mut height, mut width) = get_dimensions(); 47 | let mut files: Vec; 48 | if args.is_empty() { 49 | files = vec![Files { 50 | lines: vec![Vec::new()], 51 | history: History { 52 | pos: 0, 53 | list: Vec::new(), 54 | }, 55 | save_file_path: String::new(), 56 | history_file: String::new(), 57 | placement: 0, 58 | line: 0, 59 | start: 0, 60 | top: 0, 61 | cursor: 0, 62 | }] 63 | } else { 64 | files = Vec::with_capacity(args.len()); 65 | for arg in args { 66 | files.push(open_file(arg, &history_dir)); 67 | } 68 | } 69 | let mut n = 0; 70 | let mut clip = Vec::new(); 71 | terminal::enable_raw_mode().unwrap(); 72 | 'outer: loop { 73 | let mut top = files[n].top; 74 | let mut start = files[n].start; 75 | let mut line = files[n].line; 76 | let mut placement = files[n].placement; 77 | clear(&files[n].lines, top, height, start, width); 78 | print_line_number( 79 | height, 80 | line, 81 | placement, 82 | top, 83 | start, 84 | files[n].save_file_path.clone(), 85 | ); 86 | stdout.flush().unwrap(); 87 | let mut saved = true; 88 | let mut err = String::new(); 89 | let mut mode: Mode = Default; 90 | let mut ln: (usize, usize) = (0, 0); 91 | let mut orig: (usize, usize) = (0, 0); 92 | let mut word: Vec = Vec::new(); 93 | let mut time; 94 | loop { 95 | if (height, width) != get_dimensions() { 96 | (height, width) = get_dimensions(); 97 | top = fix_top(top, line, height); 98 | start = fix_top(start, placement, width); 99 | clear(&files[n].lines, top, height, start, width); 100 | } 101 | if files[n].history.list.len() >= 1000 { 102 | files[n].history.list.drain(1000..); 103 | } 104 | if files[n].history.pos > files[n].history.list.len() { 105 | files[n].history.list.clear(); 106 | } 107 | time = Instant::now(); 108 | let c = read_single_char(); 109 | match c { 110 | '\n' if mode != Search => { 111 | //enter 112 | if mode == Edit || mode == Digraph { 113 | saved = false; 114 | line += 1; 115 | if time.elapsed().as_millis() != 0 { 116 | let mut ln: Vec = files[n].lines[line - 1][..placement] 117 | .iter() 118 | .take_while(|&&c| c == ' ' || c == '\t') 119 | .cloned() 120 | .collect(); 121 | let count = ln.len(); 122 | ln.extend::>( 123 | files[n].lines[line - 1].drain(placement..).collect(), 124 | ); 125 | files[n].lines.insert(line, ln); 126 | placement = count 127 | } else { 128 | let ln = files[n].lines[line - 1].drain(placement..).collect(); 129 | files[n].lines.insert(line, ln); 130 | placement = 0; 131 | } 132 | files[n].cursor = placement; 133 | start = 0; 134 | if line == height + top { 135 | top += 1; 136 | } 137 | clear(&files[n].lines, top, height, start, width); 138 | fix_history(&mut files[n].history); 139 | files[n].history.list.insert( 140 | 0, 141 | Point { 142 | add: true, 143 | split: true, 144 | pos: (line, placement), 145 | char: '\n', 146 | line: None, 147 | }, 148 | ); 149 | } else { 150 | if line + 1 != files[n].lines.len() { 151 | line += 1; 152 | } 153 | placement = files[n].lines[line] 154 | .iter() 155 | .take_while(|&&c| c == ' ' || c == '\t') 156 | .count(); 157 | let s = start; 158 | start = fix_top(start, placement, width); 159 | if line == height + top { 160 | top += 1; 161 | clear(&files[n].lines, top, height, start, width); 162 | } else if s != start { 163 | clear(&files[n].lines, top, height, start, width); 164 | } 165 | } 166 | } 167 | '\x08' => { 168 | //backspace 169 | if mode == Edit || mode == Digraph { 170 | saved = false; 171 | if placement == 0 { 172 | if line == 0 { 173 | continue; 174 | } 175 | line -= 1; 176 | placement = files[n].lines[line].len(); 177 | let t = files[n].lines.remove(line + 1); 178 | files[n].lines[line].extend(t); 179 | start = fix_top(start, placement, width); 180 | clear(&files[n].lines, top, height, start, width); 181 | print!("\x1b[E\x1b[G\x1b[K"); 182 | fix_history(&mut files[n].history); 183 | files[n].history.list.insert( 184 | 0, 185 | Point { 186 | add: false, 187 | split: true, 188 | pos: (line, placement), 189 | char: '\0', 190 | line: None, 191 | }, 192 | ) 193 | } else { 194 | placement -= 1; 195 | fix_history(&mut files[n].history); 196 | let ln = files[n].lines[line].remove(placement); 197 | files[n].history.list.insert( 198 | 0, 199 | Point { 200 | add: false, 201 | split: false, 202 | pos: (line, placement), 203 | char: ln, 204 | line: None, 205 | }, 206 | ); 207 | if placement == files[n].lines[line].len() { 208 | if placement + 1 == start { 209 | start -= 1; 210 | clear(&files[n].lines, top, height, start, width); 211 | } else { 212 | print!("\x1b[D\x1b[K"); 213 | } 214 | } else if placement + 1 == start { 215 | start -= 1; 216 | clear(&files[n].lines, top, height, start, width); 217 | } else { 218 | print!("\x1b[D"); 219 | clear_line(&files[n].lines, line, placement, width, start) 220 | } 221 | } 222 | files[n].cursor = placement; 223 | } else if mode == Search && !word.is_empty() { 224 | word.pop(); 225 | } 226 | } 227 | '\x06' => { 228 | //Delete 229 | saved = false; 230 | if placement == files[n].lines[line].len() { 231 | if line + 1 == files[n].lines.len() { 232 | continue; 233 | } 234 | let t = files[n].lines.remove(line + 1); 235 | files[n].lines[line].extend(t); 236 | start = fix_top(start, placement, width); 237 | clear(&files[n].lines, top, height, start, width); 238 | print!("\x1b[E\x1b[G\x1b[K"); 239 | fix_history(&mut files[n].history); 240 | files[n].history.list.insert( 241 | 0, 242 | Point { 243 | add: false, 244 | split: true, 245 | pos: (line, placement), 246 | char: '\0', 247 | line: None, 248 | }, 249 | ) 250 | } else { 251 | fix_history(&mut files[n].history); 252 | let ln = files[n].lines[line].remove(placement); 253 | files[n].history.list.insert( 254 | 0, 255 | Point { 256 | add: false, 257 | split: false, 258 | pos: (line, placement), 259 | char: ln, 260 | line: None, 261 | }, 262 | ); 263 | clear(&files[n].lines, top, height, start, width); 264 | } 265 | files[n].cursor = placement; 266 | } 267 | '\x15' if (mode == Edit || mode == Digraph) && placement != 0 => { 268 | //ctrl+back 269 | saved = false; 270 | let initial = placement; 271 | let mut did = false; 272 | let mut on_white = files[n].lines[line][placement - 1].is_whitespace(); 273 | for (i, c) in files[n].lines[line][..placement - 1] 274 | .iter() 275 | .rev() 276 | .enumerate() 277 | { 278 | if c.is_whitespace() { 279 | if !on_white { 280 | placement -= i; 281 | placement -= 1; 282 | did = true; 283 | files[n].cursor = placement; 284 | break; 285 | } 286 | } else { 287 | on_white = false; 288 | } 289 | } 290 | if !did { 291 | placement = 0; 292 | } 293 | fix_history(&mut files[n].history); 294 | let ln = files[n].lines[line].drain(placement..initial).collect(); 295 | files[n].history.list.insert( 296 | 0, 297 | Point { 298 | add: false, 299 | split: true, 300 | pos: (line, placement), 301 | char: '\0', 302 | line: Some(ln), 303 | }, 304 | ); 305 | if start > placement { 306 | start = placement; 307 | clear(&files[n].lines, top, height, start, width); 308 | } else { 309 | print!("\x1b[{}D", initial - placement); 310 | clear_line(&files[n].lines, line, placement, width, start) 311 | } 312 | } 313 | '\x01' => { 314 | //home 315 | placement = 0; 316 | line = 0; 317 | if top != 0 || start != 0 { 318 | top = 0; 319 | start = 0; 320 | clear(&files[n].lines, top, height, start, width); 321 | } 322 | files[n].cursor = placement; 323 | if mode == Search { 324 | ln = (line, placement); 325 | } 326 | } 327 | '\x02' => { 328 | //end 329 | line = files[n].lines.len() - 1; 330 | placement = files[n].lines[line].len(); 331 | if files[n].lines.len() > height || placement > width { 332 | top = files[n].lines.len().saturating_sub(height); 333 | start = placement.saturating_sub(width); 334 | clear(&files[n].lines, top, height, start, width); 335 | } 336 | files[n].cursor = placement; 337 | if mode == Search { 338 | ln = (line, placement); 339 | } 340 | } 341 | '\x03' => { 342 | //page up 343 | placement = 0; 344 | if line < height { 345 | top = 0; 346 | line = 0; 347 | let s = start; 348 | start = fix_top(start, placement, width); 349 | if s != 0 { 350 | clear(&files[n].lines, top, height, start, width); 351 | } 352 | } else { 353 | line -= height; 354 | if height < top { 355 | top -= height; 356 | } else { 357 | top = 0; 358 | } 359 | start = fix_top(start, placement, width); 360 | clear(&files[n].lines, top, height, start, width); 361 | } 362 | files[n].cursor = placement; 363 | if mode == Search { 364 | ln = (line, placement); 365 | } 366 | } 367 | '\x04' => { 368 | //page down 369 | if line + height >= files[n].lines.len() { 370 | top = files[n].lines.len().saturating_sub(height); 371 | line = files[n].lines.len() - 1; 372 | placement = files[n].lines[line].len(); 373 | start = fix_top(start, placement, width); 374 | clear(&files[n].lines, top, height, start, width); 375 | } else { 376 | placement = 0; 377 | line += height; 378 | top += height; 379 | if top + height > files[n].lines.len() { 380 | top = files[n].lines.len() - height; 381 | } 382 | start = fix_top(start, placement, width); 383 | clear(&files[n].lines, top, height, start, width); 384 | } 385 | files[n].cursor = placement; 386 | if mode == Search { 387 | ln = (line, placement); 388 | } 389 | } 390 | '\x12' if placement != 0 => { 391 | //ctrl+left 392 | let mut did = false; 393 | let mut on_white = files[n].lines[line][placement - 1].is_whitespace(); 394 | for (i, c) in files[n].lines[line][..placement - 1] 395 | .iter() 396 | .rev() 397 | .enumerate() 398 | { 399 | if c.is_whitespace() { 400 | if !on_white { 401 | placement -= i; 402 | placement -= 1; 403 | did = true; 404 | break; 405 | } 406 | } else { 407 | on_white = false; 408 | } 409 | } 410 | if !did { 411 | placement = 0; 412 | } 413 | files[n].cursor = placement; 414 | if start > placement { 415 | start = placement; 416 | clear(&files[n].lines, top, height, start, width); 417 | } 418 | } 419 | '\x13' if files[n].lines[line].len() != placement => { 420 | //ctrl+right 421 | let mut did = false; 422 | let mut on_white = files[n].lines[line][placement].is_whitespace(); 423 | for (i, c) in files[n].lines[line][placement..].iter().enumerate() { 424 | if !c.is_whitespace() { 425 | if on_white { 426 | placement += i; 427 | did = true; 428 | break; 429 | } 430 | } else { 431 | on_white = true 432 | } 433 | } 434 | if !did { 435 | placement = files[n].lines[line].len(); 436 | } 437 | files[n].cursor = placement; 438 | if start + width < placement { 439 | start += placement - (start + width) + 1; 440 | clear(&files[n].lines, top, height, start, width); 441 | } 442 | } 443 | '\x1B' | 'h' if c != 'h' || mode == Default => { 444 | //left 445 | if placement == 0 { 446 | if line == 0 { 447 | continue; 448 | } 449 | line -= 1; 450 | placement = files[n].lines[line].len(); 451 | let s = start; 452 | start = fix_top(start, placement, width); 453 | if line + 1 == top { 454 | top -= 1; 455 | if placement + 1 == start { 456 | start -= 1; 457 | } 458 | clear(&files[n].lines, top, height, start, width); 459 | } else if start != s { 460 | if placement + 1 == start { 461 | start -= 1; 462 | } 463 | clear(&files[n].lines, top, height, start, width); 464 | } else if placement + 1 == start { 465 | start -= 1; 466 | clear(&files[n].lines, top, height, start, width); 467 | } 468 | } else { 469 | placement -= 1; 470 | if placement + 1 == start { 471 | start -= 1; 472 | clear(&files[n].lines, top, height, start, width); 473 | } 474 | } 475 | files[n].cursor = placement; 476 | if mode == Search { 477 | ln = (line, placement); 478 | } 479 | } 480 | '\x1C' | 'l' if c != 'l' || mode == Default => { 481 | //right 482 | if placement == files[n].lines[line].len() { 483 | if line + 1 != files[n].lines.len() { 484 | placement = 0; 485 | line += 1; 486 | let s = start; 487 | start = fix_top(start, placement, width); 488 | if line == height + top { 489 | top += 1; 490 | if placement == width + start { 491 | start += 1; 492 | } 493 | clear(&files[n].lines, top, height, start, width); 494 | } else if start != s { 495 | if placement == width + start { 496 | start += 1; 497 | } 498 | clear(&files[n].lines, top, height, start, width); 499 | } else if placement == width + start { 500 | start += 1; 501 | clear(&files[n].lines, top, height, start, width); 502 | } 503 | } 504 | } else { 505 | placement += 1; 506 | if placement == width + start { 507 | start += 1; 508 | clear(&files[n].lines, top, height, start, width); 509 | } 510 | } 511 | files[n].cursor = placement; 512 | if mode == Search { 513 | ln = (line, placement); 514 | } 515 | } 516 | '\x1D' | 'k' if c != 'k' || mode == Default => { 517 | //up 518 | if line == 0 { 519 | placement = 0; 520 | files[n].cursor = 0; 521 | if start != 0 { 522 | start = 0; 523 | if line + 1 == top { 524 | top -= 1; 525 | } 526 | clear(&files[n].lines, top, height, start, width); 527 | } 528 | } else { 529 | line -= 1; 530 | if files[n].cursor != 0 { 531 | if files[n].lines[line].len() > files[n].cursor { 532 | placement = files[n].cursor; 533 | } else if placement < files[n].cursor 534 | || files[n].lines[line].len() < placement 535 | { 536 | if files[n].lines[line].is_empty() { 537 | placement = 0; 538 | } else { 539 | placement = files[n].lines[line].len(); 540 | } 541 | } 542 | let s = start; 543 | start = fix_top(start, placement, width); 544 | if s != start { 545 | if line + 1 == top { 546 | top -= 1; 547 | } 548 | clear(&files[n].lines, top, height, start, width); 549 | } else if line + 1 == top { 550 | top -= 1; 551 | clear(&files[n].lines, top, height, start, width); 552 | } 553 | } else if line + 1 == top { 554 | top -= 1; 555 | clear(&files[n].lines, top, height, start, width); 556 | } 557 | } 558 | if mode == Search { 559 | ln = (line, placement); 560 | } 561 | } 562 | '\x1E' | 'j' if c != 'j' || mode == Default => { 563 | //down 564 | if line + 1 == files[n].lines.len() { 565 | if !files[n].lines[line].is_empty() { 566 | placement = files[n].lines[line].len(); 567 | files[n].cursor = placement; 568 | let s = start; 569 | start = fix_top(start, placement, width); 570 | if s != start { 571 | clear(&files[n].lines, top, height, start, width); 572 | } 573 | } 574 | } else { 575 | line += 1; 576 | if files[n].lines[line].is_empty() { 577 | placement = 0; 578 | } else if files[n].cursor != 0 { 579 | if files[n].lines[line].len() > files[n].cursor { 580 | placement = files[n].cursor; 581 | } else if placement < files[n].cursor 582 | || files[n].lines[line].len() < placement 583 | { 584 | placement = files[n].lines[line].len(); 585 | } 586 | } 587 | let s = start; 588 | start = fix_top(start, placement, width); 589 | if s != start { 590 | if line == height + top { 591 | top += 1; 592 | } 593 | clear(&files[n].lines, top, height, start, width); 594 | } else if line == height + top { 595 | top += 1; 596 | clear(&files[n].lines, top, height, start, width); 597 | } 598 | } 599 | if mode == Search { 600 | ln = (line, placement); 601 | } 602 | } 603 | '\x1A' => { 604 | //esc 605 | mode = Default; 606 | err.clear(); 607 | } 608 | '`' if mode == Default && n + 1 != files.len() => { 609 | //next file 610 | files[n].placement = placement; 611 | files[n].line = line; 612 | files[n].start = start; 613 | files[n].top = top; 614 | n += 1; 615 | print!("\x1b[H\x1b[J"); 616 | continue 'outer; 617 | } 618 | '~' if mode == Default && n != 0 => { 619 | //last file 620 | files[n].placement = placement; 621 | files[n].line = line; 622 | files[n].start = start; 623 | files[n].top = top; 624 | n -= 1; 625 | print!("\x1b[H\x1b[J"); 626 | continue 'outer; 627 | } 628 | '0' if mode == Default => { 629 | //start of line 630 | placement = 0; 631 | files[n].cursor = placement; 632 | if start != 0 { 633 | start = 0; 634 | clear(&files[n].lines, top, height, start, width); 635 | } 636 | } 637 | '$' if mode == Default => { 638 | //end of line 639 | placement = files[n].lines[line].len(); 640 | files[n].cursor = placement; 641 | if placement > start + width { 642 | start = placement - width + 1; 643 | clear(&files[n].lines, top, height, start, width); 644 | } 645 | } 646 | 'w' if mode == Default => { 647 | //save 648 | err = save_file(&mut files[n], &history_dir); 649 | line = min(line, files[n].lines.len() - 1); 650 | placement = min(placement, files[n].lines[line].len()); 651 | top = fix_top(top, line, height); 652 | start = fix_top(start, placement, width); 653 | if err.is_empty() { 654 | saved = true; 655 | } 656 | } 657 | 'y' if mode == Default => { 658 | //copy line 659 | clip = files[n].lines[line].clone(); 660 | } 661 | 'd' if mode == Default => { 662 | //cut line 663 | saved = false; 664 | placement = 0; 665 | files[n].cursor = 0; 666 | start = 0; 667 | if line + 1 == files[n].lines.len() { 668 | clip = files[n].lines.pop().unwrap(); 669 | files[n].lines.push(Vec::new()); 670 | print!("\x1b[G\x1b[K"); 671 | } else { 672 | clip = files[n].lines.remove(line); 673 | if top + height > files[n].lines.len() { 674 | print!("\x1b[G\x1b[J"); 675 | } 676 | clear(&files[n].lines, top, height, start, width); 677 | } 678 | fix_history(&mut files[n].history); 679 | files[n].history.list.insert( 680 | 0, 681 | Point { 682 | add: false, 683 | split: false, 684 | pos: (line, placement), 685 | char: '\0', 686 | line: Some(clip.clone()), 687 | }, 688 | ) 689 | } 690 | 'p' if mode == Default => { 691 | //paste line 692 | saved = false; 693 | files[n].lines.insert(line, clip.clone()); 694 | placement = 0; 695 | files[n].cursor = 0; 696 | start = 0; 697 | clear(&files[n].lines, top, height, start, width); 698 | fix_history(&mut files[n].history); 699 | files[n].history.list.insert( 700 | 0, 701 | Point { 702 | add: true, 703 | split: false, 704 | pos: (line, placement), 705 | char: '\0', 706 | line: Some(clip.clone()), 707 | }, 708 | ) 709 | } 710 | 'R' if mode == Default => { 711 | //replace all 712 | if let Ok(search) = get_word(&mut stdout, height) { 713 | let search = search.chars().collect::>(); 714 | if let Ok(replace) = get_word(&mut stdout, height) { 715 | let replace = replace.chars().collect::>(); 716 | for i in files[n].lines.iter_mut() { 717 | for j in 0..(i.len() - search.len() + 1) { 718 | if i[j..j + search.len()] == search { 719 | i.splice(j..j + search.len(), replace.clone()); 720 | } 721 | } 722 | } 723 | start = fix_top(start, placement, width); 724 | clear(&files[n].lines, top, height, start, width); 725 | } 726 | } 727 | } 728 | '/' if mode == Default => { 729 | //enable search 730 | mode = Search; 731 | orig = (line, placement); 732 | word = Vec::new() 733 | } 734 | '\x14' | 'Q' if c != 'Q' || mode == Default => exit(), //ctrl+c 735 | 'q' if mode == Default => { 736 | //quit 737 | if saved { 738 | exit(); 739 | } else { 740 | err = "unsaved changes, 'Q' to quit".to_string(); 741 | } 742 | } 743 | '\x05' if mode == Default || mode == Edit => mode = Insert, 744 | 'i' if mode == Default => mode = Edit, 745 | 'v' if mode == Default => mode = Digraph, 746 | 's' if mode == Default => { 747 | if let Ok(file_path) = get_word(&mut stdout, height) { 748 | files[n].save_file_path = file_path; 749 | err = save_file(&mut files[n], &history_dir); 750 | }; 751 | } 752 | 'g' if mode == Default => { 753 | //goto line 754 | if let Ok(number) = get_word(&mut stdout, height) { 755 | match number.parse::() { 756 | Ok(num) if num - 1 < files[n].lines.len() => { 757 | line = num - 1; 758 | if placement > files[n].lines[line].len() { 759 | placement = files[n].lines[line].len(); 760 | } 761 | let s = (top, start); 762 | top = fix_top(top, line, height); 763 | start = fix_top(start, placement, width); 764 | if s != (top, start) { 765 | clear(&files[n].lines, top, height, start, width); 766 | } 767 | } 768 | Ok(_) => err = "file too short".to_string(), 769 | Err(e) => err = e.to_string(), 770 | } 771 | } 772 | } 773 | 'o' if mode == Default => { 774 | if let Ok(file_path) = get_word(&mut stdout, height) { 775 | let j = n; 776 | let path = match canonicalize(&file_path) { 777 | Ok(n) => n, 778 | Err(e) => { 779 | err = e.to_string(); 780 | print_line_number(height, line, placement, top, start, err.clone()); 781 | stdout.flush().unwrap(); 782 | continue; 783 | } 784 | }; 785 | for (index, file) in files.iter().enumerate() { 786 | if canonicalize(&file.save_file_path).unwrap() == path { 787 | n = index; 788 | } 789 | } 790 | if n == j { 791 | files.push(open_file(&file_path, &history_dir)); 792 | n = files.len() - 1; 793 | } 794 | print!("\x1b[H\x1b[J"); 795 | continue 'outer; 796 | }; 797 | } 798 | 'u' if mode == Default && files[n].history.list.len() != files[n].history.pos => { 799 | //undo 800 | match ( 801 | files[n].history.list[files[n].history.pos].add, 802 | files[n].history.list[files[n].history.pos].split, 803 | files[n].history.list[files[n].history.pos].line.clone(), 804 | ) { 805 | (false, false, None) => { 806 | line = files[n].history.list[files[n].history.pos].pos.0; 807 | placement = files[n].history.list[files[n].history.pos].pos.1; 808 | if line == files[n].lines.len() { 809 | files[n].lines.push(Vec::new()); 810 | } 811 | let char = files[n].history.list[files[n].history.pos].char; 812 | files[n].lines[line].insert(placement, char); 813 | placement += 1; 814 | } 815 | (true, false, None) => { 816 | line = files[n].history.list[files[n].history.pos].pos.0; 817 | placement = files[n].history.list[files[n].history.pos].pos.1 - 1; 818 | files[n].lines[line].remove(placement); 819 | } 820 | (false, true, None) => { 821 | line = files[n].history.list[files[n].history.pos].pos.0 + 1; 822 | placement = 0; 823 | let pos = files[n].history.list[files[n].history.pos].pos.1; 824 | let l = files[n].lines[line - 1].drain(pos..).collect(); 825 | files[n].lines.insert(line, l); 826 | } 827 | (true, true, None) => { 828 | line = files[n].history.list[files[n].history.pos].pos.0 - 1; 829 | placement = files[n].lines[line].len(); 830 | let l = files[n].lines.remove(line + 1); 831 | files[n].lines[line].extend(l); 832 | } 833 | (false, false, Some(l)) => { 834 | line = files[n].history.list[files[n].history.pos].pos.0; 835 | placement = 0; 836 | if line == files[n].lines.len() { 837 | files[n].lines.push(l.clone()); 838 | } else { 839 | files[n].lines.insert(line, l.clone()); 840 | } 841 | } 842 | (true, false, Some(_)) => { 843 | line = files[n].history.list[files[n].history.pos].pos.0; 844 | placement = 0; 845 | files[n].lines.remove(line); 846 | } 847 | (false, true, Some(l)) => { 848 | line = files[n].history.list[files[n].history.pos].pos.0; 849 | placement = files[n].history.list[files[n].history.pos].pos.1; 850 | let mut un = l.clone(); 851 | un.extend(files[n].lines[line].drain(placement..)); 852 | files[n].lines[line].extend(&un); 853 | placement += l.len(); 854 | } 855 | _ => unimplemented!(), 856 | } 857 | files[n].cursor = placement; 858 | top = fix_top(top, line, height); 859 | start = fix_top(start, placement, width); 860 | clear(&files[n].lines, top, height, start, width); 861 | files[n].history.pos += 1; 862 | } 863 | 'U' if mode == Default && files[n].history.pos > 0 => { 864 | //redo 865 | files[n].history.pos -= 1; 866 | match ( 867 | files[n].history.list[files[n].history.pos].add, 868 | files[n].history.list[files[n].history.pos].split, 869 | files[n].history.list[files[n].history.pos].line.clone(), 870 | ) { 871 | (false, false, None) => { 872 | line = files[n].history.list[files[n].history.pos].pos.0; 873 | placement = files[n].history.list[files[n].history.pos].pos.1; 874 | files[n].lines[line].remove(placement); 875 | } 876 | (true, false, None) => { 877 | line = files[n].history.list[files[n].history.pos].pos.0; 878 | placement = files[n].history.list[files[n].history.pos].pos.1 - 1; 879 | let char = files[n].history.list[files[n].history.pos].char; 880 | files[n].lines[line].insert(placement, char); 881 | placement += 1; 882 | } 883 | (false, true, None) => { 884 | line = files[n].history.list[files[n].history.pos].pos.0; 885 | placement = files[n].lines[line].len(); 886 | let l = files[n].lines.remove(line + 1); 887 | files[n].lines[line].extend(l); 888 | } 889 | (true, true, None) => { 890 | line = files[n].history.list[files[n].history.pos].pos.0; 891 | placement = 0; 892 | if line == files[n].lines.len() { 893 | files[n].lines.push(Vec::new()) 894 | } 895 | let pos = files[n].history.list[files[n].history.pos].pos.1; 896 | let l = files[n].lines[line].drain(pos..).collect(); 897 | files[n].lines.insert(line, l); 898 | } 899 | (false, false, Some(_)) => { 900 | line = files[n].history.list[files[n].history.pos].pos.0; 901 | placement = 0; 902 | files[n].lines.remove(line); 903 | } 904 | (true, false, Some(l)) => { 905 | line = files[n].history.list[files[n].history.pos].pos.0; 906 | placement = 0; 907 | if line == files[n].lines.len() { 908 | files[n].lines.push(l.clone()); 909 | } else { 910 | files[n].lines.insert(line, l.clone()); 911 | } 912 | } 913 | (false, true, Some(l)) => { 914 | line = files[n].history.list[files[n].history.pos].pos.0; 915 | placement = files[n].history.list[files[n].history.pos].pos.1; 916 | files[n].lines[line].drain(placement..placement + l.len()); 917 | } 918 | _ => unimplemented!(), 919 | } 920 | files[n].cursor = placement; 921 | top = fix_top(top, line, height); 922 | start = fix_top(start, placement, width); 923 | clear(&files[n].lines, top, height, start, width); 924 | } 925 | '\0' => {} 926 | _ if !c.is_ascii() 927 | || c.is_ascii_graphic() 928 | || c == ' ' 929 | || c == '\t' 930 | || c == '\n' => 931 | { 932 | if mode == Edit || mode == Digraph || mode == Insert { 933 | saved = false; 934 | if mode == Insert && files[n].lines[line].len() != placement { 935 | files[n].lines[line].remove(placement); 936 | } 937 | files[n].lines[line].insert( 938 | placement, 939 | if mode == Digraph { 940 | match c { 941 | 'a' => 'α', 942 | 'A' => 'Α', 943 | 'b' => 'β', 944 | 'B' => 'Β', 945 | 'c' => 'ξ', 946 | 'C' => 'Ξ', 947 | 'd' => 'Δ', 948 | 'D' => 'δ', 949 | 'e' => 'ε', 950 | 'E' => 'Ε', 951 | 'f' => 'φ', 952 | 'F' => 'Φ', 953 | 'g' => 'γ', 954 | 'G' => 'Γ', 955 | 'h' => 'η', 956 | 'H' => 'Η', 957 | 'i' => 'ι', 958 | 'I' => 'Ι', 959 | 'k' => 'κ', 960 | 'K' => 'Κ', 961 | 'l' => 'λ', 962 | 'L' => 'Λ', 963 | 'm' => 'μ', 964 | 'M' => 'Μ', 965 | 'n' => 'ν', 966 | 'N' => 'Ν', 967 | 'o' => 'ο', 968 | 'O' => 'Ο', 969 | 'p' => 'π', 970 | 'P' => 'Π', 971 | 'q' => 'θ', 972 | 'Q' => 'Θ', 973 | 'r' => 'ρ', 974 | 'R' => 'Ρ', 975 | 's' => 'σ', 976 | 'S' => 'Σ', 977 | 't' => 'τ', 978 | 'T' => 'Τ', 979 | 'u' => 'υ', 980 | 'U' => 'Υ', 981 | 'w' => 'ω', 982 | 'W' => 'Ω', 983 | 'y' => 'ψ', 984 | 'Y' => 'Ψ', 985 | 'x' => 'χ', 986 | 'X' => 'Χ', 987 | 'z' => 'ζ', 988 | 'Z' => 'Ζ', 989 | _ => continue, 990 | } 991 | } else { 992 | c 993 | }, 994 | ); 995 | if placement + 1 == width + start { 996 | placement += 1; 997 | files[n].cursor = placement; 998 | start += 1; 999 | clear(&files[n].lines, top, height, start, width); 1000 | } else { 1001 | clear_line(&files[n].lines, line, placement, width, start); 1002 | placement += 1; 1003 | files[n].cursor = placement; 1004 | } 1005 | fix_history(&mut files[n].history); 1006 | files[n].history.list.insert( 1007 | 0, 1008 | Point { 1009 | add: true, 1010 | split: false, 1011 | pos: (line, placement), 1012 | char: c, 1013 | line: None, 1014 | }, 1015 | ) 1016 | } else if mode == Search { 1017 | if c != '\n' { 1018 | if start + width == placement + word.len() + 1 1019 | && files[n].lines[ln.0][ln.1 + word.len()] == c 1020 | { 1021 | start += 1; 1022 | clear(&files[n].lines, top, height, start, width); 1023 | stdout.flush().unwrap(); 1024 | } 1025 | ln = orig; 1026 | word.push(c); 1027 | } 1028 | 'inner: for (l, i) in files[n].lines.iter().enumerate() { 1029 | if l >= ln.0 && word.len() <= i.len() { 1030 | for j in 1031 | if l == ln.0 { ln.1 + 1 } else { 0 }..(i.len() - word.len() + 1) 1032 | { 1033 | if i[j..j + word.len()] == word { 1034 | ln = (l, j); 1035 | (line, placement) = ln; 1036 | top = fix_top(top, line, height); 1037 | start = fix_top(start, placement, width); 1038 | if start + width == placement + 1 { 1039 | start += word.len() 1040 | } 1041 | files[n].cursor = placement; 1042 | clear(&files[n].lines, top, height, start, width); 1043 | break 'inner; 1044 | } 1045 | } 1046 | ln = (0, 0); 1047 | } 1048 | } 1049 | } 1050 | } 1051 | _ => {} 1052 | } 1053 | print_line_number( 1054 | height, 1055 | line, 1056 | placement, 1057 | top, 1058 | start, 1059 | if mode == Search { 1060 | word.iter().collect() 1061 | } else { 1062 | err.clone() 1063 | }, 1064 | ); 1065 | stdout.flush().unwrap(); 1066 | } 1067 | } 1068 | } 1069 | #[derive(PartialEq)] 1070 | enum Mode { 1071 | Edit, 1072 | Insert, 1073 | Search, 1074 | Digraph, 1075 | Default, 1076 | } 1077 | -------------------------------------------------------------------------------- /src/misc.rs: -------------------------------------------------------------------------------- 1 | use crate::history::History; 2 | use crossterm::{ 3 | event::{read, Event, KeyCode, KeyEvent, KeyModifiers}, 4 | terminal, 5 | }; 6 | use std::{ 7 | cmp::{min, Ordering}, 8 | fs::canonicalize, 9 | io::{stdout, Write}, 10 | }; 11 | #[cfg(not(unix))] 12 | use term_size::dimensions; 13 | #[cfg(unix)] 14 | use { 15 | libc::{ioctl, winsize, STDOUT_FILENO, TIOCGWINSZ}, 16 | std::mem, 17 | }; 18 | pub fn help() { 19 | println!( 20 | "'i' edit mode\n\ 21 | 'esc' exit all modes\n\ 22 | 'h' left\n\ 23 | 'l' right\n\ 24 | 'j' down\n\ 25 | 'k' up\n\ 26 | '0' move to beginning of line\n\ 27 | '$' move to end of line\n\ 28 | 'ins' insert mode\n\ 29 | 'page down' move 1 page down\n\ 30 | 'page up' move 1 page up\n\ 31 | 'home' go to start of file\n\ 32 | 'end' go to end of file\n\ 33 | 'y' copy line\n\ 34 | 'd' cut line\n\ 35 | 'p' print line\n\ 36 | 'w' save\n\ 37 | 's' save as\n\ 38 | 'o' open file\n\ 39 | '`' go to next file\n\ 40 | '~' go to last file\n\ 41 | 'q' quit\n\ 42 | 'u' undo\n\ 43 | 'U' redo\n\ 44 | 'R' replace all\n\ 45 | 'g' goto line #\n\ 46 | '/' search mode\n\ 47 | 'v' greek mode" 48 | ); 49 | } 50 | pub fn exit() { 51 | print!("\x1b[G\x1b[{}B\x1b[?1049l", get_dimensions().0); 52 | stdout().flush().unwrap(); 53 | terminal::disable_raw_mode().unwrap(); 54 | std::process::exit(0); 55 | } 56 | pub fn fix_history(history: &mut History) { 57 | if history.pos != 0 { 58 | history.list.drain(..history.pos); 59 | history.pos = 0; 60 | } 61 | } 62 | pub fn fix_top(top: usize, line: usize, height: usize) -> usize { 63 | match top.cmp(&line) { 64 | Ordering::Greater => line, 65 | Ordering::Less => { 66 | if height > line { 67 | 0 68 | } else if top + height > line { 69 | top 70 | } else { 71 | line - height + 1 72 | } 73 | } 74 | Ordering::Equal => top, 75 | } 76 | } 77 | pub fn read_single_char() -> char { 78 | match match read() { 79 | Ok(c) => c, 80 | Err(_) => return '\0', 81 | } { 82 | Event::Key(KeyEvent { 83 | code, modifiers, .. 84 | }) => match (code, modifiers) { 85 | (KeyCode::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT) => c, 86 | (KeyCode::Char('c'), KeyModifiers::CONTROL) => '\x14', 87 | (KeyCode::Enter, KeyModifiers::NONE) => '\n', 88 | (KeyCode::Esc, KeyModifiers::NONE) => '\x1A', 89 | (KeyCode::Backspace, KeyModifiers::NONE) => '\x08', 90 | (KeyCode::Char('h'), KeyModifiers::CONTROL) => '\x15', //weird bug ctrl+backspace=ctrl+h 91 | (KeyCode::Left, KeyModifiers::CONTROL) => '\x12', 92 | (KeyCode::Right, KeyModifiers::CONTROL) => '\x13', 93 | (KeyCode::Left, KeyModifiers::NONE) => '\x1B', 94 | (KeyCode::Right, KeyModifiers::NONE) => '\x1C', 95 | (KeyCode::Up, KeyModifiers::NONE) => '\x1D', 96 | (KeyCode::Down, KeyModifiers::NONE) => '\x1E', 97 | (KeyCode::PageDown, KeyModifiers::NONE) => '\x04', 98 | (KeyCode::PageUp, KeyModifiers::NONE) => '\x03', 99 | (KeyCode::End, KeyModifiers::NONE) => '\x02', 100 | (KeyCode::Home, KeyModifiers::NONE) => '\x01', 101 | (KeyCode::Insert, KeyModifiers::NONE) => '\x05', 102 | (KeyCode::Delete, KeyModifiers::NONE) => '\x06', 103 | (KeyCode::Tab, KeyModifiers::NONE) => '\t', 104 | _ => '\0', 105 | }, 106 | _ => '\0', 107 | } 108 | } 109 | #[cfg(unix)] 110 | pub fn get_dimensions() -> (usize, usize) { 111 | unsafe { 112 | let mut size: winsize = mem::zeroed(); 113 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut size); 114 | (size.ws_row as usize - 1, size.ws_col as usize) 115 | } 116 | } 117 | #[cfg(not(unix))] 118 | pub fn get_dimensions() -> (usize, usize) { 119 | if let Some((width, height)) = dimensions() { 120 | (height - 1, width) 121 | } else { 122 | (80, 80) 123 | } 124 | } 125 | pub fn clear_line(lines: &[Vec], line: usize, placement: usize, width: usize, start: usize) { 126 | print!( 127 | "\x1b[K{}", 128 | lines[line][placement..min(lines[line].len(), width + start)] 129 | .iter() 130 | .collect::() 131 | .replace('\t', " ") 132 | ); 133 | } 134 | pub fn clear(lines: &[Vec], top: usize, height: usize, start: usize, width: usize) { 135 | print!( 136 | "\x1b[?25l\x1b[H{}\x1b[?25h", 137 | lines[top..min(lines.len(), height + top)] 138 | .iter() 139 | .map(|vec| { 140 | if start <= vec.len() { 141 | vec[start..min(vec.len(), width + start)] 142 | .iter() 143 | .collect::() 144 | + "\x1b[K" 145 | } else { 146 | "\x1b[K".to_string() 147 | } 148 | }) 149 | .collect::>() 150 | .join("\x1b[E\x1b[G") 151 | .replace('\t', " ") 152 | ); 153 | } 154 | pub fn print_line_number( 155 | height: usize, 156 | line: usize, 157 | placement: usize, 158 | top: usize, 159 | start: usize, 160 | word: String, 161 | ) { 162 | let n = format!("{},{}", line + 1, placement + 1); 163 | let i = 10 * (n.len().ilog10() + 1) as usize - n.len(); 164 | print!( 165 | "\x1b[H\x1b[{}B\x1b[K{} \x1b[{}C{}\x1b[H{}{}", 166 | height + 1, 167 | n, 168 | i, 169 | word, 170 | if line - top == 0 { 171 | String::new() 172 | } else { 173 | "\x1b[".to_owned() + &(line - top).to_string() + "B" 174 | }, 175 | if placement - start == 0 { 176 | String::new() 177 | } else { 178 | "\x1b[".to_owned() + &(placement - start).to_string() + "C" 179 | } 180 | ); 181 | } 182 | #[cfg(unix)] 183 | pub fn get_file(path: &str, history_dir: &str) -> String { 184 | history_dir.to_owned() 185 | + &canonicalize(path) 186 | .unwrap() 187 | .to_str() 188 | .unwrap() 189 | .replace('/', "%") 190 | } 191 | #[cfg(not(unix))] 192 | pub fn get_file(path: &str, history_dir: &str) -> String { 193 | let history_file = canonicalize(path) 194 | .unwrap() 195 | .to_str() 196 | .unwrap() 197 | .replace('\\', "%"); 198 | history_dir.to_owned() + &history_file[history_file.find(':').unwrap() + 1..] 199 | } 200 | --------------------------------------------------------------------------------