<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>hi2code&#39;s blog</title>
    <link>https://blog.hi2code.com/</link>
    <description>Recent content on hi2code&#39;s blog</description>
    <generator>Hugo -- 0.147.7</generator>
    <language>en</language>
    <lastBuildDate>Thu, 30 May 2024 12:47:56 +0000</lastBuildDate>
    <atom:link href="https://blog.hi2code.com/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>xargs Usage</title>
      <link>https://blog.hi2code.com/posts/xargs-usage/</link>
      <pubDate>Thu, 30 May 2024 12:47:56 +0000</pubDate>
      <guid>https://blog.hi2code.com/posts/xargs-usage/</guid>
      <description>&lt;p&gt;Introducing xargs working principle and common usage&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Introducing xargs working principle and common usage</p>
<h2 id="working-principle">Working Principle</h2>
<p>xargs reads data from standard input stdin, and splits according to delimiters (usually spaces, tabs, or newlines), passes the split parts as parameters to the following command. Then repeats continuously. Steps as follows:</p>
<ol>
<li>Read stdin, detect delimiter</li>
<li>Build command, execute</li>
<li>Wait for command execution to complete, then repeat step 1</li>
</ol>
<h2 id="common-usage">Common Usage</h2>
<p>xargs is commonly used when needing to pass one command&rsquo;s output as another command&rsquo;s parameters. Compare <code>|</code> data flow as follows:</p>
<ul>
<li><code>|</code> stdout -&gt; stdin: Pipe can make standard output to standard input</li>
<li><code>xargs</code> stdin -&gt; command arguments: Can make standard input to command line arguments</li>
<li><code>command1 | xargs command2</code> stdout -&gt; stdin -&gt; command arguments Combined use can make command 1 output to command 2 parameters</li>
</ul>
<p>When xargs doesn&rsquo;t use any options, it will split out one part according to each delimiter, then execute command once, is a loop execution process</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Delete multiple files, ls shows all files, filter txt format, then each file as rm parameter, loop delete</span>
</span></span><span style="display:flex;"><span>ls | grep .txt | xargs rm
</span></span></code></pre></div><p>When building complex commands, can use <code>-I</code> option, -I followed by placeholder, each replacement, the split fragment will replace placeholder to build command</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ls | grep .txt | xargs -I <span style="color:#f92672">{}</span> cp <span style="color:#f92672">{}</span> /destination/
</span></span></code></pre></div><p>Can also use -P option for concurrent command execution</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cat file.txt | xargs -P <span style="color:#ae81ff">4</span> -I <span style="color:#f92672">{}</span> command <span style="color:#f92672">{}</span>
</span></span></code></pre></div><h2 id="how-to-debug-xargs">How to debug xargs?</h2>
<p>Use <code>-p</code> option can enter prompt mode, will prompt user before each command execution, this way can see each executed command</p>
<h2 id="references">References</h2>
<ul>
<li>
<p><a href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html">xargs - construct argument lists and invoke utility</a></p>
</li>
<li>
<p><a href="https://runcloud.io/blog/pipes-vs-xargs-writing-bash-scripts-in-linux">Pipes vs Xargs: Which One To Use When Writing Bash Scripts In Linux</a></p>
</li>
<li>
<p><a href="https://stackoverflow.com/questions/35589179/when-to-use-xargs-when-piping">When to use xargs when piping?</a></p>
</li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>Java Series - String Processing</title>
      <link>https://blog.hi2code.com/posts/java-string-processing/</link>
      <pubDate>Tue, 28 May 2024 22:46:10 +0000</pubDate>
      <guid>https://blog.hi2code.com/posts/java-string-processing/</guid>
      <description>&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;This article mainly introduces Java String related APIs and usage, string operations.&lt;/p&gt;
