Explorar o código

Start implementing code for a CLI prompt.

Sam Jaffe %!s(int64=5) %!d(string=hai) anos
pai
achega
f1f82c57c7
Modificáronse 5 ficheiros con 134 adicións e 2 borrados
  1. 10 2
      cli.xcodeproj/project.pbxproj
  2. 58 0
      include/cli/message.h
  3. 27 0
      include/cli/prompt.h
  4. 9 0
      src/prompt.cxx
  5. 30 0
      test/prompt_test.cxx

+ 10 - 2
cli.xcodeproj/project.pbxproj

@@ -12,6 +12,8 @@
 		CDC748E725313CBC008D9D1D /* cli_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC748E625313CBC008D9D1D /* cli_test.cxx */; };
 		CDC748E925313D81008D9D1D /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748B425313553008D9D1D /* GoogleMock.framework */; };
 		CDC748F4253141AF008D9D1D /* libstring-utils.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748F125314196008D9D1D /* libstring-utils.a */; };
+		CDF978A525323ADB00204B5C /* prompt_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDF978A425323ADB00204B5C /* prompt_test.cxx */; };
+		CDF978A725323BFF00204B5C /* prompt.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDF978A625323BFF00204B5C /* prompt.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -75,6 +77,8 @@
 		CDC748E2253136FD008D9D1D /* cli.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cli.cxx; sourceTree = "<group>"; };
 		CDC748E625313CBC008D9D1D /* cli_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cli_test.cxx; sourceTree = "<group>"; };
 		CDC748EA25314195008D9D1D /* string-utils.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = "string-utils.xcodeproj"; path = "external/string-utils/string-utils.xcodeproj"; sourceTree = "<group>"; };
+		CDF978A425323ADB00204B5C /* prompt_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = prompt_test.cxx; sourceTree = "<group>"; };
+		CDF978A625323BFF00204B5C /* prompt.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = prompt.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -136,6 +140,7 @@
 			isa = PBXGroup;
 			children = (
 				CDC748E2253136FD008D9D1D /* cli.cxx */,
+				CDF978A625323BFF00204B5C /* prompt.cxx */,
 			);
 			path = src;
 			sourceTree = "<group>";
@@ -144,6 +149,7 @@
 			isa = PBXGroup;
 			children = (
 				CDC748E625313CBC008D9D1D /* cli_test.cxx */,
+				CDF978A425323ADB00204B5C /* prompt_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -327,6 +333,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				CDC748E5253136FD008D9D1D /* cli.cxx in Sources */,
+				CDF978A725323BFF00204B5C /* prompt.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -335,6 +342,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				CDC748E725313CBC008D9D1D /* cli_test.cxx in Sources */,
+				CDF978A525323ADB00204B5C /* prompt_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -355,7 +363,7 @@
 				ALWAYS_SEARCH_USER_PATHS = NO;
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_CXX_LIBRARY = "libc++";
 				CLANG_ENABLE_MODULES = YES;
 				CLANG_ENABLE_OBJC_ARC = YES;
@@ -415,7 +423,7 @@
 				ALWAYS_SEARCH_USER_PATHS = NO;
 				CLANG_ANALYZER_NONNULL = YES;
 				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
-				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LANGUAGE_STANDARD = "c++17";
 				CLANG_CXX_LIBRARY = "libc++";
 				CLANG_ENABLE_MODULES = YES;
 				CLANG_ENABLE_OBJC_ARC = YES;

+ 58 - 0
include/cli/message.h

@@ -0,0 +1,58 @@
+//
+//  message.h
+//  cli
+//
+//  Created by Sam Jaffe on 10/10/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+#pragma once
+
+#include <iostream>
+#include <memory>
+#include <tuple>
+
+namespace cli { namespace detail {
+
+template <typename T, size_t ...Is>
+void print(std::ostream &os, T const & tup, std::index_sequence<Is...>) {
+  os << std::boolalpha;
+  using swallow = int[];
+  (void)swallow{0, ((void)(os << std::get<Is>(tup)), 0)...};
+  os << std::noboolalpha;
+}
+
+template <typename T> struct up_string { using type = T; };
+template <size_t N> struct up_string<char const(&)[N]> {
+  using type = std::string;
+};
+
+template <typename T> using up_string_t = typename up_string<T>::type;
+
+}}
+
+namespace cli {
+
+struct message {
+  virtual ~message() = default;
+  virtual void print(std::ostream &) const = 0;
+};
+
+
+template <typename ...T>
+struct message_impl : public message {
+  std::tuple<T...> tuple;
+  message_impl(std::tuple<T...> &&tuple) : tuple(std::move(tuple)) {}
+  void print(std::ostream &os) const override {
+    detail::print(os, tuple, std::make_index_sequence<sizeof...(T)>{});
+  }
+};
+
+template <typename ...Args>
+auto make_message(Args &&...args) {
+  using detail::up_string_t;
+  using message_t = message_impl<up_string_t<Args>...>;
+  auto tuple = std::make_tuple(std::forward<up_string_t<Args>>(args)...);
+  return std::make_shared<message_t>(std::move(tuple));
+}
+
+}

+ 27 - 0
include/cli/prompt.h

@@ -0,0 +1,27 @@
+//
+//  prompt.h
+//  cli
+//
+//  Created by Sam Jaffe on 10/10/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <memory>
+
+#include "message.h"
+
+namespace cli {
+
+class prompt {
+private:
+  std::shared_ptr<struct message> input_prompt_;
+  
+public:
+  template <typename ...Args>
+  prompt(Args &&...args)
+      : input_prompt_(make_message(std::forward<Args>(args)...)) {}
+};
+
+}

+ 9 - 0
src/prompt.cxx

@@ -0,0 +1,9 @@
+//
+//  prompt.cxx
+//  cli
+//
+//  Created by Sam Jaffe on 10/10/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#include "cli/prompt.h"

+ 30 - 0
test/prompt_test.cxx

@@ -0,0 +1,30 @@
+//
+//  prompt_test.cxx
+//  cli-test
+//
+//  Created by Sam Jaffe on 10/10/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#include "cli/prompt.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#if XCODE_UNIT_TEST
+// This is a hack to allow XCode to properly display failures when running
+// unit tests.
+#undef EXPECT_THAT
+#define EXPECT_THAT ASSERT_THAT
+#undef EXPECT_TRUE
+#define EXPECT_TRUE ASSERT_TRUE
+#undef EXPECT_FALSE
+#define EXPECT_FALSE ASSERT_FALSE
+#endif
+
+TEST(PromptTest, WillOutputEverythingInMessageWithNoSpaces) {
+  std::stringstream out;
+  auto msg = cli::make_message("Example", 1, true);
+  msg->print(out);
+  EXPECT_THAT(out.str(), "Example1true");
+}