&lt;h2 id=&#34;string-core-knowledge-points&#34;&gt;String Core Knowledge Points&lt;/h2&gt;
&lt;p&gt;Strings in Java are immutable, once created cannot be modified. All operations on original strings such as deletion, replacement, concatenation, etc., will not modify the original string, but produce a new string.&lt;/p&gt;
&lt;p&gt;And to reduce memory usage, when created directly with literals, it will look up in the string constant pool. If the same exists, directly return the reference. As shown in the following example.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h2 id="summary">Summary</h2>
<p>This article mainly introduces Java String related APIs and usage, string operations.</p>
<h2 id="string-core-knowledge-points">String Core Knowledge Points</h2>
<p>Strings in Java are immutable, once created cannot be modified. All operations on original strings such as deletion, replacement, concatenation, etc., will not modify the original string, but produce a new string.</p>
<p>And to reduce memory usage, when created directly with literals, it will look up in the string constant pool. If the same exists, directly return the reference. As shown in the following example.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Strings created with literals will first look up in the string constant pool, if same exists, return reference  </span>
</span></span><span style="display:flex;"><span>String s1 <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello World&#34;</span>;  
</span></span><span style="display:flex;"><span>String s2 <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello World&#34;</span>;  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;s1 == s2 ? &#34;</span> <span style="color:#f92672">+</span> (s1 <span style="color:#f92672">==</span> s2));<span style="color:#75715e">// true  </span>
</span></span><span style="display:flex;"><span>  
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Strings created with new String() will be created on heap memory  </span>
</span></span><span style="display:flex;"><span>String s3 <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> String(<span style="color:#e6db74">&#34;Hello World&#34;</span>);  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;s1 == s3 ? &#34;</span> <span style="color:#f92672">+</span> (s1 <span style="color:#f92672">==</span> s3));<span style="color:#75715e">// false</span>
</span></span></code></pre></div><h2 id="string-operations">String Operations</h2>
<p>String operations can be divided into the following categories:</p>
<ol>
<li>
<p>Creation:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String s1 <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello World&#34;</span>;<span style="color:#75715e">// Create directly with literal</span>
</span></span><span style="display:flex;"><span>String s2 <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> String(<span style="color:#e6db74">&#34;Hello World&#34;</span>);<span style="color:#75715e">// new a String, placed on heap</span>
</span></span><span style="display:flex;"><span>String oneStr <span style="color:#f92672">=</span> String.<span style="color:#a6e22e">valueOf</span>(1);<span style="color:#75715e">// Create by converting from other data types</span>
</span></span></code></pre></div></li>
<li>
<p>Concatenation:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Concatenate with + sign</span>
</span></span><span style="display:flex;"><span>String s1 <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello&#34;</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34; World&#34;</span>;
</span></span></code></pre></div></li>
<li>
<p>Splitting:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#75715e">// Split by specified string</span>
</span></span><span style="display:flex;"><span>String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;apple,banana,cherry&#34;</span>;  
</span></span><span style="display:flex;"><span>String<span style="color:#f92672">[]</span> parts <span style="color:#f92672">=</span> str.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#34;,&#34;</span>);  
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> (String part : parts) {  
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(part);  
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>Substring:</p>
<p>substring function, pass in start and end positions, substring position follows left-closed right-open principle, i.e., includes start position, excludes end position</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello, world!&#34;</span>;  
</span></span><span style="display:flex;"><span>String subStr <span style="color:#f92672">=</span> str.<span style="color:#a6e22e">substring</span>(7);  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(subStr);  <span style="color:#75715e">// Output: world!</span>
</span></span><span style="display:flex;"><span>String subStr <span style="color:#f92672">=</span> str.<span style="color:#a6e22e">substring</span>(7, 12);
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(subStr); <span style="color:#75715e">// Output: world</span>
</span></span></code></pre></div></li>
<li>
<p>Search:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello&#34;</span>;  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">contains</span>(<span style="color:#e6db74">&#34;ll&#34;</span>));<span style="color:#75715e">//true  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">startsWith</span>(<span style="color:#e6db74">&#34;H&#34;</span>));<span style="color:#75715e">//true  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">endsWith</span>(<span style="color:#e6db74">&#34;o&#34;</span>));<span style="color:#75715e">//true  </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Locate character position  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">indexOf</span>(<span style="color:#e6db74">&#34;l&#34;</span>));<span style="color:#75715e">//2  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">lastIndexOf</span>(<span style="color:#e6db74">&#34;l&#34;</span>));<span style="color:#75715e">//3  </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Get from position  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">charAt</span>(0));<span style="color:#75715e">//H  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">charAt</span>(str.<span style="color:#a6e22e">length</span>() <span style="color:#f92672">-</span> 1));<span style="color:#75715e">//o</span>
</span></span></code></pre></div></li>
<li>
<p>Replacement and Modification:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello&#34;</span>;  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">replace</span>(<span style="color:#e6db74">&#34;H&#34;</span>,<span style="color:#e6db74">&#34;h&#34;</span>));<span style="color:#75715e">//hello  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">replace</span>(<span style="color:#e6db74">&#34;llo&#34;</span>,<span style="color:#e6db74">&#34;&#34;</span>));<span style="color:#75715e">//He, used for deletion</span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">replaceAll</span>(<span style="color:#e6db74">&#34;[a-z]&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>));<span style="color:#75715e">//H, delete all lowercase letters</span>
</span></span></code></pre></div></li>
<li>
<p>Formatting:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String formattedString <span style="color:#f92672">=</span> String.<span style="color:#a6e22e">format</span>(<span style="color:#e6db74">&#34;Integer: %d&#34;</span>, 2);
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(formattedString);  <span style="color:#75715e">// Output: Integer: 42</span>
</span></span></code></pre></div></li>
<li>
<p>Case Conversion:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Hello&#34;</span>;  
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">toLowerCase</span>());<span style="color:#75715e">//hello  </span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(str.<span style="color:#a6e22e">toUpperCase</span>());<span style="color:#75715e">//HELLO</span>
</span></span></code></pre></div></li>
<li>
<p>Remove Specified Characters:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;  hello  &#34;</span>.<span style="color:#a6e22e">trim</span>());<span style="color:#75715e">//hello</span>
</span></span></code></pre></div></li>
</ol>
<h2 id="stringbuilder-and-stringbuffer-usage">StringBuilder and StringBuffer Usage</h2>
<p>Because String cannot be modified once created, for strings that need frequent modification, StringBuilder and StringBuffer should be used.</p>
<ul>
<li>StringBuilder is not thread-safe, but efficient, suitable for single-thread situations</li>
<li>StringBuffer is thread-safe, suitable for multiple threads modifying the same string</li>
</ul>
<p>For specific implementation principles, check the source code analysis section below.</p>
<h2 id="stringsutils-utility-library">StringsUtils Utility Library</h2>
<p><a href="https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html">StringsUtils</a> is Apache&rsquo;s string processing utility library, encapsulating common complex string processing. And null-safe (i.e., if the passed string is null, it won&rsquo;t cause Null Point Exception).</p>
<ul>
<li>IsEmpty - Check if string is empty string, i.e. &quot;&quot;</li>
<li>IsBlank - Check if string is blank string, e.g. <code>&quot;\n\t&quot;</code> such blank strings return true</li>
<li>Trim/Strip - Remove leading and trailing spaces</li>
<li>Equals/Compare - Compare two strings in a null-safe way</li>
<li>startsWith - Check if string starts with prefix in a null-safe way</li>
<li>endsWith - Check if string ends with suffix in a null-safe way</li>
<li>IndexOf/LastIndexOf/Contains - null-safe index checking</li>
<li>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - Retrieve index of any in a set of strings</li>
<li>ContainsOnly/ContainsNone/ContainsAny - Check if string only contains/doesn&rsquo;t contain/contains any of these characters</li>
<li>Substring/Left/Right/Mid - null-safe substring extraction</li>
<li>SubstringBefore/SubstringAfter/SubstringBetween - Substring extraction relative to other strings</li>
<li>Split/Join - Split string into substring array, vice versa</li>
<li>Remove/Delete - Remove part of string</li>
<li>Replace/Overlay - Search in string and replace with another string</li>
<li>Chomp/Chop - Remove last part of string</li>
<li>AppendIfMissing - Append suffix at end of string if not exists</li>
<li>PrependIfMissing - Add prefix at beginning of string if not exists</li>
<li>LeftPad/RightPad/Center/Repeat - Pad string</li>
<li>UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - Change string case</li>
<li>CountMatches - Count occurrences of one string in another string</li>
<li>IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - Check characters in string</li>
<li>DefaultString - Prevent null input string</li>
<li>Rotate - Rotate (circular shift) string</li>
<li>Reverse/ReverseDelimited - Reverse string</li>
<li>Abbreviate - Abbreviate string using ellipsis or another given string</li>
<li>Difference - Compare strings and report their differences</li>
<li>LevenshteinDistance - Number of changes needed to transform one string to another</li>
</ul>
<h2 id="source-code-analysis">Source Code Analysis</h2>
<h3 id="string">String</h3>
<p>String class is final, immutable</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">final</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">String</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">implements</span> java.<span style="color:#a6e22e">io</span>.<span style="color:#a6e22e">Serializable</span>, Comparable<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span>, CharSequence {
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><h3 id="stringbuilder-and-stringbuffer">StringBuilder and StringBuffer</h3>
<p>StringBuilder and StringBuffer classes, main method is append for adding strings, both inherit from AbstractStringBuilder. The main implementation of append is in the parent class. append in both StringBuilder and StringBuffer calls parent class methods, but StringBuffer for thread safety, each method uses synchronized locking.</p>
<p>StringBuilder thread unsafe</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> StringBuilder <span style="color:#a6e22e">append</span>(CharSequence s) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">super</span>.<span style="color:#a6e22e">append</span>(s);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">this</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>StringBuffer is thread-safe, you can see all append methods use synchronized keyword</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">synchronized</span> StringBuffer <span style="color:#a6e22e">append</span>(CharSequence s) {
</span></span><span style="display:flex;"><span>    toStringCache <span style="color:#f92672">=</span> <span style="color:#66d9ef">null</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">super</span>.<span style="color:#a6e22e">append</span>(s);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">this</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="references">References</h2>
<ul>
<li><a href="https://www.digitalocean.com/community/tutorials/what-is-java-string-pool">What is Java String Pool?</a></li>
</ul>
]]></content:encoded>
    </item>
    <item>
      <title>Resolve Tailwind CSS IntelliSense Extension Vscode Not Working</title>
      <link>https://blog.hi2code.com/posts/tailwindcss_vscode_plugin_not_work_debug/</link>
      <pubDate>Mon, 01 Jan 2024 18:44:36 +0000</pubDate>
      <guid>https://blog.hi2code.com/posts/tailwindcss_vscode_plugin_not_work_debug/</guid>
      <description>&lt;p&gt;When using tailwindcss in vscode, you may encounter issues where the plugin does not work, fails to auto-complete, and does not display class names in color. There are several methods to troubleshoot this issue.&lt;/p&gt;
&lt;p&gt;The problems can be investigated in the following order.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the Plugin&amp;rsquo;s Log Output
Use Ctrl + Shift + P, search for tailwind css show output. Check the log for any errors. For instance, if there are errors related to missing dependencies.
For example, if the log shows an error like Tailwind CSS: Can&amp;rsquo;t resolve ‘@headlessui/tailwindcss’, then installing the corresponding dependency can solve the problem.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;npm i --save-dev @headlessui/tailwindcss
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start=&#34;2&#34;&gt;
&lt;li&gt;File Not Included in tailwindcss
Check if the file is included in tailwindcss. Open the tailwind.config.js file, and add the files that need to be included in the content field.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;/** @type {import(&amp;#39;tailwindcss&amp;#39;).Config} */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;module&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;exports&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;content&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;./src/**/*.{html,js,html.j2}&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;./templates/**/*.html.j2&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;./public/**/*.{html,js,css}&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  ],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;theme&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;extend&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; {},
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;plugins&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt; [],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start=&#34;3&#34;&gt;
&lt;li&gt;vscode Not Enabling Quick Suggestions for String Inputs&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Add the following configuration in vscode&amp;rsquo;s settings.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When using tailwindcss in vscode, you may encounter issues where the plugin does not work, fails to auto-complete, and does not display class names in color. There are several methods to troubleshoot this issue.</p>
<p>The problems can be investigated in the following order.</p>
<ol>
<li>Check the Plugin&rsquo;s Log Output
Use Ctrl + Shift + P, search for tailwind css show output. Check the log for any errors. For instance, if there are errors related to missing dependencies.
For example, if the log shows an error like Tailwind CSS: Can&rsquo;t resolve ‘@headlessui/tailwindcss’, then installing the corresponding dependency can solve the problem.</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>npm i --save-dev @headlessui/tailwindcss
</span></span></code></pre></div><ol start="2">
<li>File Not Included in tailwindcss
Check if the file is included in tailwindcss. Open the tailwind.config.js file, and add the files that need to be included in the content field.</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">/** @type {import(&#39;tailwindcss&#39;).Config} */</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">content</span><span style="color:#f92672">:</span> [
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./src/**/*.{html,js,html.j2}&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./templates/**/*.html.j2&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./public/**/*.{html,js,css}&#34;</span>,
</span></span><span style="display:flex;"><span>  ],
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">theme</span><span style="color:#f92672">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">extend</span><span style="color:#f92672">:</span> {},
</span></span><span style="display:flex;"><span>  },
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">plugins</span><span style="color:#f92672">:</span> [],
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div><ol start="3">
<li>vscode Not Enabling Quick Suggestions for String Inputs</li>
</ol>
<p>Add the following configuration in vscode&rsquo;s settings.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#e6db74">&#34;editor.quickSuggestions&#34;</span><span style="color:#f92672">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;strings&#34;</span><span style="color:#f92672">:</span> <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><ol start="4">
<li>Plugin Not Activated for Certain Special File Formats</li>
</ol>
<p>Due to Django&rsquo;s Jinja template files, usually with a j2 extension, the vscode configuration needs to be modified as follows.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span>  <span style="color:#e6db74">&#34;tailwindCSS.includeLanguages&#34;</span><span style="color:#960050;background-color:#1e0010">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;html&#34;</span>: <span style="color:#e6db74">&#34;html&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;javascript&#34;</span>: <span style="color:#e6db74">&#34;javascript&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;css&#34;</span>: <span style="color:#e6db74">&#34;css&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;typescript&#34;</span>: <span style="color:#e6db74">&#34;typescript&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;jinja-html&#34;</span>:<span style="color:#e6db74">&#34;html&#34;</span>
</span></span><span style="display:flex;"><span>  }<span style="color:#960050;background-color:#1e0010">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#e6db74">&#34;files.associations&#34;</span><span style="color:#960050;background-color:#1e0010">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;*.j2&#34;</span>: <span style="color:#e6db74">&#34;jinja-html&#34;</span>
</span></span><span style="display:flex;"><span>  }<span style="color:#960050;background-color:#1e0010">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#e6db74">&#34;editor.quickSuggestions&#34;</span><span style="color:#960050;background-color:#1e0010">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&#34;strings&#34;</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>  }
</span></span></code></pre></div><p>Modify the tailwind.config.js file as follows, adding support for j2 format.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">content</span><span style="color:#f92672">:</span> [
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./src/**/*.{html,js,html.j2}&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./templates/**/*.html.j2&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;./public/**/*.{html,js,css}&#34;</span>,
</span></span><span style="display:flex;"><span>  ],
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">theme</span><span style="color:#f92672">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">extend</span><span style="color:#f92672">:</span> {},
</span></span><span style="display:flex;"><span>  },
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">plugins</span><span style="color:#f92672">:</span> [],
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div>]]></content:encoded>
    </item>
    <item>
      <title>Wrap Third-party JavaScript Library as Vue3 Component</title>
      <link>https://blog.hi2code.com/posts/wrap_javascript_ui_library_to_vue_component/</link>
      <pubDate>Thu, 21 Dec 2023 22:38:57 +0800</pubDate>
      <guid>https://blog.hi2code.com/posts/wrap_javascript_ui_library_to_vue_component/</guid>
      <description>&lt;p&gt;Wrap third-party UI components as Vue components, using &lt;a href=&#34;https://github.com/frappe/gantt&#34;&gt;Frappe Gantt&lt;/a&gt; as example
Want to use Gantt chart component in Vue, reference vue-echart implementation&lt;/p&gt;
&lt;h1 id=&#34;conversion-steps&#34;&gt;Conversion Steps&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;In on Mount hook, initialize, and convert callbacks to event emit&lt;/li&gt;
&lt;li&gt;Implement v-model two-way binding
&lt;ol&gt;
&lt;li&gt;Initialize, then emit events in js library callbacks&lt;/li&gt;
&lt;li&gt;Pass all changes from js library to vue component (modify passed props in callback event emit function)&lt;/li&gt;
&lt;li&gt;Changes from external vue component pass to js library (use watch to implement, when vue component state changes, synchronously modify js library ui)&lt;/li&gt;
&lt;li&gt;Finally handle all changes to internal content of slots (option lists). (use updated lifecycle hook)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;what-are-web-components&#34;&gt;What are Web Components&lt;/h1&gt;
&lt;p&gt;&lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements&#34;&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements&lt;/a&gt;
Need to wrap as a web component&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Wrap third-party UI components as Vue components, using <a href="https://github.com/frappe/gantt">Frappe Gantt</a> as example
Want to use Gantt chart component in Vue, reference vue-echart implementation</p>
<h1 id="conversion-steps">Conversion Steps</h1>
<ol>
<li>In on Mount hook, initialize, and convert callbacks to event emit</li>
<li>Implement v-model two-way binding
<ol>
<li>Initialize, then emit events in js library callbacks</li>
<li>Pass all changes from js library to vue component (modify passed props in callback event emit function)</li>
<li>Changes from external vue component pass to js library (use watch to implement, when vue component state changes, synchronously modify js library ui)</li>
<li>Finally handle all changes to internal content of slots (option lists). (use updated lifecycle hook)</li>
</ol>
</li>
</ol>
<h1 id="what-are-web-components">What are Web Components</h1>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements">https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements</a>
Need to wrap as a web component</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-html" data-lang="html"><span style="display:flex;"><span><span style="color:#75715e">&lt;!DOCTYPE html&gt;</span>
</span></span><span style="display:flex;"><span>&lt;<span style="color:#f92672">html</span> <span style="color:#a6e22e">lang</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;en&#34;</span>&gt;
</span></span><span style="display:flex;"><span>&lt;<span style="color:#f92672">head</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">charset</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;UTF-8&#34;</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">name</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;viewport&#34;</span> <span style="color:#a6e22e">content</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;width=device-width, initial-scale=1.0&#34;</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">title</span>&gt;Custom Element Demo&lt;/<span style="color:#f92672">title</span>&gt;
</span></span><span style="display:flex;"><span>&lt;/<span style="color:#f92672">head</span>&gt;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>&lt;<span style="color:#f92672">body</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">my-custom-element</span> <span style="color:#a6e22e">color</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;blue&#34;</span> <span style="color:#a6e22e">size</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;20px&#34;</span>&gt;Click me!&lt;/<span style="color:#f92672">my-custom-element</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#f92672">script</span>&gt;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyCustomElement</span> <span style="color:#66d9ef">extends</span> <span style="color:#a6e22e">HTMLElement</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">static</span> <span style="color:#a6e22e">observedAttributes</span> <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;color&#34;</span>, <span style="color:#e6db74">&#34;size&#34;</span>];
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">constructor</span>() {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">super</span>();
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">attachShadow</span>({ <span style="color:#a6e22e">mode</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#34;open&#34;</span> }); <span style="color:#75715e">// Use Shadow DOM
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">shadowRoot</span>.<span style="color:#a6e22e">innerHTML</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                    &lt;style&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                        :host {
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                            display: inline-block;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                            padding: 10px;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                            border: 2px solid black;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                            cursor: pointer;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                        }
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                    &lt;/style&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                    &lt;slot&gt;&lt;/slot&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">                `</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">connectedCallback</span>() {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;Custom element added to page.&#34;</span>);
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">updateStyle</span>(); <span style="color:#75715e">// Initialize style
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">addEventListener</span>(<span style="color:#e6db74">&#34;click&#34;</span>, <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">handleClick</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">disconnectedCallback</span>() {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;Custom element removed from page.&#34;</span>);
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">removeEventListener</span>(<span style="color:#e6db74">&#34;click&#34;</span>, <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">handleClick</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">attributeChangedCallback</span>(<span style="color:#a6e22e">name</span>, <span style="color:#a6e22e">oldValue</span>, <span style="color:#a6e22e">newValue</span>) {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">`Attribute </span><span style="color:#e6db74">${</span><span style="color:#a6e22e">name</span><span style="color:#e6db74">}</span><span style="color:#e6db74"> changed: from &#34;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">oldValue</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34; to &#34;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">newValue</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;`</span>);
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">updateStyle</span>(); <span style="color:#75715e">// Update style when attribute changes
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">updateStyle</span>() {
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">color</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">getAttribute</span>(<span style="color:#e6db74">&#34;color&#34;</span>) <span style="color:#f92672">||</span> <span style="color:#e6db74">&#34;black&#34;</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">size</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">getAttribute</span>(<span style="color:#e6db74">&#34;size&#34;</span>) <span style="color:#f92672">||</span> <span style="color:#e6db74">&#34;16px&#34;</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">style</span>.<span style="color:#a6e22e">color</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">color</span>;
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">style</span>.<span style="color:#a6e22e">fontSize</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">size</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">handleClick</span>() {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">alert</span>(<span style="color:#e6db74">&#34;Custom element clicked!&#34;</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">customElements</span>.<span style="color:#a6e22e">define</span>(<span style="color:#e6db74">&#34;my-custom-element&#34;</span>, <span style="color:#a6e22e">MyCustomElement</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Dynamically change attributes
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">setTimeout</span>(() =&gt; {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">customElement</span> <span style="color:#f92672">=</span> document.<span style="color:#a6e22e">querySelector</span>(<span style="color:#e6db74">&#34;my-custom-element&#34;</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">customElement</span>.<span style="color:#a6e22e">setAttribute</span>(<span style="color:#e6db74">&#34;color&#34;</span>, <span style="color:#e6db74">&#34;red&#34;</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">customElement</span>.<span style="color:#a6e22e">setAttribute</span>(<span style="color:#e6db74">&#34;size&#34;</span>, <span style="color:#e6db74">&#34;24px&#34;</span>);
</span></span><span style="display:flex;"><span>        }, <span style="color:#ae81ff">2000</span>);
</span></span><span style="display:flex;"><span>    &lt;/<span style="color:#f92672">script</span>&gt;
</span></span><span style="display:flex;"><span>&lt;/<span style="color:#f92672">body</span>&gt;
</span></span><span style="display:flex;"><span>&lt;/<span style="color:#f92672">html</span>&gt;
</span></span></code></pre></div><h1 id="problems-encountered">Problems Encountered</h1>
<ol>
<li>Third-party component gantt internally updates tasks when dragging, causing watcheffect to respond in loop. Loop, causing infinite updates</li>
</ol>
<pre tabindex="0"><code>1. After external tasks change, need to synchronously update js component library ui
2. And can modify tasks through js component library ui. Also need to synchronously modify to external vue tasks
</code></pre><p>Divided into two situations:</p>
<ul>
<li>If js component only reads data, can use</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> <span style="color:#a6e22e">defineComponent</span>({
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">props</span><span style="color:#f92672">:</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">tasks</span><span style="color:#f92672">:</span> Array <span style="color:#a6e22e">as</span> <span style="color:#a6e22e">PropType</span><span style="color:#f92672">&lt;</span><span style="color:#a6e22e">Tasks</span><span style="color:#f92672">&gt;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">options</span><span style="color:#f92672">:</span> Object,
</span></span><span style="display:flex;"><span>  },
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">emits</span><span style="color:#f92672">:</span> [<span style="color:#e6db74">&#34;date-change&#34;</span>, <span style="color:#e6db74">&#34;update:modelValue&#34;</span>],
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">setup</span>(<span style="color:#a6e22e">props</span>, <span style="color:#a6e22e">context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">root</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">shallowRef</span><span style="color:#f92672">&lt;</span><span style="color:#a6e22e">GanttElement</span><span style="color:#f92672">&gt;</span>();
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">gantt</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">shallowRef</span><span style="color:#f92672">&lt;</span><span style="color:#a6e22e">Gantt</span><span style="color:#f92672">&gt;</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">function</span> <span style="color:#a6e22e">init</span>() {
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">if</span> (<span style="color:#f92672">!</span><span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">value</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#e6db74">&#34;root is &#34;</span>, <span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">value</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">instance</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">gantt</span>.<span style="color:#a6e22e">value</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">Gantt</span>(<span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">value</span>, <span style="color:#a6e22e">props</span>.<span style="color:#a6e22e">tasks</span>, {
</span></span><span style="display:flex;"><span>        ...<span style="color:#a6e22e">props</span>.<span style="color:#a6e22e">options</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">on_date_change</span><span style="color:#f92672">:</span> (<span style="color:#a6e22e">task</span>) =&gt; {
</span></span><span style="display:flex;"><span>          <span style="color:#75715e">// Can get latest task data here
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>          <span style="color:#a6e22e">context</span>.<span style="color:#a6e22e">emit</span>(<span style="color:#e6db74">&#34;date-change&#34;</span>, <span style="color:#a6e22e">task</span>);
</span></span><span style="display:flex;"><span>          <span style="color:#75715e">// Modify props.tasks, return through event
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>          <span style="color:#75715e">// context.emit(&#34;update:modelValue&#34;,)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        },
</span></span><span style="display:flex;"><span>      }));
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">instance</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;Gantt chart initialized successfully.&#34;</span>);
</span></span><span style="display:flex;"><span>      } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#e6db74">&#34;Failed to initialize Gantt chart.&#34;</span>);
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">onMounted</span>(() =&gt; {
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">init</span>();
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">watchEffect</span>(()=&gt; {
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">props</span>.<span style="color:#a6e22e">tasks</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">gantt</span>.<span style="color:#a6e22e">value</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;tasks update&#34;</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">gantt</span>.<span style="color:#a6e22e">value</span>.<span style="color:#a6e22e">refresh</span>(<span style="color:#a6e22e">props</span>.<span style="color:#a6e22e">tasks</span>);
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> {
</span></span><span style="display:flex;"><span>      <span style="color:#a6e22e">root</span>,
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>  },
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">render</span>() {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">attrs</span> <span style="color:#f92672">=</span> {} <span style="color:#a6e22e">as</span> <span style="color:#a6e22e">any</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">attrs</span>.<span style="color:#a6e22e">ref</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;root&#34;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">h</span>(<span style="color:#a6e22e">TAG_NAME</span>, <span style="color:#a6e22e">attrs</span>);
</span></span><span style="display:flex;"><span>  },
</span></span><span style="display:flex;"><span>});
</span></span></code></pre></div><ul>
<li>If js component modifies data, cannot use like above, because there are two modification points (vue reactivity, js internal component changing data)
<ul>
<li><strong>Clarify data unidirectional flow:</strong>
<ul>
<li>Vue external updates <code>tasks</code>, pass to JS component library, trigger UI update.</li>
<li>JS component library notifies Vue through event callbacks to modify <code>tasks</code>, managed uniformly by Vue.</li>
</ul>
</li>
<li><strong>Avoid directly operating reactive data:</strong>
<ul>
<li>Use local <code>ref</code> copy to transfer <code>tasks</code>, avoid directly modifying <code>props.tasks</code>.</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="watch-old-and-new-values-output-same">watch old and new values output same</h2>
<p><a href="https://stackoverflow.com/questions/62729380/vue-watch-outputs-same-oldvalue-and-newvalue">https://stackoverflow.com/questions/62729380/vue-watch-outputs-same-oldvalue-and-newvalue</a></p>
<h1 id="references">References</h1>
<p><a href="https://www.smashingmagazine.com/2019/02/vue-framework-third-party-javascript/#components-and-component-libraries">https://www.smashingmagazine.com/2019/02/vue-framework-third-party-javascript/#components-and-component-libraries</a></p>
<p><a href="https://codepen.io/smashing-magazine/pen/QoLJJV">https://codepen.io/smashing-magazine/pen/QoLJJV</a></p>
]]></content:encoded>
    </item>
    <item>
      <title>Bash Simple Tutorial</title>
      <link>https://blog.hi2code.com/posts/bash_tutorial/</link>
      <pubDate>Wed, 15 Nov 2023 22:38:57 +0000</pubDate>
      <guid>https://blog.hi2code.com/posts/bash_tutorial/</guid>
      <description>&lt;p&gt;Bash is the default command-line environment (Shell) on most Linux distributions. Its syntax is simple and can conveniently operate native commands on Linux-like systems, suitable for automation work such as backups and quick deployments.&lt;/p&gt;
&lt;p&gt;Bash is the default command-line environment (Shell) on most Linux distributions. Usually, the command-line commands we talk about are executed in Bash.&lt;/p&gt;
&lt;h2 id=&#34;installation&#34;&gt;Installation&lt;/h2&gt;
&lt;p&gt;Installing Bash software:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;macOS&lt;/p&gt;
&lt;p&gt;No installation required, Bash comes pre-installed. The &amp;ldquo;Terminal&amp;rdquo; program can execute Bash scripts.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Bash is the default command-line environment (Shell) on most Linux distributions. Its syntax is simple and can conveniently operate native commands on Linux-like systems, suitable for automation work such as backups and quick deployments.</p>
<p>Bash is the default command-line environment (Shell) on most Linux distributions. Usually, the command-line commands we talk about are executed in Bash.</p>
<h2 id="installation">Installation</h2>
<p>Installing Bash software:</p>
<ul>
<li>
<p>macOS</p>
<p>No installation required, Bash comes pre-installed. The &ldquo;Terminal&rdquo; program can execute Bash scripts.</p>
</li>
<li>
<p>Windows</p>
<p><a href="https://git-scm.com/download/win">Download Git client</a>, which will install a Bash program when installing Git client. After installation, find the Bash program in the Start menu.</p>
</li>
<li>
<p>Linux</p>
<p>No installation required, Bash comes pre-installed. If using a graphical interface, use the &ldquo;Terminal&rdquo; program to execute Bash scripts; in command-line interface, you directly enter the Bash program.</p>
</li>
</ul>
<h2 id="comments">Comments</h2>
<p>In Bash scripts, <code>#</code> represents comments. Strings after the <code>#</code> symbol will be ignored.</p>
<h2 id="first-script">First Script</h2>
<p>Let&rsquo;s open Bash and execute a script in the command line to display files under the root path.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ ls /  <span style="color:#75715e"># Display files under root path. Bash scripts use # symbol for comments, characters after # will be ignored</span>
</span></span><span style="display:flex;"><span>bin   data  etc   opt
</span></span></code></pre></div><blockquote>
<p>Note: In example code, each line starting with <code>$</code> indicates the current script execution user is a non-root account. Lines starting with <code>#</code> indicate the script execution user is a root user.</p>
<p><strong>When copying to your own command line for execution, only copy the part after the <code>$</code> symbol.</strong></p></blockquote>
<p><code>ls</code> is a command (i.e., a program) in Linux used to list files under a path.</p>
<h2 id="execution-process">Execution Process</h2>
<p>The most important point to understand about Bash scripts is: <strong>Each script line is first expanded, then executed</strong>.</p>
<p>Expansion can be simply understood as replacement. Let&rsquo;s change the above command to display files under the user&rsquo;s home path:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>ls ~
</span></span></code></pre></div><p>The execution process is:</p>
<ol>
<li>First expansion: Assuming the username is bob, <code>~</code> will be replaced with <code>/home/bob</code>.</li>
<li>Then execution: <code>ls /home/bob</code></li>
</ol>
<p>This is called tilde expansion. There are many other types of expansions that we will explain gradually.</p>
<h2 id="variable-expansion">Variable Expansion</h2>
<p>There are two types of variables in Bash: environment variables and custom variables.</p>
<blockquote>
<p>We can use the echo command to print variable values, similar to the print function in other languages.</p></blockquote>
<p><strong>Environment Variables:</strong></p>
<p>Environment variables are predefined variables when Bash starts. They can be used directly.</p>
<p>Add <code>$</code> symbol before the variable name to reference the variable.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ echo $HOME <span style="color:#75715e"># HOME variable represents the user&#39;s home path</span>
</span></span><span style="display:flex;"><span>/home/bob
</span></span></code></pre></div><p>The execution process expands <code>$HOME</code> to <code>/home/bob</code> through parameter expansion, then executes.</p>
<p><strong>Custom Variables:</strong></p>
<p>The following example defines a variable a and prints its value.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ a<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span> <span style="color:#75715e"># Define a variable a. Note: no spaces allowed on either side of the equal sign</span>
</span></span><span style="display:flex;"><span>$ echo $a <span style="color:#75715e"># Print the value of variable a</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span>
</span></span></code></pre></div><h2 id="subcommand-expansion">Subcommand Expansion</h2>
<p>We can execute a subcommand first in one line command, then execute this command. The format is <code>$(sub command)</code>.</p>
<p>As in the following command, first execute <code>ls ~</code>, then execute echo to print the subcommand return result.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>echo <span style="color:#66d9ef">$(</span>ls ~<span style="color:#66d9ef">)</span>
</span></span></code></pre></div><h2 id="loop-statements">Loop Statements</h2>
<p>Mainly use two types of loops: while loop and for loop. While loop involves condition judgment, which we&rsquo;ll discuss after learning conditions. Let&rsquo;s first talk about for loop iteration. For loop takes &ldquo;elements&rdquo; sequentially from an &ldquo;element list&rdquo;.</p>
<blockquote>
<p>From here on, we start involving multi-line command scripts. For multi-line scripts, in interactive command line, after pressing Enter, the prompt <code>&gt;</code> allows continued editing, rather than direct execution.</p></blockquote>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>files<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ls ~<span style="color:#66d9ef">)</span>      <span style="color:#75715e"># Subcommand expansion, and assign to files variable</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> file in $files <span style="color:#75715e"># Take each file from files variable in turn, assign to variable file.</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">do</span>                 <span style="color:#75715e"># do indicates loop body start</span>
</span></span><span style="display:flex;"><span>  echo $file        <span style="color:#75715e"># Statements to execute each loop</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">done</span>               <span style="color:#75715e"># done indicates loop body end</span>
</span></span></code></pre></div><h2 id="conditional-statements">Conditional Statements</h2>
<p>Conditional statements in Bash are quite different from other programming languages. Its conditional statements need to use the test command for judgment.</p>
<p>The format of conditional statements is as follows:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#66d9ef">if</span> test-commands; <span style="color:#66d9ef">then</span>
</span></span><span style="display:flex;"><span>  consequent-commands;
</span></span><span style="display:flex;"><span><span style="color:#f92672">[</span><span style="color:#66d9ef">elif</span> more-test-commands; <span style="color:#66d9ef">then</span> <span style="color:#75715e"># elif and else branches are optional</span>
</span></span><span style="display:flex;"><span>  more-consequents;<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">[</span><span style="color:#66d9ef">else</span> alternate-consequents;<span style="color:#f92672">]</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span></code></pre></div><p>if is followed by conditional statements (test-commands), executing different conditional branches based on the truth value of the conditional statements. As shown in the following example, test has many usages. For detailed usage, check <a href="https://www.runoob.com/linux/linux-shell-test.html">here</a>.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#66d9ef">if</span> test <span style="color:#ae81ff">1</span> -ne 2;<span style="color:#66d9ef">then</span> <span style="color:#75715e"># Judge 1 is not equal to 2, here judgment is true, execute if branch</span>
</span></span><span style="display:flex;"><span>  echo Condition is true
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>  echo Condition is false
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span></code></pre></div><p>Another common usage is to judge whether a command executed successfully, as shown in the following example.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>echo Execute some command <span style="color:#75715e"># After we execute some command, we need to judge whether it executed successfully in the script</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> test $? -eq 0; <span style="color:#66d9ef">then</span> 
</span></span><span style="display:flex;"><span><span style="color:#75715e"># $? is the return value of the previous command, -eq 0 judges whether the previous command return value equals 0. Command returns 0 indicates success, returns 1 indicates failure</span>
</span></span><span style="display:flex;"><span> echo Command execution failed
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span></code></pre></div><p>The test command has two other writing methods, but essentially they all call the test command for judgment. <code>[</code> is a simplification of test, so <code>[</code> needs to be immediately followed by a space, otherwise it will report an error.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Single bracket writing</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> <span style="color:#ae81ff">1</span> -ne <span style="color:#ae81ff">2</span> <span style="color:#f92672">]</span>;<span style="color:#66d9ef">then</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Double bracket writing</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#f92672">[[</span> <span style="color:#ae81ff">1</span> -ne <span style="color:#ae81ff">2</span> <span style="color:#f92672">]]</span>;<span style="color:#66d9ef">then</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">fi</span>
</span></span></code></pre></div><h2 id="references">References</h2>
<ul>
<li><a href="https://www.gnu.org/software/bash/manual/bash.html">Bash Reference Manual</a></li>
<li><a href="https://www.30secondsofcode.org/articles/s/terminal-shortcuts-cheatsheet">Bash Shortcuts Cheat Sheet</a></li>
<li><a href="https://wangdoc.com/bash">Bash Script Tutorial</a></li>
</ul>
]]></content:encoded>
    </item>
    <item>
      <title>Python Standard Library Series — datetime</title>
      <link>https://blog.hi2code.com/posts/python_datetime/</link>
      <pubDate>Thu, 15 Jun 2023 22:38:57 +0800</pubDate>
      <guid>https://blog.hi2code.com/posts/python_datetime/</guid>
      <description>&lt;p&gt;Introducing Python standard library datetime&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Introducing Python standard library datetime</p>
<h2 id="time-concepts">Time Concepts</h2>
<p>Need to clarify several concepts first</p>
<ul>
<li>Physical time: Physical concept time, continuously flowing time</li>
<li>Timestamp: Offset from January 1, 1970, 0:00:00 of Coordinated Universal Time (UTC).</li>
<li>UTC time: Based on <a href="https://en.wikipedia.org/wiki/Atomic_time" title="Atomic time">atomic time</a> second length, as close as possible to <a href="https://en.wikipedia.org/wiki/Greenwich_Mean_Time" title="Greenwich Mean Time">Greenwich Mean Time</a> in time.</li>
<li>Timezone: How many hours offset from UTC time, for example Beijing is in UTC+8 timezone, time is UTC time plus 8 hours,</li>
<li>Time string: Human-readable string representing time.
<ul>
<li>RFC 2822: Form like <code>Tue, 1 Jul 2003 10:52:37 +0200</code></li>
<li><a href="https://datatracker.ietf.org/doc/html/rfc3339">RFC 3339</a>: Form like <code>1996-12-19T16:39:57-08:00</code></li>
<li>ISO 8601: Difference from RFC 3339 see <a href="https://ijmacd.github.io/rfc3339-iso8601/">RFC3339-ISO8601</a></li>
</ul>
</li>
</ul>
<p>If Beijing time January 1, 2023, 8 o&rsquo;clock. This 8 o&rsquo;clock refers to 8 o&rsquo;clock in UTC+8 timezone. Corresponding UTC time is January 1, 2023, 0 o&rsquo;clock. Corresponding millisecond timestamp is 1672531200000. RFC3339 string is <code>2023-01-01T08:00:00+08:00</code></p>
<p>Concept memory progression as follows:</p>
<p>Physical time -&gt; Timestamp -&gt; UTC time -&gt; Time with timezone information</p>
<h2 id="examples">Examples</h2>
<p>Class names in datetime module are not capitalized. And there is a datetime class, class name exactly same as module name, easily confused. When using, note: <code>from datetime import datetime</code>. Will import datetime class from datetime module. If directly <code>import datetime</code>, will import module, need to use <code>datetime.datetime</code> to represent datetime class</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> datetime <span style="color:#f92672">import</span> datetime,timedelta
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Get current time. Below are all class methods</span>
</span></span><span style="display:flex;"><span>print(datetime<span style="color:#f92672">.</span>utcnow()) 
</span></span><span style="display:flex;"><span>print(datetime<span style="color:#f92672">.</span>now()) <span style="color:#75715e"># Current timezone time, return type is datetime class</span>
</span></span><span style="display:flex;"><span>print(datetime<span style="color:#f92672">.</span>now()<span style="color:#f92672">.</span>timestamp()) <span style="color:#75715e"># Output float timestamp, before decimal point is seconds, after is milliseconds</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Time addition and subtraction</span>
</span></span><span style="display:flex;"><span>now <span style="color:#f92672">=</span> datetime<span style="color:#f92672">.</span>now()
</span></span><span style="display:flex;"><span>now <span style="color:#f92672">+</span> timedelta(hours<span style="color:#f92672">=</span><span style="color:#ae81ff">8</span>) <span style="color:#75715e"># Add 8 hours</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Mutual conversion with time strings</span>
</span></span><span style="display:flex;"><span>nowstr <span style="color:#f92672">=</span> now<span style="color:#f92672">.</span>strftime(<span style="color:#e6db74">&#39;%Y-%m-</span><span style="color:#e6db74">%d</span><span style="color:#e6db74"> %H:%M:%S&#39;</span>) <span style="color:#75715e"># strftime is str format time abbreviation</span>
</span></span><span style="display:flex;"><span>now <span style="color:#f92672">=</span> datetime<span style="color:#f92672">.</span>strptime(<span style="color:#e6db74">&#39;2023-06-26 00:59:15&#39;</span>,<span style="color:#e6db74">&#39;%Y-%m-</span><span style="color:#e6db74">%d</span><span style="color:#e6db74"> %H:%M:%S&#39;</span>)
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Convert from rfc3339 string to datetime</span>
</span></span><span style="display:flex;"><span>rfc3339_str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;2023-06-25T14:30:00.000Z&#34;</span>
</span></span><span style="display:flex;"><span>datetime<span style="color:#f92672">.</span>fromisoformat(rfc3339_str)
</span></span></code></pre></div><h2 id="datetime-module-source-code-explanation">datetime Module Source Code Explanation</h2>
<p><a href="https://github.com/python/cpython/blob/3.11/Lib/datetime.py">datetime module source code</a>
Inheritance relationship as follows</p>
<pre tabindex="0"><code>object
    timedelta # This class represents time interval
    tzinfo
        timezone # Timezone information
    time
    date # Date
        datetime # Date + time
</code></pre>]]></content:encoded>
    </item>
    <item>
      <title></title>
      <link>https://blog.hi2code.com/about/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://blog.hi2code.com/about/</guid>
      <description>&lt;p&gt;This is hi2code&amp;rsquo;s blog&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>This is hi2code&rsquo;s blog</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
